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.

CreateProfileForm.tsx 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 { sortBy } from 'lodash';
  22. import { translate } from 'sonar-ui-common/helpers/l10n';
  23. import { SubmitButton, ResetButtonLink } from 'sonar-ui-common/components/controls/buttons';
  24. import Modal from 'sonar-ui-common/components/controls/Modal';
  25. import Select from 'sonar-ui-common/components/controls/Select';
  26. import {
  27. changeProfileParent,
  28. createQualityProfile,
  29. getImporters
  30. } from '../../../api/quality-profiles';
  31. import { Profile } from '../types';
  32. interface Props {
  33. languages: Array<{ key: string; name: string }>;
  34. onClose: () => void;
  35. onCreate: Function;
  36. organization: string | null;
  37. profiles: Profile[];
  38. }
  39. interface State {
  40. importers: Array<{ key: string; languages: Array<string>; name: string }>;
  41. language?: string;
  42. loading: boolean;
  43. name: string;
  44. parent?: string;
  45. preloading: boolean;
  46. }
  47. export default class CreateProfileForm extends React.PureComponent<Props, State> {
  48. mounted = false;
  49. state: State = { importers: [], loading: false, name: '', preloading: true };
  50. componentDidMount() {
  51. this.mounted = true;
  52. this.fetchImporters();
  53. }
  54. componentWillUnmount() {
  55. this.mounted = false;
  56. }
  57. fetchImporters() {
  58. getImporters().then(
  59. importers => {
  60. if (this.mounted) {
  61. this.setState({ importers, preloading: false });
  62. }
  63. },
  64. () => {
  65. if (this.mounted) {
  66. this.setState({ preloading: false });
  67. }
  68. }
  69. );
  70. }
  71. handleNameChange = (event: React.SyntheticEvent<HTMLInputElement>) => {
  72. this.setState({ name: event.currentTarget.value });
  73. };
  74. handleLanguageChange = (option: { value: string }) => {
  75. this.setState({ language: option.value });
  76. };
  77. handleParentChange = (option: { value: string } | null) => {
  78. this.setState({ parent: option ? option.value : undefined });
  79. };
  80. handleFormSubmit = async (event: React.SyntheticEvent<HTMLFormElement>) => {
  81. event.preventDefault();
  82. this.setState({ loading: true });
  83. const data = new FormData(event.currentTarget);
  84. if (this.props.organization) {
  85. data.append('organization', this.props.organization);
  86. }
  87. try {
  88. const { profile } = await createQualityProfile(data);
  89. if (this.state.parent) {
  90. await changeProfileParent(profile.key, this.state.parent);
  91. }
  92. this.props.onCreate(profile);
  93. } finally {
  94. if (this.mounted) {
  95. this.setState({ loading: false });
  96. }
  97. }
  98. };
  99. render() {
  100. const header = translate('quality_profiles.new_profile');
  101. const languages = sortBy(this.props.languages, 'name');
  102. let profiles: Array<{ label: string; value: string }> = [];
  103. const selectedLanguage = this.state.language || languages[0].key;
  104. const importers = this.state.importers.filter(importer =>
  105. importer.languages.includes(selectedLanguage)
  106. );
  107. if (selectedLanguage) {
  108. const languageProfiles = this.props.profiles.filter(p => p.language === selectedLanguage);
  109. profiles = [
  110. { label: translate('none'), value: '' },
  111. ...sortBy(languageProfiles, 'name').map(profile => ({
  112. label: profile.isBuiltIn
  113. ? `${profile.name} (${translate('quality_profiles.built_in')})`
  114. : profile.name,
  115. value: profile.key
  116. }))
  117. ];
  118. }
  119. return (
  120. <Modal contentLabel={header} onRequestClose={this.props.onClose} size="small">
  121. <form id="create-profile-form" onSubmit={this.handleFormSubmit}>
  122. <div className="modal-head">
  123. <h2>{header}</h2>
  124. </div>
  125. {this.state.preloading ? (
  126. <div className="modal-body">
  127. <i className="spinner" />
  128. </div>
  129. ) : (
  130. <div className="modal-body">
  131. <div className="modal-field">
  132. <label htmlFor="create-profile-name">
  133. {translate('name')}
  134. <em className="mandatory">*</em>
  135. </label>
  136. <input
  137. autoFocus={true}
  138. id="create-profile-name"
  139. maxLength={100}
  140. name="name"
  141. onChange={this.handleNameChange}
  142. required={true}
  143. size={50}
  144. type="text"
  145. value={this.state.name}
  146. />
  147. </div>
  148. <div className="modal-field">
  149. <label htmlFor="create-profile-language">
  150. {translate('language')}
  151. <em className="mandatory">*</em>
  152. </label>
  153. <Select
  154. clearable={false}
  155. id="create-profile-language"
  156. name="language"
  157. onChange={this.handleLanguageChange}
  158. options={languages.map(language => ({
  159. label: language.name,
  160. value: language.key
  161. }))}
  162. value={selectedLanguage}
  163. />
  164. </div>
  165. {selectedLanguage && profiles.length && (
  166. <div className="modal-field">
  167. <label htmlFor="create-profile-parent">
  168. {translate('quality_profiles.parent')}
  169. </label>
  170. <Select
  171. clearable={true}
  172. id="create-profile-parent"
  173. name="parentKey"
  174. onChange={this.handleParentChange}
  175. options={profiles}
  176. value={this.state.parent || ''}
  177. />
  178. </div>
  179. )}
  180. {importers.map(importer => (
  181. <div
  182. className="modal-field spacer-bottom js-importer"
  183. data-key={importer.key}
  184. key={importer.key}>
  185. <label htmlFor={'create-profile-form-backup-' + importer.key}>
  186. {importer.name}
  187. </label>
  188. <input
  189. id={'create-profile-form-backup-' + importer.key}
  190. name={'backup_' + importer.key}
  191. type="file"
  192. />
  193. <p className="note">
  194. {translate('quality_profiles.optional_configuration_file')}
  195. </p>
  196. </div>
  197. ))}
  198. {/* drop me when we stop supporting ie11 */}
  199. <input name="hello-ie11" type="hidden" value="" />
  200. </div>
  201. )}
  202. <div className="modal-foot">
  203. {this.state.loading && <i className="spinner spacer-right" />}
  204. {!this.state.preloading && (
  205. <SubmitButton disabled={this.state.loading} id="create-profile-submit">
  206. {translate('create')}
  207. </SubmitButton>
  208. )}
  209. <ResetButtonLink id="create-profile-cancel" onClick={this.props.onClose}>
  210. {translate('cancel')}
  211. </ResetButtonLink>
  212. </div>
  213. </form>
  214. </Modal>
  215. );
  216. }
  217. }