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.

Subscription.tsx 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 { connect } from 'react-redux';
  22. import { ReportStatus, subscribe, unsubscribe } from '../../../api/report';
  23. import addGlobalSuccessMessage from '../../../app/utils/addGlobalSuccessMessage';
  24. import throwGlobalError from '../../../app/utils/throwGlobalError';
  25. import { translate, translateWithParameters } from '../../../helpers/l10n';
  26. import { isLoggedIn } from '../../../helpers/users';
  27. import { getCurrentUser, Store } from '../../../store/rootReducer';
  28. interface Props {
  29. component: string;
  30. currentUser: T.CurrentUser;
  31. onSubscribe: () => void;
  32. status: ReportStatus;
  33. }
  34. export class Subscription extends React.PureComponent<Props> {
  35. handleSubscription = (subscribed: boolean) => {
  36. addGlobalSuccessMessage(
  37. subscribed
  38. ? translateWithParameters('report.subscribe_x_success', this.getFrequencyText())
  39. : translateWithParameters('report.unsubscribe_x_success', this.getFrequencyText())
  40. );
  41. this.props.onSubscribe();
  42. };
  43. handleSubscribe = () => {
  44. subscribe(this.props.component)
  45. .then(() => this.handleSubscription(true))
  46. .catch(throwGlobalError);
  47. };
  48. handleUnsubscribe = () => {
  49. unsubscribe(this.props.component)
  50. .then(() => this.handleSubscription(false))
  51. .catch(throwGlobalError);
  52. };
  53. getFrequencyText = () => {
  54. const effectiveFrequency =
  55. this.props.status.componentFrequency || this.props.status.globalFrequency;
  56. return translate('report.frequency', effectiveFrequency);
  57. };
  58. render() {
  59. const hasEmail = isLoggedIn(this.props.currentUser) && !!this.props.currentUser.email;
  60. const { status } = this.props;
  61. if (!hasEmail) {
  62. return <span className="text-muted-2">{translate('report.no_email_to_subscribe')}</span>;
  63. }
  64. return status.subscribed ? (
  65. <a href="#" onClick={this.handleUnsubscribe}>
  66. {translateWithParameters('report.unsubscribe_x', this.getFrequencyText())}
  67. </a>
  68. ) : (
  69. <a href="#" onClick={this.handleSubscribe}>
  70. {translateWithParameters('report.subscribe_x', this.getFrequencyText())}
  71. </a>
  72. );
  73. }
  74. }
  75. const mapStateToProps = (state: Store) => ({
  76. currentUser: getCurrentUser(state)
  77. });
  78. export default connect(mapStateToProps)(Subscription);