resolve.js 1.9 KB
Newer Older
1 2
/* global CommentsStore */

M
Mike Greiling 已提交
3
import Vue from 'vue';
P
Phil Hughes 已提交
4
import Flash from '../../flash';
5
import '../../vue_shared/vue_resource_interceptor';
M
Mike Greiling 已提交
6

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

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

15 16 17
  resolve(noteId) {
    return this.noteResource.save({ noteId }, {});
  }
18

19 20 21
  unresolve(noteId) {
    return this.noteResource.delete({ noteId }, {});
  }
22

23 24 25 26
  toggleResolveForDiscussion(mergeRequestId, discussionId) {
    const discussion = CommentsStore.state[discussionId];
    const isResolved = discussion.isResolved();
    let promise;
27

28 29 30 31 32
    if (isResolved) {
      promise = this.unResolveAll(mergeRequestId, discussionId);
    } else {
      promise = this.resolveAll(mergeRequestId, discussionId);
    }
33

F
Filipa Lacerda 已提交
34 35 36 37 38
    promise
      .then(resp => resp.json())
      .then((data) => {
        discussion.loading = false;
        const resolvedBy = data ? data.resolved_by : null;
39

40 41
        if (isResolved) {
          discussion.unResolveAllNotes();
42
        } else {
F
Filipa Lacerda 已提交
43
          discussion.resolveAllNotes(resolvedBy);
44
        }
45

46
        if (gl.mrWidget) gl.mrWidget.checkStatus();
47
        discussion.updateHeadline(data);
F
Filipa Lacerda 已提交
48 49
      })
      .catch(() => new Flash('An error occurred when trying to resolve a discussion. Please try again.'));
50
  }
P
Phil Hughes 已提交
51

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

55 56 57 58
    discussion.loading = true;

    return this.discussionResource.save({
      mergeRequestId,
F
Filipa Lacerda 已提交
59
      discussionId,
60 61
    }, {});
  }
62

63 64
  unResolveAll(mergeRequestId, discussionId) {
    const discussion = CommentsStore.state[discussionId];
P
Phil Hughes 已提交
65

66
    discussion.loading = true;
67

68 69
    return this.discussionResource.delete({
      mergeRequestId,
F
Filipa Lacerda 已提交
70
      discussionId,
71
    }, {});
72
  }
73
}
74

75
gl.DiffNotesResolveServiceClass = ResolveServiceClass;