store.js.es6 2.2 KB
Newer Older
1
/* global gl, Flash */
2
/* eslint-disable no-param-reassign, no-underscore-dangle */
Regis's avatar
Regis committed
3 4
/*= require vue_realtime_listener/index.js */

5
((gl) => {
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
  const pageValues = (headers) => {
    const normalizedHeaders = {};

    Object.keys(headers).forEach((e) => {
      normalizedHeaders[e.toUpperCase()] = headers[e];
    });

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

    return paginationInfo;
  };
Regis's avatar
Regis committed
24

25
  gl.PipelineStore = class {
26
    fetchDataLoop(Vue, pageNum, url, apiScope) {
27 28 29 30
      const updatePipelineNums = (count) => {
        const { all } = count;
        const running = count.running_or_pending;
        document.querySelector('.js-totalbuilds-count').innerHTML = all;
31 32 33
        document.querySelector('.js-running-count').innerHTML = running;
      };

34
      const goFetch = () =>
35
        this.$http.get(`${url}?scope=${apiScope}&page=${pageNum}`)
36
          .then((response) => {
Regis's avatar
Regis committed
37
            const pageInfo = pageValues(response.headers);
38
            this.pageInfo = Object.assign({}, this.pageInfo, pageInfo);
Regis's avatar
Regis committed
39

40
            const res = JSON.parse(response.body);
41 42
            this.count = Object.assign({}, this.count, res.count);
            this.pipelines = Object.assign([], this.pipelines, res.pipelines);
Regis's avatar
Regis committed
43

44
            updatePipelineNums(this.count);
45
            this.pageRequest = false;
46 47 48 49
          }, () => {
            this.pageRequest = false;
            return new Flash('Something went wrong on our end.');
          });
50

51 52
      goFetch();

53 54 55 56 57
      const startTimeLoops = () => {
        this.timeLoopInterval = setInterval(() => {
          this.$children
            .filter(e => e.$options._componentTag === 'time-ago')
            .forEach(e => e.changeTime());
58
        }, 10000);
59 60 61 62
      };

      startTimeLoops();

Regis's avatar
Regis committed
63 64
      const removeIntervals = () => clearInterval(this.timeLoopInterval);
      const startIntervals = () => startTimeLoops();
65

Regis's avatar
Regis committed
66
      gl.VueRealtimeListener(removeIntervals, startIntervals);
67
    }
68 69
  };
})(window.gl || (window.gl = {}));