/* * SonarQube * Copyright (C) 2009-2018 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 { getAllMetrics } from '../../../api/metrics'; import { CustomMeasure, Metric } from '../../../app/types'; import DeferredSpinner from '../../../components/common/DeferredSpinner'; import Select from '../../../components/controls/Select'; import SimpleModal from '../../../components/controls/SimpleModal'; import { SubmitButton, ResetButtonLink } from '../../../components/ui/buttons'; import { translate } from '../../../helpers/l10n'; interface Props { confirmButtonText: string; header: string; measure?: CustomMeasure; onClose: () => void; onSubmit: (data: { description: string; metricKey: string; value: string }) => Promise; skipMetrics?: string[]; } interface State { description: string; loading: boolean; metricKey?: string; metrics?: Metric[]; value: string; } export default class Form extends React.PureComponent { mounted = false; constructor(props: Props) { super(props); this.state = { description: (props.measure && props.measure.description) || '', loading: false, metricKey: props.measure && props.measure.metric.key, value: (props.measure && props.measure.value) || '' }; } componentDidMount() { this.mounted = true; if (!this.props.measure) { this.fetchCustomMetrics(); } } componentWillUnmount() { this.mounted = false; } handleSubmit = () => { return this.state.metricKey ? this.props .onSubmit({ description: this.state.description, metricKey: this.state.metricKey, value: this.state.value }) .then(this.props.onClose) : Promise.reject(undefined); }; fetchCustomMetrics = () => { this.setState({ loading: true }); getAllMetrics({ isCustom: true }).then( metrics => { if (this.mounted) { this.setState({ loading: false, metrics }); } }, () => { if (this.mounted) { this.setState({ loading: false }); } } ); }; handleMetricSelect = ({ value }: { value: string }) => { this.setState({ metricKey: value }); }; handleDescriptionChange = (event: React.ChangeEvent) => { this.setState({ description: event.currentTarget.value }); }; handleValueChange = (event: React.ChangeEvent) => { this.setState({ value: event.currentTarget.value }); }; renderMetricSelect = (options: { label: string; value: string }[]) => { if (!options.length && !this.state.loading) { return (
{translate('custom_measures.all_metrics_taken')}
); } return (
{this.state.loading ? ( ) : (