summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_pipelines_index/store.js.es6
blob: 0ee21f00fdc7d2ddd28cba99971ca7ef9c77f578 (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
/* global gl, Flash */
/* eslint-disable no-param-reassign, no-underscore-dangle */
require('../vue_realtime_listener');

((gl) => {
  const pageValues = (headers) => {
    const normalized = gl.utils.normalizeHeaders(headers);

    const paginationInfo = {
      perPage: +normalized['X-PER-PAGE'],
      page: +normalized['X-PAGE'],
      total: +normalized['X-TOTAL'],
      totalPages: +normalized['X-TOTAL-PAGES'],
      nextPage: +normalized['X-NEXT-PAGE'],
      previousPage: +normalized['X-PREV-PAGE'],
    };

    return paginationInfo;
  };

  gl.PipelineStore = class {
    fetchDataLoop(Vue, pageNum, url, apiScope) {
      this.pageRequest = true;
      const updatePipelineNums = (count) => {
        const { all } = count;
        const running = count.running_or_pending;
        document.querySelector('.js-totalbuilds-count').innerHTML = all;
        document.querySelector('.js-running-count').innerHTML = running;
      };

      const goFetch = () =>
        this.$http.get(`${url}?scope=${apiScope}&page=${pageNum}`)
          .then((response) => {
            const pageInfo = pageValues(response.headers);
            this.pageInfo = Object.assign({}, this.pageInfo, pageInfo);

            const res = JSON.parse(response.body);
            this.count = Object.assign({}, this.count, res.count);
            this.pipelines = Object.assign([], this.pipelines, res.pipelines);

            updatePipelineNums(this.count);
            this.pageRequest = false;
          }, () => {
            this.pageRequest = false;
            return new Flash('An error occurred while fetching the pipelines, please reload the page again.');
          });

      goFetch();

      const startTimeLoops = () => {
        this.timeLoopInterval = setInterval(() => {
          this.$children[0].$children.reduce((acc, component) => {
            const timeAgoComponent = component.$children.filter(el => el.$options._componentTag === 'time-ago')[0];
            acc.push(timeAgoComponent);
            return acc;
          }, []).forEach(e => e.changeTime());
        }, 10000);
      };

      startTimeLoops();

      const removeIntervals = () => clearInterval(this.timeLoopInterval);
      const startIntervals = () => startTimeLoops();

      gl.VueRealtimeListener(removeIntervals, startIntervals);
    }
  };
})(window.gl || (window.gl = {}));