aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/projectsManagement/BulkApplyTemplateModal.tsx
blob: 86e8b4e1dce6d12af621dc331e94359eac84344c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * SonarQube
 * Copyright (C) 2009-2019 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
import * as React from 'react';
import { getPermissionTemplates, bulkApplyTemplate } from '../../api/permissions';
import { translate, translateWithParameters } from '../../helpers/l10n';
import Modal from '../../components/controls/Modal';
import Select from '../../components/controls/Select';
import { Button, ResetButtonLink } from '../../components/ui/buttons';
import { toNotSoISOString } from '../../helpers/dates';
import { Alert } from '../../components/ui/Alert';

export interface Props {
  analyzedBefore: Date | undefined;
  onClose: () => void;
  organization: string;
  provisioned: boolean;
  qualifier: string;
  query: string;
  selection: string[];
  total: number;
}

interface State {
  done: boolean;
  loading: boolean;
  permissionTemplate?: string;
  permissionTemplates?: T.PermissionTemplate[];
  submitting: boolean;
}

export default class BulkApplyTemplateModal extends React.PureComponent<Props, State> {
  mounted = false;
  state: State = { done: false, loading: true, submitting: false };

  componentDidMount() {
    this.mounted = true;
    this.loadPermissionTemplates();
  }

  componentWillUnmount() {
    this.mounted = false;
  }

  loadPermissionTemplates() {
    this.setState({ loading: true });
    getPermissionTemplates(this.props.organization).then(
      ({ permissionTemplates }) => {
        if (this.mounted) {
          this.setState({
            loading: false,
            permissionTemplate:
              permissionTemplates.length > 0 ? permissionTemplates[0].id : undefined,
            permissionTemplates
          });
        }
      },
      () => {
        if (this.mounted) {
          this.setState({ loading: false });
        }
      }
    );
  }

  handleConfirmClick = () => {
    const { analyzedBefore } = this.props;
    const { permissionTemplate } = this.state;
    if (permissionTemplate) {
      this.setState({ submitting: true });
      const parameters = this.props.selection.length
        ? {
            organization: this.props.organization,
            projects: this.props.selection.join(),
            templateId: permissionTemplate
          }
        : {
            analyzedBefore: analyzedBefore && toNotSoISOString(analyzedBefore),
            onProvisionedOnly: this.props.provisioned || undefined,
            organization: this.props.organization,
            qualifiers: this.props.qualifier,
            q: this.props.query || undefined,
            templateId: permissionTemplate
          };
      bulkApplyTemplate(parameters).then(
        () => {
          if (this.mounted) {
            this.setState({ done: true, submitting: false });
          }
        },
        () => {
          if (this.mounted) {
            this.setState({ submitting: false });
          }
        }
      );
    }
  };

  handlePermissionTemplateChange = ({ value }: { value: string }) => {
    this.setState({ permissionTemplate: value });
  };

  renderWarning = () => (
    <Alert variant="warning">
      {this.props.selection.length
        ? translateWithParameters(
            'permission_templates.bulk_apply_permission_template.apply_to_selected',
            this.props.selection.length
          )
        : translateWithParameters(
            'permission_templates.bulk_apply_permission_template.apply_to_all',
            this.props.total
          )}
    </Alert>
  );

  renderSelect = () => (
    <div className="modal-field">
      <label>
        {translate('template')}
        <em className="mandatory">*</em>
      </label>
      <Select
        clearable={false}
        disabled={this.state.submitting}
        onChange={this.handlePermissionTemplateChange}
        options={this.state.permissionTemplates!.map(t => ({ label: t.name, value: t.id }))}
        value={this.state.permissionTemplate}
      />
    </div>
  );

  render() {
    const { done, loading, permissionTemplates, submitting } = this.state;
    const header = translate('permission_templates.bulk_apply_permission_template');

    return (
      <Modal contentLabel={header} onRequestClose={this.props.onClose} size="small">
        <header className="modal-head">
          <h2>{header}</h2>
        </header>

        <div className="modal-body">
          {done && (
            <Alert variant="success">{translate('projects_role.apply_template.success')}</Alert>
          )}

          {loading && <i className="spinner" />}

          {!loading && !done && permissionTemplates && this.renderWarning()}
          {!loading && !done && permissionTemplates && this.renderSelect()}
        </div>

        <footer className="modal-foot">
          {submitting && <i className="spinner spacer-right" />}
          {!loading &&
            !done &&
            permissionTemplates && (
              <Button disabled={submitting} onClick={this.handleConfirmClick}>
                {translate('apply')}
              </Button>
            )}
          <ResetButtonLink className="js-modal-close" onClick={this.props.onClose}>
            {done ? translate('close') : translate('cancel')}
          </ResetButtonLink>
        </footer>
      </Modal>
    );
  }
}