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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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 { ResetButtonLink, SubmitButton } from 'sonar-ui-common/components/controls/buttons';
  22. import Select from 'sonar-ui-common/components/controls/Select';
  23. import SimpleModal from 'sonar-ui-common/components/controls/SimpleModal';
  24. import { Alert } from 'sonar-ui-common/components/ui/Alert';
  25. import DeferredSpinner from 'sonar-ui-common/components/ui/DeferredSpinner';
  26. import MandatoryFieldMarker from 'sonar-ui-common/components/ui/MandatoryFieldMarker';
  27. import { translate } from 'sonar-ui-common/helpers/l10n';
  28. import { getAllMetrics } from '../../../api/metrics';
  29. interface Props {
  30. confirmButtonText: string;
  31. header: string;
  32. measure?: T.CustomMeasure;
  33. onClose: () => void;
  34. onSubmit: (data: { description: string; metricKey: string; value: string }) => Promise<void>;
  35. skipMetrics?: string[];
  36. }
  37. interface State {
  38. description: string;
  39. loading: boolean;
  40. metricKey?: string;
  41. metrics?: T.Metric[];
  42. value: string;
  43. }
  44. export default class Form extends React.PureComponent<Props, State> {
  45. mounted = false;
  46. constructor(props: Props) {
  47. super(props);
  48. this.state = {
  49. description: (props.measure && props.measure.description) || '',
  50. loading: false,
  51. metricKey: props.measure && props.measure.metric.key,
  52. value: (props.measure && props.measure.value) || ''
  53. };
  54. }
  55. componentDidMount() {
  56. this.mounted = true;
  57. if (!this.props.measure) {
  58. this.fetchCustomMetrics();
  59. }
  60. }
  61. componentWillUnmount() {
  62. this.mounted = false;
  63. }
  64. handleSubmit = () => {
  65. return this.state.metricKey
  66. ? this.props
  67. .onSubmit({
  68. description: this.state.description,
  69. metricKey: this.state.metricKey,
  70. value: this.state.value
  71. })
  72. .then(this.props.onClose)
  73. : Promise.reject(undefined);
  74. };
  75. fetchCustomMetrics = () => {
  76. this.setState({ loading: true });
  77. getAllMetrics({ isCustom: true }).then(
  78. metrics => {
  79. if (this.mounted) {
  80. this.setState({ loading: false, metrics });
  81. }
  82. },
  83. () => {
  84. if (this.mounted) {
  85. this.setState({ loading: false });
  86. }
  87. }
  88. );
  89. };
  90. handleMetricSelect = ({ value }: { value: string }) => {
  91. this.setState({ metricKey: value });
  92. };
  93. handleDescriptionChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
  94. this.setState({ description: event.currentTarget.value });
  95. };
  96. handleValueChange = (event: React.ChangeEvent<HTMLInputElement>) => {
  97. this.setState({ value: event.currentTarget.value });
  98. };
  99. renderMetricSelect = (options: { label: string; value: string }[]) => {
  100. if (!options.length && !this.state.loading) {
  101. return <Alert variant="warning">{translate('custom_measures.all_metrics_taken')}</Alert>;
  102. }
  103. return (
  104. <div className="modal-field">
  105. <label htmlFor="create-custom-measure-metric">
  106. {translate('custom_measures.metric')}
  107. <MandatoryFieldMarker />
  108. </label>
  109. {this.state.loading ? (
  110. <i className="spinner" />
  111. ) : (
  112. <Select
  113. autoFocus={true}
  114. clearable={false}
  115. id="create-custom-measure-metric"
  116. onChange={this.handleMetricSelect}
  117. options={options}
  118. value={this.state.metricKey}
  119. />
  120. )}
  121. </div>
  122. );
  123. };
  124. render() {
  125. const { skipMetrics = [] } = this.props;
  126. const { metrics = [] } = this.state;
  127. const options = metrics
  128. .filter(metric => !skipMetrics.includes(metric.key))
  129. .map(metric => ({ label: metric.name, value: metric.key }));
  130. const forbidSubmitting = !this.props.measure && !options.length;
  131. return (
  132. <SimpleModal
  133. header={this.props.header}
  134. onClose={this.props.onClose}
  135. onSubmit={this.handleSubmit}
  136. size="small">
  137. {({ onCloseClick, onFormSubmit, submitting }) => (
  138. <form onSubmit={onFormSubmit}>
  139. <header className="modal-head">
  140. <h2>{this.props.header}</h2>
  141. </header>
  142. <div className="modal-body">
  143. {!this.props.measure && this.renderMetricSelect(options)}
  144. <div className="modal-field">
  145. <label htmlFor="create-custom-measure-value">
  146. {translate('value')}
  147. <MandatoryFieldMarker />
  148. </label>
  149. <input
  150. autoFocus={this.props.measure !== undefined}
  151. id="create-custom-measure-value"
  152. maxLength={200}
  153. name="value"
  154. onChange={this.handleValueChange}
  155. required={true}
  156. type="text"
  157. value={this.state.value}
  158. />
  159. </div>
  160. <div className="modal-field">
  161. <label htmlFor="create-custom-measure-description">
  162. {translate('description')}
  163. </label>
  164. <textarea
  165. id="create-custom-measure-description"
  166. name="description"
  167. onChange={this.handleDescriptionChange}
  168. value={this.state.description}
  169. />
  170. </div>
  171. </div>
  172. <footer className="modal-foot">
  173. <DeferredSpinner className="spacer-right" loading={submitting} />
  174. <SubmitButton
  175. disabled={forbidSubmitting || submitting}
  176. id="create-custom-measure-submit">
  177. {this.props.confirmButtonText}
  178. </SubmitButton>
  179. <ResetButtonLink
  180. disabled={submitting}
  181. id="create-custom-measure-cancel"
  182. onClick={onCloseClick}>
  183. {translate('cancel')}
  184. </ResetButtonLink>
  185. </footer>
  186. </form>
  187. )}
  188. </SimpleModal>
  189. );
  190. }
  191. }