/* * SonarQube * Copyright (C) 2009-2019 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 { translateWithParameters, translate } from 'sonar-ui-common/helpers/l10n'; import { SubmitButton, ResetButtonLink } from 'sonar-ui-common/components/controls/buttons'; import DeferredSpinner from 'sonar-ui-common/components/ui/DeferredSpinner'; import SimpleModal from 'sonar-ui-common/components/controls/SimpleModal'; import { Alert } from 'sonar-ui-common/components/ui/Alert'; import Select from 'sonar-ui-common/components/controls/Select'; import { getPermissionTemplates, applyTemplateToProject } from '../../../../api/permissions'; interface Props { onApply?: () => void; onClose: () => void; organization: string | undefined; project: { key: string; name: string }; } interface State { done: boolean; loading: boolean; permissionTemplate?: string; permissionTemplates?: T.PermissionTemplate[]; } export default class ApplyTemplate extends React.PureComponent { mounted = false; state: State = { done: false, loading: true }; componentDidMount() { this.mounted = true; this.fetchPermissionTemplates(); } componentWillUnmount() { this.mounted = false; } fetchPermissionTemplates = () => { getPermissionTemplates(this.props.organization).then( ({ permissionTemplates }) => { if (this.mounted) { this.setState({ loading: false, permissionTemplates }); } }, () => { if (this.mounted) { this.setState({ loading: false }); } } ); }; handleSubmit = () => { if (this.state.permissionTemplate) { return applyTemplateToProject({ organization: this.props.organization, projectKey: this.props.project.key, templateId: this.state.permissionTemplate }).then(() => { if (this.mounted) { if (this.props.onApply) { this.props.onApply(); } this.setState({ done: true }); } }); } else { return Promise.reject(undefined); } }; handlePermissionTemplateChange = ({ value }: { value: string }) => { this.setState({ permissionTemplate: value }); }; render() { const header = translateWithParameters( 'projects_role.apply_template_to_xxx', this.props.project.name ); return ( {({ onCloseClick, onFormSubmit, submitting }) => (

{header}

{this.state.done ? ( {translate('projects_role.apply_template.success')} ) : ( <> {this.state.loading ? ( ) : (
{this.state.permissionTemplates && (