summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/environments/stores/environments_store.js.es6
blob: 252e349962e1960c86567d1e4fcc64ad6e1ea463 (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
require('~/lib/utils/common_utils');
/**
 * Environments Store.
 *
 * Stores received environments, count of stopped environments and count of
 * available environments.
 */
class EnvironmentsStore {
  constructor() {
    this.state = {};
    this.state.environments = [];
    this.state.stoppedCounter = 0;
    this.state.availableCounter = 0;
    this.state.paginationInformation = {};

    return this;
  }

  /**
   *
   * Stores the received environments.
   *
   * Each environment has the following schema
   * { name: String, size: Number, latest: Object }
   *
   * If the `size` is bigger than 1, it means it should be rendered as a folder.
   * In those cases we add `isFolder` key in order to render it properly.
   *
   * @param  {Array} environments
   * @returns {Array}
   */
  storeEnvironments(environments = []) {
    const filteredEnvironments = environments.map((env) => {
      if (env.size > 1) {
        return Object.assign({}, env, { isFolder: true });
      }

      return env;
    });

    this.state.environments = filteredEnvironments;

    return filteredEnvironments;
  }

  storePagination(pagination = {}) {
    const normalizedHeaders = gl.utils.normalizeHeaders(pagination);
    const paginationInformation = {
      perPage: parseInt(normalizedHeaders['X-PER-PAGE'], 10),
      page: parseInt(normalizedHeaders['X-PAGE'], 10),
      total: parseInt(normalizedHeaders['X-TOTAL'], 10),
      totalPages: parseInt(normalizedHeaders['X-TOTAL-PAGES'], 10),
      nextPage: parseInt(normalizedHeaders['X-NEXT-PAGE'], 10),
      previousPage: parseInt(normalizedHeaders['X-PREV-PAGE'], 10),
    };

    this.state.paginationInformation = paginationInformation;
    return paginationInformation;
  }

  /**
   * Stores the number of available environments.
   *
   * @param  {Number} count = 0
   * @return {Number}
   */
  storeAvailableCount(count = 0) {
    this.state.availableCounter = count;
    return count;
  }

  /**
   * Stores the number of closed environments.
   *
   * @param  {Number} count = 0
   * @return {Number}
   */
  storeStoppedCount(count = 0) {
    this.state.stoppedCounter = count;
    return count;
  }
}

module.exports = EnvironmentsStore;