*/
import * as React from 'react';
import { sortBy } from 'lodash';
-import { getImporters, createQualityProfile } from '../../../api/quality-profiles';
+import {
+ changeProfileParent,
+ createQualityProfile,
+ getImporters
+} from '../../../api/quality-profiles';
import Modal from '../../../components/controls/Modal';
import Select from '../../../components/controls/Select';
import { SubmitButton, ResetButtonLink } from '../../../components/ui/buttons';
import { translate } from '../../../helpers/l10n';
+import { Profile } from '../types';
interface Props {
languages: Array<{ key: string; name: string }>;
onClose: () => void;
onCreate: Function;
organization: string | null;
+ profiles: Profile[];
}
interface State {
language?: string;
loading: boolean;
name: string;
+ parent?: string;
preloading: boolean;
}
this.setState({ language: option.value });
};
+ handleParentChange = (option: { value: string } | null) => {
+ this.setState({ parent: option ? option.value : undefined });
+ };
+
handleFormSubmit = (event: React.SyntheticEvent<HTMLFormElement>) => {
event.preventDefault();
}
createQualityProfile(data).then(
- (response: any) => this.props.onCreate(response.profile),
+ ({ profile }: { profile: Profile }) => {
+ if (this.state.parent) {
+ // eslint-disable-next-line promise/no-nesting
+ changeProfileParent(profile.key, this.state.parent).then(
+ () => {
+ this.props.onCreate(profile);
+ },
+ () => {}
+ );
+ } else {
+ this.props.onCreate(profile);
+ }
+ },
() => {
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 (
<Modal contentLabel={header} onRequestClose={this.props.onClose}>
<form id="create-profile-form" onSubmit={this.handleFormSubmit}>
value={selectedLanguage}
/>
</div>
+ {selectedLanguage &&
+ profiles.length && (
+ <div className="modal-field">
+ <label htmlFor="create-profile-parent">
+ {translate('quality_profiles.parent')}
+ </label>
+ <Select
+ clearable={true}
+ id="create-profile-parent"
+ name="parentKey"
+ onChange={this.handleParentChange}
+ options={profiles}
+ value={this.state.parent || ''}
+ />
+ </div>
+ )}
{importers.map(importer => (
<div
className="modal-field spacer-bottom js-importer"