/* * SonarQube * Copyright (C) 2009-2022 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 { Helmet } from 'react-helmet-async'; import { connect } from 'react-redux'; import { getSettingsNavigation } from '../../api/nav'; import { getPendingPlugins } from '../../api/plugins'; import { getSystemStatus, waitSystemUPStatus } from '../../api/system'; import handleRequiredAuthorization from '../../app/utils/handleRequiredAuthorization'; import { translate } from '../../helpers/l10n'; import { setAdminPages } from '../../store/appState'; import { getAppState, Store } from '../../store/rootReducer'; import { PendingPluginResult } from '../../types/plugins'; import { AppState, Extension, SysStatus } from '../../types/types'; import AdminContext, { defaultPendingPlugins, defaultSystemStatus } from './AdminContext'; import SettingsNav from './nav/settings/SettingsNav'; interface Props { appState: Pick; location: {}; setAdminPages: (adminPages: Extension[]) => void; } interface State { pendingPlugins: PendingPluginResult; systemStatus: SysStatus; } export class AdminContainer extends React.PureComponent { mounted = false; state: State = { pendingPlugins: defaultPendingPlugins, systemStatus: defaultSystemStatus }; componentDidMount() { this.mounted = true; if (!this.props.appState.canAdmin) { handleRequiredAuthorization(); } else { this.fetchNavigationSettings(); this.fetchPendingPlugins(); this.fetchSystemStatus(); } } componentWillUnmount() { this.mounted = false; } fetchNavigationSettings = () => { getSettingsNavigation().then( r => this.props.setAdminPages(r.extensions), () => {} ); }; fetchPendingPlugins = () => { getPendingPlugins().then( pendingPlugins => { if (this.mounted) { this.setState({ pendingPlugins }); } }, () => {} ); }; fetchSystemStatus = () => { getSystemStatus().then( ({ status }) => { if (this.mounted) { this.setState({ systemStatus: status }); if (status === 'RESTARTING') { this.waitRestartingDone(); } } }, () => {} ); }; waitRestartingDone = () => { waitSystemUPStatus().then( ({ status }) => { if (this.mounted) { this.setState({ systemStatus: status }); document.location.reload(); } }, () => {} ); }; render() { const { adminPages } = this.props.appState; // Check that the adminPages are loaded if (!adminPages) { return null; } const { pendingPlugins, systemStatus } = this.state; const defaultTitle = translate('layout.settings'); return (
{this.props.children}
); } } const mapStateToProps = (state: Store) => ({ appState: getAppState(state) }); const mapDispatchToProps = { setAdminPages }; export default connect(mapStateToProps, mapDispatchToProps)(AdminContainer);