summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/diff_notes/services/resolve.js
blob: 0687028ca54208aaa95200a91cdccf12797a4926 (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
/* global CommentsStore */

import Vue from 'vue';
import Flash from '../../flash';
import '../../vue_shared/vue_resource_interceptor';
import { __ } from '~/locale';

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

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

  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(resp => resp.json())
      .then(data => {
        discussion.loading = false;
        const resolvedBy = data ? data.resolved_by : null;

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

        if (gl.mrWidget) gl.mrWidget.checkStatus();
        discussion.updateHeadline(data);
      })
      .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;