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.

DeliveryAccordion.tsx 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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 AlertErrorIcon from 'sonar-ui-common/components/icons/AlertErrorIcon';
  22. import AlertSuccessIcon from 'sonar-ui-common/components/icons/AlertSuccessIcon';
  23. import BoxedGroupAccordion from 'sonar-ui-common/components/controls/BoxedGroupAccordion';
  24. import DeliveryItem from './DeliveryItem';
  25. import DateTimeFormatter from '../../../components/intl/DateTimeFormatter';
  26. import { getDelivery } from '../../../api/webhooks';
  27. interface Props {
  28. delivery: T.WebhookDelivery;
  29. }
  30. interface State {
  31. loading: boolean;
  32. open: boolean;
  33. payload?: string;
  34. }
  35. export default class DeliveryAccordion extends React.PureComponent<Props, State> {
  36. mounted = false;
  37. state: State = { loading: false, open: false };
  38. componentDidMount() {
  39. this.mounted = true;
  40. }
  41. componentWillUnmount() {
  42. this.mounted = false;
  43. }
  44. fetchPayload = ({ delivery } = this.props) => {
  45. this.setState({ loading: true });
  46. return getDelivery({ deliveryId: delivery.id }).then(
  47. ({ delivery }) => {
  48. if (this.mounted) {
  49. this.setState({ payload: delivery.payload, loading: false });
  50. }
  51. },
  52. () => {
  53. if (this.mounted) {
  54. this.setState({ loading: false });
  55. }
  56. }
  57. );
  58. };
  59. formatPayload = (payload: string) => {
  60. try {
  61. return JSON.stringify(JSON.parse(payload), undefined, 2);
  62. } catch (error) {
  63. return payload;
  64. }
  65. };
  66. handleClick = () => {
  67. if (!this.state.payload) {
  68. this.fetchPayload();
  69. }
  70. this.setState(({ open }) => ({ open: !open }));
  71. };
  72. render() {
  73. const { delivery } = this.props;
  74. const { loading, open, payload } = this.state;
  75. return (
  76. <BoxedGroupAccordion
  77. onClick={this.handleClick}
  78. open={open}
  79. renderHeader={() =>
  80. delivery.success ? (
  81. <AlertSuccessIcon className="pull-right js-success" />
  82. ) : (
  83. <AlertErrorIcon className="pull-right js-error" />
  84. )
  85. }
  86. title={<DateTimeFormatter date={delivery.at} />}>
  87. <DeliveryItem
  88. className="big-spacer-left"
  89. delivery={delivery}
  90. loading={loading}
  91. payload={payload}
  92. />
  93. </BoxedGroupAccordion>
  94. );
  95. }
  96. }