BigW Consortium Gitlab

environments_store.js.es6 3.83 KB
Newer Older
Filipa Lacerda committed
1
/* eslint-disable no-param-reassign */
2 3 4
(() => {
  window.gl = window.gl || {};
  window.gl.environmentsList = window.gl.environmentsList || {};
5

6
  gl.environmentsList.EnvironmentsStore = {
7
    state: {},
8

9
    create() {
10
      this.state.environments = [];
11 12
      this.state.stoppedCounter = 0;
      this.state.availableCounter = 0;
Filipa Lacerda committed
13 14

      return this;
15
    },
16

17
    /**
18 19
     * In order to display a tree view we need to modify the received
     * data in to a tree structure based on `environment_type`
20
     * sorted alphabetically.
21
     * In each children a `vue-` property will be added. This property will be
22
     * used to know if an item is a children mostly for css purposes. This is
23 24
     * needed because the children row is a fragment instance and therfore does
     * not accept non-prop attributes.
25 26
     *
     *
27 28 29 30 31 32 33 34 35
     * @example
     * it will transform this:
     * [
     *   { name: "environment", environment_type: "review" },
     *   { name: "environment_1", environment_type: null }
     *   { name: "environment_2, environment_type: "review" }
     * ]
     * into this:
     * [
36
     *   { name: "review", children:
37
     *      [
38 39
     *        { name: "environment", environment_type: "review", vue-isChildren: true},
     *        { name: "environment_2", environment_type: "review", vue-isChildren: true}
40 41 42 43
     *      ]
     *   },
     *  {name: "environment_1", environment_type: null}
     * ]
44 45
     *
     *
Filipa Lacerda committed
46 47
     * @param  {Array} environments List of environments.
     * @returns {Array} Tree structured array with the received environments.
48
     */
Filipa Lacerda committed
49
    storeEnvironments(environments = []) {
50 51 52
      this.state.stoppedCounter = this.countByState(environments, 'stopped');
      this.state.availableCounter = this.countByState(environments, 'available');

Filipa Lacerda committed
53
      const environmentsTree = environments.reduce((acc, environment) => {
54
        if (environment.environment_type !== null) {
55 56
          const occurs = acc.filter(element => element.children &&
             element.name === environment.environment_type);
57

58
          environment['vue-isChildren'] = true;
59

60 61
          if (occurs.length) {
            acc[acc.indexOf(occurs[0])].children.push(environment);
62
            acc[acc.indexOf(occurs[0])].children.sort(this.sortByName);
63 64 65
          } else {
            acc.push({
              name: environment.environment_type,
66
              children: [environment],
67 68
              isOpen: false,
              'vue-isChildren': environment['vue-isChildren'],
69 70 71
            });
          }
        } else {
Filipa Lacerda committed
72
          acc.push(environment);
73 74 75
        }

        return acc;
76
      }, []).sort(this.sortByName);
77

Filipa Lacerda committed
78
      this.state.environments = environmentsTree;
79

Filipa Lacerda committed
80
      return environmentsTree;
81
    },
82

83 84 85 86 87 88 89 90 91
    /**
     * Toggles folder open property given the environment type.
     *
     * @param  {String} envType
     * @return {Array}
     */
    toggleFolder(envType) {
      const environments = this.state.environments;

Filipa Lacerda committed
92 93
      const environmentsCopy = environments.map((env) => {
        if (env['vue-isChildren'] && env.name === envType) {
94 95 96 97 98 99
          env.isOpen = !env.isOpen;
        }

        return env;
      });

Filipa Lacerda committed
100
      this.state.environments = environmentsCopy;
101

Filipa Lacerda committed
102
      return environmentsCopy;
103 104
    },

105
    /**
106
     * Given an array of environments, returns the number of environments
107 108
     * that have the given state.
     *
109
     * @param  {Array} environments
110 111 112 113
     * @param  {String} state
     * @returns {Number}
     */
    countByState(environments, state) {
114
      return environments.filter(env => env.state === state).length;
115 116
    },

117 118 119 120 121 122 123
    /**
     * Sorts the two objects provided by their name.
     *
     * @param  {Object} a
     * @param  {Object} b
     * @returns {Number}
     */
124
    sortByName(a, b) {
125 126
      const nameA = a.name.toUpperCase();
      const nameB = b.name.toUpperCase();
127

128
      return nameA < nameB ? -1 : nameA > nameB ? 1 : 0; // eslint-disable-line
129 130
    },
  };
131
})();