/* * 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 { sortBy } from 'lodash'; import { translate } from 'sonar-ui-common/helpers/l10n'; import { SubmitButton, ResetButtonLink } from 'sonar-ui-common/components/controls/buttons'; import Modal from 'sonar-ui-common/components/controls/Modal'; import Select from 'sonar-ui-common/components/controls/Select'; import { changeProfileParent, createQualityProfile, getImporters } from '../../../api/quality-profiles'; import { Profile } from '../types'; interface Props { languages: Array<{ key: string; name: string }>; onClose: () => void; onCreate: Function; organization: string | null; profiles: Profile[]; } interface State { importers: Array<{ key: string; languages: Array; name: string }>; language?: string; loading: boolean; name: string; parent?: string; preloading: boolean; } export default class CreateProfileForm extends React.PureComponent { mounted = false; state: State = { importers: [], loading: false, name: '', preloading: true }; componentDidMount() { this.mounted = true; this.fetchImporters(); } componentWillUnmount() { this.mounted = false; } fetchImporters() { getImporters().then( importers => { if (this.mounted) { this.setState({ importers, preloading: false }); } }, () => { if (this.mounted) { this.setState({ preloading: false }); } } ); } handleNameChange = (event: React.SyntheticEvent) => { this.setState({ name: event.currentTarget.value }); }; handleLanguageChange = (option: { value: string }) => { this.setState({ language: option.value }); }; handleParentChange = (option: { value: string } | null) => { this.setState({ parent: option ? option.value : undefined }); }; handleFormSubmit = async (event: React.SyntheticEvent) => { event.preventDefault(); this.setState({ loading: true }); const data = new FormData(event.currentTarget); if (this.props.organization) { data.append('organization', this.props.organization); } try { const { profile } = await createQualityProfile(data); if (this.state.parent) { await changeProfileParent(profile.key, this.state.parent); } this.props.onCreate(profile); } finally { if (this.mounted) { this.setState({ loading: false }); } } }; render() { const header = translate('quality_profiles.new_profile'); const languages = sortBy(this.props.languages, 'name'); let profiles: Array<{ label: string; value: string }> = []; const selectedLanguage = this.state.language || languages[0].key; const importers = this.state.importers.filter(importer => importer.languages.includes(selectedLanguage) ); if (selectedLanguage) { const languageProfiles = this.props.profiles.filter(p => p.language === selectedLanguage); profiles = [ { label: translate('none'), value: '' }, ...sortBy(languageProfiles, 'name').map(profile => ({ label: profile.isBuiltIn ? `${profile.name} (${translate('quality_profiles.built_in')})` : profile.name, value: profile.key })) ]; } return (

{header}

{this.state.preloading ? (
) : (
)} {importers.map(importer => (

{translate('quality_profiles.optional_configuration_file')}

))} {/* drop me when we stop supporting ie11 */}
)}
{this.state.loading && } {!this.state.preloading && ( {translate('create')} )} {translate('cancel')}
); } }