summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/environments/stores/environmnets_store.js.es6
blob: 8b5cb67ed37ed9ef1fd5ccc16019f90769a3f1e1 (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
(() => {
  window.gl = window.gl || {};
  window.gl.environmentsList = window.gl.environmentsList || {};
  
  gl.environmentsList.EnvironmentsStore = {
    state: {},
    
    create () {
      this.state.environments = [];
    },
    
    /**
     * In order to display a tree view we need to modify the received 
     * data in to a tree structure based on `environment_type` 
     * sorted alphabetically.
     * In each children a `vue-` property will be added. This property will be
     * used to know if an item is a children mostly for css purposes. This is 
     * needed because the children row is a fragment instance and therfore does
     * not accept non-prop attributes.
     * 
     * 
     * @example
     * it will transform this:
     * [
     *   { name: "environment", environment_type: "review" },
     *   { name: "environment_1", environment_type: null }
     *   { name: "environment_2, environment_type: "review" }
     * ]
     * into this:
     * [
     *   { name: "review", children: 
     *      [
     *        { name: "environment", environment_type: "review", vue-isChildren: true},
     *        { name: "environment_2", environment_type: "review", vue-isChildren: true}
     *      ]
     *   },
     *  {name: "environment_1", environment_type: null}
     * ]
     * 
     * 
     * @param  {Array} environments List of environments.
     * @returns {Array} Tree structured array with the received environments.
     */
    storeEnvironments(environments) {
      const environmentsTree = environments.reduce((acc, environment) => {
        const data = Object.assign({}, environment);
        
        if (data.last_deployment) {

          //humanizes actions names if there are any actions
          if (data.last_deployment.manual_actions) {
            data.last_deployment.manual_actions = data.last_deployment.manual_actions.map((action) => Object.assign({}, action, {name: gl.text.humanize(action.name)}));
          }
          
          //transforms created date for deployment in a human readable format
          if (data.last_deployment.created_at) {
            // TODO - how to do this without jquery
          }
        }
        
        if (environment.environment_type !== null) {
          const occurs = acc.find((element, index, array) => {
            return element.environment_type === environment.environment_type;
          });
          
          data["vue-isChildren"] = true;

          if (occurs !== undefined) {
            acc[acc.indexOf(occurs)].children.push(data);
            acc[acc.indexOf(occurs)].children.push(data).sort(this.sortByName)
          } else {
            acc.push({
              name: environment.environment_type,
              children: [
                Object.assign(data)
              ]
            });
          }
        } else {
          acc.push(data);
        }

        return acc;
      }, []).sort(this.sortByName);
    
      this.state.environments = environmentsTree;
      
      return environmentsTree;
    },
    
    sortByName (a,b) {
      const nameA = a.name.toUpperCase();
      const nameB = b.name.toUpperCase();
      
      if (nameA < nameB) {
        return -1;
      }
      
      if (nameA > nameB) {
        return 1;
      }

      return 0;
    }
  }
})();