/* * SonarQube * Copyright (C) 2009-2016 SonarSource SA * mailto:contact AT sonarsource DOT com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import * as React from 'react'; import { ReportStatus, subscribe, unsubscribe } from '../../../api/report'; import { translate, translateWithParameters } from '../../../helpers/l10n'; interface Props { component: string; currentUser: { email?: string }; status: ReportStatus; } interface State { loading: boolean; subscribed?: boolean; } export default class Subscription extends React.PureComponent { mounted: boolean; constructor(props: Props) { super(props); this.state = { subscribed: props.status.subscribed, loading: false }; } componentDidMount() { this.mounted = true; } componentWillReceiveProps(nextProps: Props) { if (nextProps.status.subscribed !== this.props.status.subscribed) { this.setState({ subscribed: nextProps.status.subscribed }); } } componentWillUnmount() { this.mounted = false; } stopLoading = () => { if (this.mounted) { this.setState({ loading: false }); } }; handleSubscription = (subscribed: boolean) => { if (this.mounted) { this.setState({ loading: false, subscribed }); } }; handleSubscribe = (e: React.SyntheticEvent) => { e.preventDefault(); e.currentTarget.blur(); this.setState({ loading: true }); subscribe(this.props.component) .then(() => this.handleSubscription(true)) .catch(this.stopLoading); }; handleUnsubscribe = (e: React.SyntheticEvent) => { e.preventDefault(); e.currentTarget.blur(); this.setState({ loading: true }); unsubscribe(this.props.component) .then(() => this.handleSubscription(false)) .catch(this.stopLoading); }; getEffectiveFrequencyText = () => { const effectiveFrequency = this.props.status.componentFrequency || this.props.status.globalFrequency; return translate('report.frequency', effectiveFrequency, 'effective'); }; renderLoading = () => this.state.loading && ; renderWhenSubscribed = () => (
{translateWithParameters('report.subscribed', this.getEffectiveFrequencyText())}
{this.renderLoading()}
); renderWhenNotSubscribed = () => (

{translateWithParameters('report.unsubscribed', this.getEffectiveFrequencyText())}

{this.renderLoading()}
); render() { const hasEmail = !!this.props.currentUser.email; const { subscribed } = this.state; let inner; if (hasEmail) { inner = subscribed ? this.renderWhenSubscribed() : this.renderWhenNotSubscribed(); } else { inner =

{translate('report.no_email_to_subscribe')}

; } return
{inner}
; } }