Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

DeliveriesForm.tsx 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 { searchDeliveries } from '../../../api/webhooks';
  22. import ListFooter from '../../../components/controls/ListFooter';
  23. import Modal from '../../../components/controls/Modal';
  24. import { ResetButtonLink } from '../../../components/controls/buttons';
  25. import DeferredSpinner from '../../../components/ui/DeferredSpinner';
  26. import { translate, translateWithParameters } from '../../../helpers/l10n';
  27. import { Paging } from '../../../types/types';
  28. import { WebhookDelivery, WebhookResponse } from '../../../types/webhook';
  29. import DeliveryAccordion from './DeliveryAccordion';
  30. interface Props {
  31. onClose: () => void;
  32. webhook: WebhookResponse;
  33. }
  34. interface State {
  35. deliveries: WebhookDelivery[];
  36. loading: boolean;
  37. paging?: Paging;
  38. }
  39. const PAGE_SIZE = 10;
  40. export default class DeliveriesForm extends React.PureComponent<Props, State> {
  41. mounted = false;
  42. state: State = { deliveries: [], loading: true };
  43. componentDidMount() {
  44. this.mounted = true;
  45. this.fetchDeliveries();
  46. }
  47. componentWillUnmount() {
  48. this.mounted = false;
  49. }
  50. fetchDeliveries = ({ webhook } = this.props) => {
  51. searchDeliveries({ webhook: webhook.key, ps: PAGE_SIZE }).then(({ deliveries, paging }) => {
  52. if (this.mounted) {
  53. this.setState({ deliveries, loading: false, paging });
  54. }
  55. }, this.stopLoading);
  56. };
  57. fetchMoreDeliveries = ({ webhook } = this.props) => {
  58. const { paging } = this.state;
  59. if (paging) {
  60. this.setState({ loading: true });
  61. searchDeliveries({ webhook: webhook.key, p: paging.pageIndex + 1, ps: PAGE_SIZE }).then(
  62. ({ deliveries, paging }) => {
  63. if (this.mounted) {
  64. this.setState((state: State) => ({
  65. deliveries: [...state.deliveries, ...deliveries],
  66. loading: false,
  67. paging,
  68. }));
  69. }
  70. },
  71. this.stopLoading
  72. );
  73. }
  74. };
  75. stopLoading = () => {
  76. if (this.mounted) {
  77. this.setState({ loading: false });
  78. }
  79. };
  80. render() {
  81. const { webhook } = this.props;
  82. const { deliveries, loading, paging } = this.state;
  83. const header = translateWithParameters('webhooks.deliveries_for_x', webhook.name);
  84. return (
  85. <Modal contentLabel={header} onRequestClose={this.props.onClose}>
  86. <header className="modal-head">
  87. <h2>{header}</h2>
  88. </header>
  89. <div className="modal-body modal-container">
  90. {deliveries.map((delivery) => (
  91. <DeliveryAccordion delivery={delivery} key={delivery.id} />
  92. ))}
  93. <div className="text-center">
  94. <DeferredSpinner loading={loading} />
  95. </div>
  96. {paging !== undefined && (
  97. <ListFooter
  98. className="little-spacer-bottom"
  99. count={deliveries.length}
  100. loadMore={this.fetchMoreDeliveries}
  101. ready={!loading}
  102. total={paging.total}
  103. />
  104. )}
  105. </div>
  106. <footer className="modal-foot">
  107. <ResetButtonLink className="js-modal-close" onClick={this.props.onClose}>
  108. {translate('close')}
  109. </ResetButtonLink>
  110. </footer>
  111. </Modal>
  112. );
  113. }
  114. }