aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/tutorials/onboarding/OrganizationStep.js
blob: 116b4c7d8c40c7d981eda897938214c9dcfdfaef (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * SonarQube
 * Copyright (C) 2009-2017 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.
 */
// @flow
import React from 'react';
import Select from 'react-select';
import classNames from 'classnames';
import { sortBy } from 'lodash';
import Step from './Step';
import NewOrganizationForm from './NewOrganizationForm';
import { getMyOrganizations } from '../../../api/organizations';
import { translate } from '../../../helpers/l10n';

/*::
type Props = {|
  currentUser: { login: string, isLoggedIn: boolean },
  finished: boolean,
  onOpen: () => void,
  onContinue: (organization: string) => void,
  open: boolean,
  stepNumber: number
|};
*/

/*::
type State = {
  loading: boolean,
  newOrganization?: string,
  existingOrganization?: string,
  existingOrganizations: Array<string>,
  personalOrganization?: string,
  selection: 'personal' | 'existing' | 'new'
};
*/

export default class OrganizationStep extends React.PureComponent {
  /*:: mounted: boolean; */
  /*:: props: Props; */
  state /*: State */ = {
    loading: true,
    existingOrganizations: [],
    selection: 'personal'
  };

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

  componentWillUnmount() {
    this.mounted = false;
  }

  fetchOrganizations = () => {
    getMyOrganizations().then(
      organizations => {
        if (this.mounted) {
          // best guess: if there is only one organization, then it is personal
          // otherwise, we can't guess, let's display them all as just "existing organizations"
          const personalOrganization = organizations.length === 1 ? organizations[0] : undefined;
          const existingOrganizations = organizations.length > 1 ? sortBy(organizations) : [];
          const selection = personalOrganization
            ? 'personal'
            : existingOrganizations.length > 0 ? 'existing' : 'new';
          this.setState({
            loading: false,
            existingOrganizations,
            personalOrganization,
            selection
          });
        }
      },
      () => {
        if (this.mounted) {
          this.setState({ loading: false });
        }
      }
    );
  };

  getSelectedOrganization = () => {
    switch (this.state.selection) {
      case 'personal':
        return this.state.personalOrganization;
      case 'existing':
        return this.state.existingOrganization;
      case 'new':
        return this.state.newOrganization;
      default:
        return null;
    }
  };

  handlePersonalClick = (event /*: Event */) => {
    event.preventDefault();
    this.setState({ selection: 'personal' });
  };

  handleExistingClick = (event /*: Event */) => {
    event.preventDefault();
    this.setState({ selection: 'existing' });
  };

  handleNewClick = (event /*: Event */) => {
    event.preventDefault();
    this.setState({ selection: 'new' });
  };

  handleOrganizationCreate = (newOrganization /*: string */) => {
    this.setState({ newOrganization });
  };

  handleOrganizationDelete = () => {
    this.setState({ newOrganization: undefined });
  };

  handleExistingOrganizationSelect = ({ value } /*: { value: string } */) => {
    this.setState({ existingOrganization: value });
  };

  handleContinueClick = (event /*: Event */) => {
    event.preventDefault();
    const organization = this.getSelectedOrganization();
    if (organization) {
      this.props.onContinue(organization);
    }
  };

  renderPersonalOrganizationOption = () => (
    <div>
      <a className="link-base-color link-no-underline" href="#" onClick={this.handlePersonalClick}>
        <i
          className={classNames('icon-radio', 'spacer-right', {
            'is-checked': this.state.selection === 'personal'
          })}
        />
        {translate('onboarding.organization.my_personal_organization')}
        <span className="note spacer-left">{this.state.personalOrganization}</span>
      </a>
    </div>
  );

  renderExistingOrganizationOption = () => (
    <div className="big-spacer-top">
      <a
        className="js-existing link-base-color link-no-underline"
        href="#"
        onClick={this.handleExistingClick}>
        <i
          className={classNames('icon-radio', 'spacer-right', {
            'is-checked': this.state.selection === 'existing'
          })}
        />
        {translate('onboarding.organization.exising_organization')}
      </a>
      {this.state.selection === 'existing' && (
        <div className="big-spacer-top">
          <Select
            className="input-super-large"
            clearable={false}
            onChange={this.handleExistingOrganizationSelect}
            options={this.state.existingOrganizations.map(organization => ({
              label: organization,
              value: organization
            }))}
            value={this.state.existingOrganization}
          />
        </div>
      )}
    </div>
  );

  renderNewOrganizationOption = () => (
    <div className="big-spacer-top">
      <a
        className="js-new link-base-color link-no-underline"
        href="#"
        onClick={this.handleNewClick}>
        <i
          className={classNames('icon-radio', 'spacer-right', {
            'is-checked': this.state.selection === 'new'
          })}
        />
        {translate('onboarding.organization.create_another_organization')}
      </a>
      {this.state.selection === 'new' && (
        <div className="big-spacer-top">
          <NewOrganizationForm
            onDelete={this.handleOrganizationDelete}
            onDone={this.handleOrganizationCreate}
            organization={this.state.newOrganization}
          />
        </div>
      )}
    </div>
  );

  renderForm = () => {
    return (
      <div className="boxed-group-inner">
        <div className="big-spacer-bottom width-50">
          {translate('onboarding.organization.text')}
        </div>

        {this.state.loading ? (
          <i className="spinner" />
        ) : (
          <div>
            {this.state.personalOrganization && this.renderPersonalOrganizationOption()}
            {this.state.existingOrganizations.length > 0 && this.renderExistingOrganizationOption()}
            {this.renderNewOrganizationOption()}
          </div>
        )}

        {this.getSelectedOrganization() != null &&
        !this.state.loading && (
          <div className="big-spacer-top">
            <button className="js-continue" onClick={this.handleContinueClick}>
              {translate('continue')}
            </button>
          </div>
        )}
      </div>
    );
  };

  renderResult = () => {
    const result = this.getSelectedOrganization();

    return result != null ? (
      <div className="boxed-group-actions">
        <i className="icon-check spacer-right" />
        <strong>{result}</strong>
      </div>
    ) : null;
  };

  render() {
    return (
      <Step
        finished={this.props.finished}
        onOpen={this.props.onOpen}
        open={this.props.open}
        renderForm={this.renderForm}
        renderResult={this.renderResult}
        stepNumber={this.props.stepNumber}
        stepTitle={translate('onboarding.organization.header')}
      />
    );
  }
}