/* * 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 { toNotSoISOString } from 'sonar-ui-common/helpers/dates'; import { translate, translateWithParameters } from 'sonar-ui-common/helpers/l10n'; import { ResetButtonLink, SubmitButton } from 'sonar-ui-common/components/controls/buttons'; import Modal from 'sonar-ui-common/components/controls/Modal'; import { Alert } from 'sonar-ui-common/components/ui/Alert'; import Select from 'sonar-ui-common/components/controls/Select'; import { getPermissionTemplates, bulkApplyTemplate } from '../../api/permissions'; export interface Props { analyzedBefore: Date | undefined; onClose: () => void; organization: string; provisioned: boolean; qualifier: string; query: string; selection: string[]; total: number; } interface State { done: boolean; loading: boolean; permissionTemplate?: string; permissionTemplates?: T.PermissionTemplate[]; submitting: boolean; } export default class BulkApplyTemplateModal extends React.PureComponent { mounted = false; state: State = { done: false, loading: true, submitting: false }; componentDidMount() { this.mounted = true; this.loadPermissionTemplates(); } componentWillUnmount() { this.mounted = false; } loadPermissionTemplates() { this.setState({ loading: true }); getPermissionTemplates(this.props.organization).then( ({ permissionTemplates }) => { if (this.mounted) { this.setState({ loading: false, permissionTemplate: permissionTemplates.length > 0 ? permissionTemplates[0].id : undefined, permissionTemplates }); } }, () => { if (this.mounted) { this.setState({ loading: false }); } } ); } handleConfirmClick = () => { const { analyzedBefore } = this.props; const { permissionTemplate } = this.state; if (permissionTemplate) { this.setState({ submitting: true }); const parameters = this.props.selection.length ? { organization: this.props.organization, projects: this.props.selection.join(), qualifiers: this.props.qualifier, templateId: permissionTemplate } : { analyzedBefore: analyzedBefore && toNotSoISOString(analyzedBefore), onProvisionedOnly: this.props.provisioned || undefined, organization: this.props.organization, qualifiers: this.props.qualifier, q: this.props.query || undefined, templateId: permissionTemplate }; bulkApplyTemplate(parameters).then( () => { if (this.mounted) { this.setState({ done: true, submitting: false }); } }, () => { if (this.mounted) { this.setState({ submitting: false }); } } ); } }; handlePermissionTemplateChange = ({ value }: { value: string }) => { this.setState({ permissionTemplate: value }); }; renderWarning = () => ( {this.props.selection.length ? translateWithParameters( 'permission_templates.bulk_apply_permission_template.apply_to_selected', this.props.selection.length ) : translateWithParameters( 'permission_templates.bulk_apply_permission_template.apply_to_all', this.props.total )} ); renderSelect = () => (