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.

ComponentReportActions.tsx 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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 {
  22. getReportStatus,
  23. subscribeToEmailReport,
  24. unsubscribeFromEmailReport
  25. } from '../../api/component-report';
  26. import withAppStateContext from '../../app/components/app-state/withAppStateContext';
  27. import withCurrentUserContext from '../../app/components/current-user/withCurrentUserContext';
  28. import { addGlobalSuccessMessage } from '../../helpers/globalMessages';
  29. import { translate, translateWithParameters } from '../../helpers/l10n';
  30. import { AppState } from '../../types/appstate';
  31. import { Branch } from '../../types/branch-like';
  32. import { ComponentQualifier } from '../../types/component';
  33. import { ComponentReportStatus } from '../../types/component-report';
  34. import { Component } from '../../types/types';
  35. import { CurrentUser, isLoggedIn } from '../../types/users';
  36. import ComponentReportActionsRenderer from './ComponentReportActionsRenderer';
  37. interface Props {
  38. appState: AppState;
  39. component: Component;
  40. branch?: Branch;
  41. currentUser: CurrentUser;
  42. }
  43. interface State {
  44. loadingStatus?: boolean;
  45. status?: ComponentReportStatus;
  46. }
  47. export class ComponentReportActions extends React.PureComponent<Props, State> {
  48. mounted = false;
  49. state: State = {};
  50. componentDidMount() {
  51. this.mounted = true;
  52. const governanceEnabled = this.props.appState.qualifiers.includes(ComponentQualifier.Portfolio);
  53. if (governanceEnabled) {
  54. this.loadReportStatus();
  55. }
  56. }
  57. componentWillUnmount() {
  58. this.mounted = false;
  59. }
  60. loadReportStatus = async () => {
  61. const { component, branch } = this.props;
  62. const status = await getReportStatus(component.key, branch?.name).catch(() => undefined);
  63. if (this.mounted) {
  64. this.setState({ status, loadingStatus: false });
  65. }
  66. };
  67. handleSubscription = (subscribed: boolean) => {
  68. const { component } = this.props;
  69. const { status } = this.state;
  70. const translationKey = subscribed
  71. ? 'component_report.subscribe_x_success'
  72. : 'component_report.unsubscribe_x_success';
  73. const frequencyTranslation = translate(
  74. 'report.frequency',
  75. status?.componentFrequency || status?.globalFrequency || ''
  76. ).toLowerCase();
  77. const qualifierTranslation = translate('qualifier', component.qualifier).toLowerCase();
  78. addGlobalSuccessMessage(
  79. translateWithParameters(translationKey, frequencyTranslation, qualifierTranslation)
  80. );
  81. this.loadReportStatus();
  82. };
  83. handleSubscribe = async () => {
  84. const { component, branch } = this.props;
  85. await subscribeToEmailReport(component.key, branch?.name);
  86. this.handleSubscription(true);
  87. };
  88. handleUnsubscribe = async () => {
  89. const { component, branch } = this.props;
  90. await unsubscribeFromEmailReport(component.key, branch?.name);
  91. this.handleSubscription(false);
  92. };
  93. render() {
  94. const { currentUser, component, branch } = this.props;
  95. const { status, loadingStatus } = this.state;
  96. if (loadingStatus || !status || (branch && !branch.excludedFromPurge)) {
  97. return null;
  98. }
  99. const currentUserHasEmail = isLoggedIn(currentUser) && !!currentUser.email;
  100. return (
  101. <ComponentReportActionsRenderer
  102. branch={branch}
  103. component={component}
  104. frequency={status.componentFrequency || status.globalFrequency}
  105. subscribed={status.subscribed}
  106. canSubscribe={status.canSubscribe}
  107. currentUserHasEmail={currentUserHasEmail}
  108. handleSubscription={this.handleSubscribe}
  109. handleUnsubscription={this.handleUnsubscribe}
  110. />
  111. );
  112. }
  113. }
  114. export default withCurrentUserContext(withAppStateContext(ComponentReportActions));