summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/merge_request_widget/approvals/approvals_api.js.es6
blob: 4f181e02f3939afb356e76f38bfaccb02ef36b3a (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
/* global Vue, Flash */
//= require ./approvals_store

(() => {
  class ApprovalsApi {
    constructor(endpoint) {
      gl.ApprovalsApi = this;
      this.init(endpoint);
    }

    init(mergeRequestEndpoint) {
      this.baseEndpoint = `${mergeRequestEndpoint}/approvals`;
      Vue.http.headers.common['X-CSRF-Token'] = $.rails.csrfToken();
    }

    fetchApprovals() {
      const flashErrorMessage = 'An error occured while retrieving approval data for this merge request.';

      return Vue.http.get(this.baseEndpoint).catch(() => new Flash(flashErrorMessage));
    }

    approveMergeRequest() {
      const flashErrorMessage = 'An error occured while submitting your approval.';

      return Vue.http.post(this.baseEndpoint).catch(() => new Flash(flashErrorMessage));
    }

    unapproveMergeRequest() {
      const flashErrorMessage = 'An error occured while removing your approval.';

      return Vue.http.delete(this.baseEndpoint).catch(() => new Flash(flashErrorMessage));
    }
  }

  gl.ApprovalsApi = ApprovalsApi;
})();