summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_pipelines_index/pipelines.js
blob: 2286ee250629c5bf19a68f2b440aa5cf2ff4c9c7 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* global Vue, gl, Flash */
/* eslint-disable no-param-reassign, no-new */
import '~/flash';
import CommitPipelinesStoreWithTimeAgo from '../commit/pipelines/pipelines_store';
import PipelinesService from './services/pipelines_service';
import PipelinesStore from './stores/pipelines_store';

window.Vue = require('vue');
require('../vue_shared/components/table_pagination');
require('../vue_shared/components/pipelines_table');

((gl) => {
  gl.VuePipelines = Vue.extend({

    props: {
      endpoint: {
        type: String,
        required: true,
      },
    },

    components: {
      'gl-pagination': gl.VueGlPagination,
      'pipelines-table-component': gl.pipelines.PipelinesTableComponent,
    },

    data() {
      const store = new PipelinesStore();

      return {
        store,
        state: store.state,
        apiScope: 'all',
        pagenum: 1,
        pageRequest: false,
      };
    },

    created() {
      this.service = new PipelinesService(this.endpoint);

      this.fetchPipelines();
    },

    beforeUpdate() {
      if (this.state.pipelines.length && this.$children) {
        CommitPipelinesStoreWithTimeAgo.startTimeAgoLoops.call(this, Vue);
      }
    },

    methods: {
      /**
       * Will change the page number and update the URL.
       *
       * @param  {Number} pageNumber desired page to go to.
       */
      change(pageNumber) {
        const param = gl.utils.setParamInURL('page', pageNumber);

        gl.utils.visitUrl(param);
        return param;
      },

      fetchPipelines() {
        const pageNumber = gl.utils.getParameterByName('page') || this.pagenum;
        const scope = gl.utils.getParameterByName('scope') || this.apiScope;

        this.pageRequest = true;
        return this.service.getPipelines(scope, pageNumber)
          .then(resp => ({
            headers: resp.headers,
            body: resp.json(),
          }))
          .then((response) => {
            this.store.storeCount(response.body.count);
            this.store.storePipelines(response.body.pipelines);
            this.store.storePagination(response.headers);
          })
          .then(() => {
            this.pageRequest = false;
          })
          .catch(() => {
            this.pageRequest = false;
            new Flash('An error occurred while fetching the pipelines, please reload the page again.');
          });
      },
    },
    template: `
      <div>
        <div class="pipelines realtime-loading" v-if="pageRequest">
          <i class="fa fa-spinner fa-spin" aria-hidden="true"></i>
        </div>

        <div class="blank-state blank-state-no-icon"
          v-if="!pageRequest && state.pipelines.length === 0">
          <h2 class="blank-state-title js-blank-state-title">
            No pipelines to show
          </h2>
        </div>

        <div class="table-holder" v-if="!pageRequest && state.pipelines.length">
          <pipelines-table-component
            :pipelines="state.pipelines"
            :service="service"/>
        </div>

        <gl-pagination
          v-if="!pageRequest && state.pipelines.length && state.pageInfo.total > state.pageInfo.perPage"
          :pagenum="pagenum"
          :change="change"
          :count="state.count.all"
          :pageInfo="state.pageInfo"
        >
        </gl-pagination>
      </div>
    `,
  });
})(window.gl || (window.gl = {}));