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.

ChangeParentForm.tsx 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 { Profile } from '../types';
  27. import { changeProfileParent } from '../../../api/quality-profiles';
  28. interface Props {
  29. onChange: () => void;
  30. onClose: () => void;
  31. profile: Profile;
  32. profiles: Profile[];
  33. }
  34. interface State {
  35. loading: boolean;
  36. selected: string | null;
  37. }
  38. export default class ChangeParentForm extends React.PureComponent<Props, State> {
  39. mounted = false;
  40. state: State = {
  41. loading: false,
  42. selected: null
  43. };
  44. componentDidMount() {
  45. this.mounted = true;
  46. }
  47. componentWillUnmount() {
  48. this.mounted = false;
  49. }
  50. handleSelectChange = (option: { value: string }) => {
  51. this.setState({ selected: option.value });
  52. };
  53. handleFormSubmit = (event: React.SyntheticEvent<HTMLFormElement>) => {
  54. event.preventDefault();
  55. const parent = this.state.selected;
  56. if (parent != null) {
  57. this.setState({ loading: true });
  58. changeProfileParent(this.props.profile.key, parent)
  59. .then(this.props.onChange)
  60. .catch(() => {
  61. if (this.mounted) {
  62. this.setState({ loading: false });
  63. }
  64. });
  65. }
  66. };
  67. render() {
  68. const { profiles } = this.props;
  69. const options = [
  70. { label: translate('none'), value: '' },
  71. ...sortBy(profiles, 'name').map(profile => ({
  72. label: profile.isBuiltIn
  73. ? `${profile.name} (${translate('quality_profiles.built_in')})`
  74. : profile.name,
  75. value: profile.key
  76. }))
  77. ];
  78. const submitDisabled =
  79. this.state.loading ||
  80. this.state.selected == null ||
  81. this.state.selected === this.props.profile.parentKey;
  82. return (
  83. <Modal
  84. contentLabel={translate('quality_profiles.change_parent')}
  85. onRequestClose={this.props.onClose}
  86. size="small">
  87. <form id="change-profile-parent-form" onSubmit={this.handleFormSubmit}>
  88. <div className="modal-head">
  89. <h2>{translate('quality_profiles.change_parent')}</h2>
  90. </div>
  91. <div className="modal-body">
  92. <div className="modal-field">
  93. <label htmlFor="change-profile-parent">
  94. {translate('quality_profiles.parent')} <em className="mandatory">*</em>
  95. </label>
  96. <Select
  97. clearable={false}
  98. id="change-profile-parent"
  99. name="parentKey"
  100. onChange={this.handleSelectChange}
  101. options={options}
  102. value={
  103. this.state.selected != null
  104. ? this.state.selected
  105. : this.props.profile.parentKey || ''
  106. }
  107. />
  108. </div>
  109. </div>
  110. <div className="modal-foot">
  111. {this.state.loading && <i className="spinner spacer-right" />}
  112. <SubmitButton disabled={submitDisabled} id="change-profile-parent-submit">
  113. {translate('change_verb')}
  114. </SubmitButton>
  115. <ResetButtonLink id="change-profile-parent-cancel" onClick={this.props.onClose}>
  116. {translate('cancel')}
  117. </ResetButtonLink>
  118. </div>
  119. </form>
  120. </Modal>
  121. );
  122. }
  123. }