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.

Report.tsx 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 Subscription from './Subscription';
  22. import { Button } from '../../../components/ui/buttons';
  23. import DropdownIcon from '../../../components/icons-components/DropdownIcon';
  24. import Dropdown from '../../../components/controls/Dropdown';
  25. import { getReportStatus, ReportStatus, getReportUrl } from '../../../api/report';
  26. import { translate } from '../../../helpers/l10n';
  27. interface Props {
  28. component: { key: string; name: string };
  29. }
  30. interface State {
  31. loading: boolean;
  32. status?: ReportStatus;
  33. }
  34. export default class Report extends React.PureComponent<Props, State> {
  35. mounted = false;
  36. state: State = { loading: true };
  37. componentDidMount() {
  38. this.mounted = true;
  39. this.loadStatus();
  40. }
  41. componentWillUnmount() {
  42. this.mounted = false;
  43. }
  44. loadStatus = () => {
  45. getReportStatus(this.props.component.key).then(
  46. status => {
  47. if (this.mounted) {
  48. this.setState({ status, loading: false });
  49. }
  50. },
  51. () => {
  52. if (this.mounted) {
  53. this.setState({ loading: false });
  54. }
  55. }
  56. );
  57. };
  58. render() {
  59. const { component } = this.props;
  60. const { status, loading } = this.state;
  61. if (loading || !status) {
  62. return null;
  63. }
  64. return status.canSubscribe ? (
  65. <Dropdown
  66. overlay={
  67. <ul className="menu">
  68. <li>
  69. <a
  70. download={component.name + ' - Executive Report.pdf'}
  71. href={getReportUrl(component.key)}
  72. target="_blank">
  73. {translate('report.print')}
  74. </a>
  75. </li>
  76. <li>
  77. <Subscription
  78. component={component.key}
  79. onSubscribe={this.loadStatus}
  80. status={status}
  81. />
  82. </li>
  83. </ul>
  84. }
  85. tagName="li">
  86. <Button className="dropdown-toggle">
  87. {translate('portfolio.pdf_report')}
  88. <DropdownIcon className="spacer-left icon-half-transparent" />
  89. </Button>
  90. </Dropdown>
  91. ) : (
  92. <a
  93. className="button"
  94. download={component.name + ' - Executive Report.pdf'}
  95. href={getReportUrl(component.key)}
  96. target="_blank">
  97. {translate('report.print')}
  98. </a>
  99. );
  100. }
  101. }