aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/project-admin/key/Key.js
blob: 4e95d344f26fa246e37c6465718459d132208554 (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
/*
 * 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.
 */
import React from 'react';
import shallowCompare from 'react-addons-shallow-compare';
import { connect } from 'react-redux';
import Header from './Header';
import UpdateForm from './UpdateForm';
import BulkUpdate from './BulkUpdate';
import FineGrainedUpdate from './FineGrainedUpdate';
import { fetchProjectModules, changeKey } from '../store/actions';
import { translate } from '../../../helpers/l10n';
import {
    addGlobalErrorMessage,
    closeAllGlobalMessages,
    addGlobalSuccessMessage
} from '../../../store/globalMessages/duck';
import { parseError } from '../../code/utils';
import { reloadUpdateKeyPage } from './utils';
import RecentHistory from '../../../app/components/nav/component/RecentHistory';
import { getProjectAdminProjectModules, getComponent } from '../../../store/rootReducer';

class Key extends React.Component {
  static propTypes = {
    component: React.PropTypes.object.isRequired,
    fetchProjectModules: React.PropTypes.func.isRequired,
    changeKey: React.PropTypes.func.isRequired,
    addGlobalErrorMessage: React.PropTypes.func.isRequired,
    addGlobalSuccessMessage: React.PropTypes.func.isRequired,
    closeAllGlobalMessages: React.PropTypes.func.isRequired
  };

  state = {
    tab: 'bulk'
  };

  componentDidMount () {
    this.props.fetchProjectModules(this.props.component.key);
  }

  shouldComponentUpdate (nextProps, nextState) {
    return shallowCompare(this, nextProps, nextState);
  }

  handleChangeKey (key, newKey) {
    return this.props.changeKey(key, newKey).then(() => {
      if (key === this.props.component.key) {
        this.props.addGlobalSuccessMessage(
            translate('update_key.key_updated.reload'));
        RecentHistory.remove(key);
        reloadUpdateKeyPage(newKey);
      } else {
        this.props.addGlobalSuccessMessage(
            translate('update_key.key_updated'));
      }
    }).catch(e => {
      parseError(e).then(this.props.addGlobalErrorMessage);
    });
  }

  handleChangeTab (tab, e) {
    e.preventDefault();
    e.target.blur();
    this.setState({ tab });
    this.props.closeAllGlobalMessages();
  }

  render () {
    const { component, modules } = this.props;

    const noModules = modules != null && modules.length === 0;
    const hasModules = modules != null && modules.length > 0;

    const { tab } = this.state;

    return (
        <div id="project-key" className="page page-limited">
          <Header/>

          {modules == null && (
              <i className="spinner"/>
          )}

          {noModules && (
              <div>
                <UpdateForm
                    component={component}
                    onKeyChange={this.handleChangeKey.bind(this)}/>
              </div>
          )}

          {hasModules && (
              <div>
                <div className="big-spacer-bottom">
                  <ul className="tabs">
                    <li>
                      <a id="update-key-tab-bulk"
                         className={tab === 'bulk' ? 'selected' : ''}
                         href="#"
                         onClick={this.handleChangeTab.bind(this, 'bulk')}>
                        {translate('update_key.bulk_update')}
                      </a>
                    </li>
                    <li>
                      <a id="update-key-tab-fine"
                         className={tab === 'fine' ? 'selected' : ''}
                         href="#"
                         onClick={this.handleChangeTab.bind(this, 'fine')}>
                        {translate('update_key.fine_grained_key_update')}
                      </a>
                    </li>
                  </ul>
                </div>

                {tab === 'bulk' && (
                    <BulkUpdate component={component}/>
                )}

                {tab === 'fine' && (
                    <FineGrainedUpdate
                        component={component}
                        modules={modules}
                        onKeyChange={this.handleChangeKey.bind(this)}
                        onSuccess={this.props.closeAllGlobalMessages}
                        onError={this.props.addGlobalErrorMessage}/>
                )}
              </div>
          )}
        </div>
    );
  }
}

const mapStateToProps = (state, ownProps) => ({
  component: getComponent(state, ownProps.location.query.id),
  modules: getProjectAdminProjectModules(state, ownProps.location.query.id)
});

export default connect(
    mapStateToProps, {
      fetchProjectModules,
      changeKey,
      addGlobalErrorMessage,
      addGlobalSuccessMessage,
      closeAllGlobalMessages
    }
)(Key);