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

((gl) => {
  const pageValues = headers => ({
    perPage: +headers['X-Per-Page'],
    page: +headers['X-Page'],
    total: +headers['X-Total'],
    totalPages: +headers['X-Total-Pages'],
    nextPage: +headers['X-Next-Page'],
    previousPage: +headers['X-Prev-Page'],
  });

  gl.PipelineStore = class {
    fetchDataLoop(Vue, pageNum, url, apiScope) {
      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('Something went wrong on our end.');
          });

      goFetch();

      const startTimeLoops = () => {
        this.timeLoopInterval = setInterval(() => {
          this.$children
            .filter(e => e.$options._componentTag === 'time-ago')
            .forEach(e => e.changeTime());
        }, 10000);
      };

      startTimeLoops();

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

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