/* * SonarQube * Copyright (C) 2009-2021 SonarSource SA * mailto:info 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 { connect } from 'react-redux'; import ChevronRightIcon from 'sonar-ui-common/components/icons/ChevronRightIcon'; import DateFormatter from 'sonar-ui-common/components/intl/DateFormatter'; import { translate } from 'sonar-ui-common/helpers/l10n'; import { fetchPrismicNews, fetchPrismicRefs, PrismicNews } from '../../../api/news'; import PlaceholderBar from '../../../components/ui/PlaceholderBar'; import { getGlobalSettingValue, Store } from '../../../store/rootReducer'; interface OwnProps { tag?: string; } interface StateProps { accessToken?: string; } type Props = OwnProps & StateProps; interface State { loading: boolean; news?: PrismicNews; } export class ProductNewsMenuItem extends React.PureComponent { mounted = false; state: State = { loading: false }; componentDidMount() { this.mounted = true; this.fetchProductNews(); } componentWillUnmount() { this.mounted = false; } fetchProductNews = () => { const { accessToken, tag } = this.props; if (accessToken) { this.setState({ loading: true }); fetchPrismicRefs() .then(({ ref }) => fetchPrismicNews({ accessToken, ref, tag })) .then( news => { if (this.mounted) { this.setState({ news: news[0], loading: false }); } }, () => { if (this.mounted) { this.setState({ loading: false }); } } ); } }; renderPlaceholder() { return (

{translate('embed_docs.latest_blog')}

{' '} {' '}

); } render() { const link = 'https://blog.sonarsource.com/'; const { loading, news } = this.state; if (loading) { return this.renderPlaceholder(); } if (!news) { return null; } return (

{translate('embed_docs.latest_blog')}

{formattedDate => {formattedDate}}

{news.data.title}

); } } const mapStateToProps = (state: Store): StateProps => { const accessToken = getGlobalSettingValue(state, 'sonar.prismic.accessToken'); return { accessToken: accessToken && accessToken.value }; }; export default connect(mapStateToProps)(ProductNewsMenuItem);