You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Form.tsx 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import * as React from 'react';
  21. import { translate } from 'sonar-ui-common/helpers/l10n';
  22. import { SubmitButton, ResetButtonLink } from 'sonar-ui-common/components/controls/buttons';
  23. import DeferredSpinner from 'sonar-ui-common/components/ui/DeferredSpinner';
  24. import SimpleModal from 'sonar-ui-common/components/controls/SimpleModal';
  25. import { Alert } from 'sonar-ui-common/components/ui/Alert';
  26. import Select from 'sonar-ui-common/components/controls/Select';
  27. import { getAllMetrics } from '../../../api/metrics';
  28. interface Props {
  29. confirmButtonText: string;
  30. header: string;
  31. measure?: T.CustomMeasure;
  32. onClose: () => void;
  33. onSubmit: (data: { description: string; metricKey: string; value: string }) => Promise<void>;
  34. skipMetrics?: string[];
  35. }
  36. interface State {
  37. description: string;
  38. loading: boolean;
  39. metricKey?: string;
  40. metrics?: T.Metric[];
  41. value: string;
  42. }
  43. export default class Form extends React.PureComponent<Props, State> {
  44. mounted = false;
  45. constructor(props: Props) {
  46. super(props);
  47. this.state = {
  48. description: (props.measure && props.measure.description) || '',
  49. loading: false,
  50. metricKey: props.measure && props.measure.metric.key,
  51. value: (props.measure && props.measure.value) || ''
  52. };
  53. }
  54. componentDidMount() {
  55. this.mounted = true;
  56. if (!this.props.measure) {
  57. this.fetchCustomMetrics();
  58. }
  59. }
  60. componentWillUnmount() {
  61. this.mounted = false;
  62. }
  63. handleSubmit = () => {
  64. return this.state.metricKey
  65. ? this.props
  66. .onSubmit({
  67. description: this.state.description,
  68. metricKey: this.state.metricKey,
  69. value: this.state.value
  70. })
  71. .then(this.props.onClose)
  72. : Promise.reject(undefined);
  73. };
  74. fetchCustomMetrics = () => {
  75. this.setState({ loading: true });
  76. getAllMetrics({ isCustom: true }).then(
  77. metrics => {
  78. if (this.mounted) {
  79. this.setState({ loading: false, metrics });
  80. }
  81. },
  82. () => {
  83. if (this.mounted) {
  84. this.setState({ loading: false });
  85. }
  86. }
  87. );
  88. };
  89. handleMetricSelect = ({ value }: { value: string }) => {
  90. this.setState({ metricKey: value });
  91. };
  92. handleDescriptionChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
  93. this.setState({ description: event.currentTarget.value });
  94. };
  95. handleValueChange = (event: React.ChangeEvent<HTMLInputElement>) => {
  96. this.setState({ value: event.currentTarget.value });
  97. };
  98. renderMetricSelect = (options: { label: string; value: string }[]) => {
  99. if (!options.length && !this.state.loading) {
  100. return <Alert variant="warning">{translate('custom_measures.all_metrics_taken')}</Alert>;
  101. }
  102. return (
  103. <div className="modal-field">
  104. <label htmlFor="create-custom-measure-metric">
  105. {translate('custom_measures.metric')}
  106. <em className="mandatory">*</em>
  107. </label>
  108. {this.state.loading ? (
  109. <i className="spinner" />
  110. ) : (
  111. <Select
  112. autoFocus={true}
  113. clearable={false}
  114. id="create-custom-measure-metric"
  115. onChange={this.handleMetricSelect}
  116. options={options}
  117. value={this.state.metricKey}
  118. />
  119. )}
  120. </div>
  121. );
  122. };
  123. render() {
  124. const { skipMetrics = [] } = this.props;
  125. const { metrics = [] } = this.state;
  126. const options = metrics
  127. .filter(metric => !skipMetrics.includes(metric.key))
  128. .map(metric => ({ label: metric.name, value: metric.key }));
  129. const forbidSubmitting = !this.props.measure && !options.length;
  130. return (
  131. <SimpleModal
  132. header={this.props.header}
  133. onClose={this.props.onClose}
  134. onSubmit={this.handleSubmit}
  135. size="small">
  136. {({ onCloseClick, onFormSubmit, submitting }) => (
  137. <form onSubmit={onFormSubmit}>
  138. <header className="modal-head">
  139. <h2>{this.props.header}</h2>
  140. </header>
  141. <div className="modal-body">
  142. {!this.props.measure && this.renderMetricSelect(options)}
  143. <div className="modal-field">
  144. <label htmlFor="create-custom-measure-value">
  145. {translate('value')}
  146. <em className="mandatory">*</em>
  147. </label>
  148. <input
  149. autoFocus={this.props.measure !== undefined}
  150. id="create-custom-measure-value"
  151. maxLength={200}
  152. name="value"
  153. onChange={this.handleValueChange}
  154. required={true}
  155. type="text"
  156. value={this.state.value}
  157. />
  158. </div>
  159. <div className="modal-field">
  160. <label htmlFor="create-custom-measure-description">
  161. {translate('description')}
  162. </label>
  163. <textarea
  164. id="create-custom-measure-description"
  165. name="description"
  166. onChange={this.handleDescriptionChange}
  167. value={this.state.description}
  168. />
  169. </div>
  170. </div>
  171. <footer className="modal-foot">
  172. <DeferredSpinner className="spacer-right" loading={submitting} />
  173. <SubmitButton
  174. disabled={forbidSubmitting || submitting}
  175. id="create-custom-measure-submit">
  176. {this.props.confirmButtonText}
  177. </SubmitButton>
  178. <ResetButtonLink
  179. disabled={submitting}
  180. id="create-custom-measure-cancel"
  181. onClick={onCloseClick}>
  182. {translate('cancel')}
  183. </ResetButtonLink>
  184. </footer>
  185. </form>
  186. )}
  187. </SimpleModal>
  188. );
  189. }
  190. }