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.

BulkApplyTemplateModal.tsx 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 { toNotSoISOString } from 'sonar-ui-common/helpers/dates';
  22. import { translate, translateWithParameters } from 'sonar-ui-common/helpers/l10n';
  23. import { ResetButtonLink, SubmitButton } from 'sonar-ui-common/components/controls/buttons';
  24. import Modal from 'sonar-ui-common/components/controls/Modal';
  25. import { Alert } from 'sonar-ui-common/components/ui/Alert';
  26. import Select from 'sonar-ui-common/components/controls/Select';
  27. import { getPermissionTemplates, bulkApplyTemplate } from '../../api/permissions';
  28. export interface Props {
  29. analyzedBefore: Date | undefined;
  30. onClose: () => void;
  31. organization: string;
  32. provisioned: boolean;
  33. qualifier: string;
  34. query: string;
  35. selection: string[];
  36. total: number;
  37. }
  38. interface State {
  39. done: boolean;
  40. loading: boolean;
  41. permissionTemplate?: string;
  42. permissionTemplates?: T.PermissionTemplate[];
  43. submitting: boolean;
  44. }
  45. export default class BulkApplyTemplateModal extends React.PureComponent<Props, State> {
  46. mounted = false;
  47. state: State = { done: false, loading: true, submitting: false };
  48. componentDidMount() {
  49. this.mounted = true;
  50. this.loadPermissionTemplates();
  51. }
  52. componentWillUnmount() {
  53. this.mounted = false;
  54. }
  55. loadPermissionTemplates() {
  56. this.setState({ loading: true });
  57. getPermissionTemplates(this.props.organization).then(
  58. ({ permissionTemplates }) => {
  59. if (this.mounted) {
  60. this.setState({
  61. loading: false,
  62. permissionTemplate:
  63. permissionTemplates.length > 0 ? permissionTemplates[0].id : undefined,
  64. permissionTemplates
  65. });
  66. }
  67. },
  68. () => {
  69. if (this.mounted) {
  70. this.setState({ loading: false });
  71. }
  72. }
  73. );
  74. }
  75. handleConfirmClick = () => {
  76. const { analyzedBefore } = this.props;
  77. const { permissionTemplate } = this.state;
  78. if (permissionTemplate) {
  79. this.setState({ submitting: true });
  80. const parameters = this.props.selection.length
  81. ? {
  82. organization: this.props.organization,
  83. projects: this.props.selection.join(),
  84. qualifiers: this.props.qualifier,
  85. templateId: permissionTemplate
  86. }
  87. : {
  88. analyzedBefore: analyzedBefore && toNotSoISOString(analyzedBefore),
  89. onProvisionedOnly: this.props.provisioned || undefined,
  90. organization: this.props.organization,
  91. qualifiers: this.props.qualifier,
  92. q: this.props.query || undefined,
  93. templateId: permissionTemplate
  94. };
  95. bulkApplyTemplate(parameters).then(
  96. () => {
  97. if (this.mounted) {
  98. this.setState({ done: true, submitting: false });
  99. }
  100. },
  101. () => {
  102. if (this.mounted) {
  103. this.setState({ submitting: false });
  104. }
  105. }
  106. );
  107. }
  108. };
  109. handlePermissionTemplateChange = ({ value }: { value: string }) => {
  110. this.setState({ permissionTemplate: value });
  111. };
  112. renderWarning = () => (
  113. <Alert variant="warning">
  114. {this.props.selection.length
  115. ? translateWithParameters(
  116. 'permission_templates.bulk_apply_permission_template.apply_to_selected',
  117. this.props.selection.length
  118. )
  119. : translateWithParameters(
  120. 'permission_templates.bulk_apply_permission_template.apply_to_all',
  121. this.props.total
  122. )}
  123. </Alert>
  124. );
  125. renderSelect = () => (
  126. <div className="modal-field">
  127. <label>
  128. {translate('template')}
  129. <em className="mandatory">*</em>
  130. </label>
  131. <Select
  132. clearable={false}
  133. disabled={this.state.submitting}
  134. onChange={this.handlePermissionTemplateChange}
  135. options={this.state.permissionTemplates!.map(t => ({ label: t.name, value: t.id }))}
  136. value={this.state.permissionTemplate}
  137. />
  138. </div>
  139. );
  140. render() {
  141. const { done, loading, permissionTemplates, submitting } = this.state;
  142. const header = translate('permission_templates.bulk_apply_permission_template');
  143. return (
  144. <Modal contentLabel={header} onRequestClose={this.props.onClose} size="small">
  145. <header className="modal-head">
  146. <h2>{header}</h2>
  147. </header>
  148. <div className="modal-body">
  149. {done && (
  150. <Alert variant="success">{translate('projects_role.apply_template.success')}</Alert>
  151. )}
  152. {loading && <i className="spinner" />}
  153. {!loading && !done && permissionTemplates && this.renderWarning()}
  154. {!loading && !done && permissionTemplates && this.renderSelect()}
  155. </div>
  156. <footer className="modal-foot">
  157. {submitting && <i className="spinner spacer-right" />}
  158. {!loading && !done && permissionTemplates && (
  159. <SubmitButton disabled={submitting} onClick={this.handleConfirmClick}>
  160. {translate('apply')}
  161. </SubmitButton>
  162. )}
  163. <ResetButtonLink onClick={this.props.onClose}>
  164. {done ? translate('close') : translate('cancel')}
  165. </ResetButtonLink>
  166. </footer>
  167. </Modal>
  168. );
  169. }
  170. }