aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/quality-profiles/compare/ComparisonContainer.js
blob: 03d5d1452125cdcecd47dcb02835d1063ba467a4 (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
/*
 * SonarQube
 * Copyright (C) 2009-2016 SonarSource SA
 * mailto:contact 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 ComparisonForm from './ComparisonForm';
import ComparisonResults from './ComparisonResults';
import { ProfileType, ProfilesListType } from '../propTypes';
import { compareProfiles } from '../../../api/quality-profiles';

export default class ComparisonContainer extends React.Component {
  static propTypes = {
    profile: ProfileType,
    profiles: ProfilesListType
  };

  static contextTypes = {
    router: React.PropTypes.object
  };

  state = {
    loading: false
  };

  componentWillMount () {
    this.handleCompare = this.handleCompare.bind(this);
  }

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

  componentDidUpdate (prevProps) {
    if (prevProps.profile !== this.props.profile ||
        prevProps.location !== this.props.location) {
      this.loadResults();
    }
  }

  componentWillUnmount () {
    this.mounted = false;
  }

  loadResults () {
    const { withKey } = this.props.location.query;
    if (!withKey) {
      this.setState({ left: null, loading: false });
      return;
    }

    this.setState({ loading: true });
    compareProfiles(this.props.profile.key, withKey).then(r => {
      if (this.mounted) {
        this.setState({
          left: r.left,
          right: r.right,
          inLeft: r.inLeft,
          inRight: r.inRight,
          modified: r.modified,
          loading: false
        });
      }
    });
  }

  handleCompare (withKey) {
    this.context.router.push({
      pathname: '/compare',
      query: {
        key: this.props.profile.key,
        withKey
      }
    });
  }

  render () {
    const { profile, profiles, location } = this.props;
    const { withKey } = location.query;
    const { left, right, inLeft, inRight, modified } = this.state;

    return (
        <div className="quality-profile-box js-profile-comparison">
          <header className="spacer-bottom">
            <ComparisonForm
                withKey={withKey}
                profile={profile}
                profiles={profiles}
                onCompare={this.handleCompare}/>

            {this.state.loading && (
                <i className="spinner spacer-left"/>
            )}
          </header>

          {left != null && (
              <ComparisonResults
                  left={left}
                  right={right}
                  inLeft={inLeft}
                  inRight={inRight}
                  modified={modified}/>
          )}
        </div>
    );
  }
}