/* * SonarQube * Copyright (C) 2009-2023 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 { applyTemplateToProject, getPermissionTemplates } from '../../../../api/permissions'; import Select from '../../../../components/controls/Select'; import SimpleModal from '../../../../components/controls/SimpleModal'; import { ResetButtonLink, SubmitButton } from '../../../../components/controls/buttons'; import { Alert } from '../../../../components/ui/Alert'; import MandatoryFieldMarker from '../../../../components/ui/MandatoryFieldMarker'; import MandatoryFieldsExplanation from '../../../../components/ui/MandatoryFieldsExplanation'; import Spinner from '../../../../components/ui/Spinner'; import { translate, translateWithParameters } from '../../../../helpers/l10n'; import { PermissionTemplate } from '../../../../types/types'; interface Props { onApply?: () => void; onClose: () => void; project: { key: string; name: string }; } interface State { done: boolean; loading: boolean; permissionTemplate?: string; permissionTemplates?: 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().then( ({ permissionTemplates }) => { if (this.mounted) { this.setState({ loading: false, permissionTemplates }); } }, () => { if (this.mounted) { this.setState({ loading: false }); } } ); }; handleSubmit = () => { if (this.state.permissionTemplate) { return applyTemplateToProject({ projectKey: this.props.project.key, templateId: this.state.permissionTemplate, }).then(() => { if (this.mounted) { if (this.props.onApply) { this.props.onApply(); } this.setState({ done: true }); } }); } }; handlePermissionTemplateChange = ({ value }: { value: string }) => { this.setState({ permissionTemplate: value }); }; render() { const header = translateWithParameters( 'projects_role.apply_template_to_x', this.props.project.name ); const options = this.state.permissionTemplates ? this.state.permissionTemplates.map((permissionTemplate) => ({ label: permissionTemplate.name, value: permissionTemplate.id, })) : []; return ( {({ onCloseClick, onFormSubmit, submitting }) => (

{header}

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