summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/diff_notes/services/resolve.js
blob: 4ea6ba8a73d8b6f1284c04bb1017e7c64aacf684 (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
/* eslint-disable class-methods-use-this, one-var, camelcase, no-new, comma-dangle, no-param-reassign, max-len */
/* global Flash */
/* global CommentsStore */

import Vue from 'vue';
import VueResource from 'vue-resource';

require('../../vue_shared/vue_resource_interceptor');

Vue.use(VueResource);

window.gl = window.gl || {};

class ResolveServiceClass {
  constructor(root) {
    this.noteResource = Vue.resource(`${root}/notes{/noteId}/resolve`);
    this.discussionResource = Vue.resource(`${root}/merge_requests{/mergeRequestId}/discussions{/discussionId}/resolve`);
  }

  resolve(noteId) {
    return this.noteResource.save({ noteId }, {});
  }

  unresolve(noteId) {
    return this.noteResource.delete({ noteId }, {});
  }

  toggleResolveForDiscussion(mergeRequestId, discussionId) {
    const discussion = CommentsStore.state[discussionId];
    const isResolved = discussion.isResolved();
    let promise;

    if (isResolved) {
      promise = this.unResolveAll(mergeRequestId, discussionId);
    } else {
      promise = this.resolveAll(mergeRequestId, discussionId);
    }

    promise.then((response) => {
      discussion.loading = false;

      if (response.status === 200) {
        const data = response.json();
        const resolved_by = data ? data.resolved_by : null;

        if (isResolved) {
          discussion.unResolveAllNotes();
        } else {
          discussion.resolveAllNotes(resolved_by);
        }

        discussion.updateHeadline(data);
      } else {
        throw new Error('An error occurred when trying to resolve discussion.');
      }
    }).catch(() => {
      new Flash('An error occurred when trying to resolve a discussion. Please try again.');
    });
  }

  resolveAll(mergeRequestId, discussionId) {
    const discussion = CommentsStore.state[discussionId];

    discussion.loading = true;

    return this.discussionResource.save({
      mergeRequestId,
      discussionId
    }, {});
  }

  unResolveAll(mergeRequestId, discussionId) {
    const discussion = CommentsStore.state[discussionId];

    discussion.loading = true;

    return this.discussionResource.delete({
      mergeRequestId,
      discussionId
    }, {});
  }
}

gl.DiffNotesResolveServiceClass = ResolveServiceClass;