You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WebhookActions.tsx 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import * as React from 'react';
  21. import ActionsDropdown, {
  22. ActionsDropdownDivider,
  23. ActionsDropdownItem,
  24. } from '../../../components/controls/ActionsDropdown';
  25. import { translate } from '../../../helpers/l10n';
  26. import { WebhookResponse, WebhookUpdatePayload } from '../../../types/webhook';
  27. import CreateWebhookForm from './CreateWebhookForm';
  28. import DeleteWebhookForm from './DeleteWebhookForm';
  29. import DeliveriesForm from './DeliveriesForm';
  30. interface Props {
  31. onDelete: (webhook: string) => Promise<void>;
  32. onUpdate: (data: WebhookUpdatePayload) => Promise<void>;
  33. webhook: WebhookResponse;
  34. }
  35. interface State {
  36. deleting: boolean;
  37. deliveries: boolean;
  38. updating: boolean;
  39. }
  40. export default class WebhookActions extends React.PureComponent<Props, State> {
  41. mounted = false;
  42. state: State = { deleting: false, deliveries: false, updating: false };
  43. componentDidMount() {
  44. this.mounted = true;
  45. }
  46. componentWillUnmount() {
  47. this.mounted = false;
  48. }
  49. handleDelete = () => {
  50. return this.props.onDelete(this.props.webhook.key);
  51. };
  52. handleDeleteClick = () => {
  53. this.setState({ deleting: true });
  54. };
  55. handleDeletingStop = () => {
  56. if (this.mounted) {
  57. this.setState({ deleting: false });
  58. }
  59. };
  60. handleDeliveriesClick = () => {
  61. this.setState({ deliveries: true });
  62. };
  63. handleDeliveriesStop = () => {
  64. this.setState({ deliveries: false });
  65. };
  66. handleUpdate = (data: { name: string; secret?: string; url: string }) => {
  67. return this.props.onUpdate({ ...data, webhook: this.props.webhook.key });
  68. };
  69. handleUpdateClick = () => {
  70. this.setState({ updating: true });
  71. };
  72. handleUpdatingStop = () => {
  73. this.setState({ updating: false });
  74. };
  75. render() {
  76. const { webhook } = this.props;
  77. return (
  78. <>
  79. <ActionsDropdown className="big-spacer-left">
  80. <ActionsDropdownItem className="js-webhook-update" onClick={this.handleUpdateClick}>
  81. {translate('update_verb')}
  82. </ActionsDropdownItem>
  83. {webhook.latestDelivery && (
  84. <ActionsDropdownItem
  85. className="js-webhook-deliveries"
  86. onClick={this.handleDeliveriesClick}
  87. >
  88. {translate('webhooks.deliveries.show')}
  89. </ActionsDropdownItem>
  90. )}
  91. <ActionsDropdownDivider />
  92. <ActionsDropdownItem
  93. className="js-webhook-delete"
  94. destructive={true}
  95. onClick={this.handleDeleteClick}
  96. >
  97. {translate('delete')}
  98. </ActionsDropdownItem>
  99. </ActionsDropdown>
  100. {this.state.deliveries && (
  101. <DeliveriesForm onClose={this.handleDeliveriesStop} webhook={webhook} />
  102. )}
  103. {this.state.updating && (
  104. <CreateWebhookForm
  105. onClose={this.handleUpdatingStop}
  106. onDone={this.handleUpdate}
  107. webhook={webhook}
  108. />
  109. )}
  110. {this.state.deleting && (
  111. <DeleteWebhookForm
  112. onClose={this.handleDeletingStop}
  113. onSubmit={this.handleDelete}
  114. webhook={webhook}
  115. />
  116. )}
  117. </>
  118. );
  119. }
  120. }