aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/code/reducers/index.js
blob: 3a86e706e8dd8b1c3d094b16e02c7a7030f27a3d (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
import _ from 'underscore';
import { combineReducers } from 'redux';

import { BROWSE, RECEIVE_COMPONENTS, SHOW_SOURCE } from '../actions';


export function fetching (state = false, action) {
  switch (action.type) {
    case BROWSE:
      return true;
    case RECEIVE_COMPONENTS:
      return false;
    default:
      return state;
  }
}


export function baseComponent (state = null, action) {
  switch (action.type) {
    case RECEIVE_COMPONENTS:
      return action.baseComponent;
    default:
      return state;
  }
}


export function components (state = null, action) {
  switch (action.type) {
    case RECEIVE_COMPONENTS:
      return action.components;
    default:
      return state;
  }
}


export function breadcrumbs (state = [], action) {
  switch (action.type) {
    case BROWSE:
      const existedIndex = state.findIndex(b => b.key === action.baseComponent.key);
      let nextBreadcrumbs;

      if (existedIndex === -1) {
        // browse deeper
        nextBreadcrumbs = [...state, action.baseComponent];
      } else {
        // use breadcrumbs
        nextBreadcrumbs = [...state.slice(0, existedIndex + 1)];
      }

      return nextBreadcrumbs;
    case SHOW_SOURCE:
      return [...state, action.component];
    default:
      return state;
  }
}


export function sourceViewer (state = null, action) {
  switch (action.type) {
    case BROWSE:
      return null;
    case SHOW_SOURCE:
      return action.component;
    default:
      return state;
  }
}


function selectCoverageMetric (component) {
  const coverage = _.findWhere(component.msr, { key: 'coverage' });
  const itCoverage = _.findWhere(component.msr, { key: 'it_coverage' });
  const overallCoverage = _.findWhere(component.msr, { key: 'overall_coverage' });

  if (coverage != null && itCoverage != null && overallCoverage != null) {
    return 'overall_coverage';
  } else if (coverage != null) {
    return 'coverage';
  } else {
    return 'it_coverage';
  }
}


export function coverageMetric (state = null, action) {
  switch (action.type) {
    case BROWSE:
      return state !== null ? state : selectCoverageMetric(action.baseComponent);
    default:
      return state;
  }
}


const rootReducer = combineReducers({
  fetching,
  baseComponent,
  components,
  breadcrumbs,
  sourceViewer,
  coverageMetric
});


export default rootReducer;