summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/diff_notes/services/resolve.js
blob: 807ab11d292d76f48ff9bedfce3c6f23874397ae (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
/* 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 '../../vue_shared/vue_resource_interceptor';

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);
        }

        gl.mrWidget.checkStatus();
        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;