gitlab-org--gitlab-foss/app/assets/javascripts/diff_notes/services/resolve.js

77 lines
2 KiB
JavaScript
Raw Normal View History

/* global CommentsStore */
2017-03-17 13:21:25 -04:00
import Vue from 'vue';
import Flash from '../../flash';
import '../../vue_shared/vue_resource_interceptor';
2017-03-17 13:21:25 -04:00
2017-04-17 02:35:56 -04:00
window.gl = window.gl || {};
2017-04-17 02:35:56 -04:00
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`);
2017-04-17 02:35:56 -04:00
}
2017-04-17 02:35:56 -04:00
resolve(noteId) {
return this.noteResource.save({ noteId }, {});
}
2017-04-17 02:35:56 -04:00
unresolve(noteId) {
return this.noteResource.delete({ noteId }, {});
}
2017-04-17 02:35:56 -04:00
toggleResolveForDiscussion(mergeRequestId, discussionId) {
const discussion = CommentsStore.state[discussionId];
const isResolved = discussion.isResolved();
let promise;
2017-04-17 02:35:56 -04:00
if (isResolved) {
promise = this.unResolveAll(mergeRequestId, discussionId);
} else {
promise = this.resolveAll(mergeRequestId, discussionId);
}
2017-07-12 10:47:09 -04:00
promise
.then(resp => resp.json())
.then((data) => {
discussion.loading = false;
const resolvedBy = data ? data.resolved_by : null;
2017-04-17 02:35:56 -04:00
if (isResolved) {
discussion.unResolveAllNotes();
} else {
2017-07-12 10:47:09 -04:00
discussion.resolveAllNotes(resolvedBy);
}
if (gl.mrWidget) gl.mrWidget.checkStatus();
2017-04-17 02:35:56 -04:00
discussion.updateHeadline(data);
document.dispatchEvent(new CustomEvent('refreshVueNotes'));
2017-07-12 10:47:09 -04:00
})
.catch(() => new Flash('An error occurred when trying to resolve a discussion. Please try again.'));
2017-04-17 02:35:56 -04:00
}
2016-07-29 06:52:08 -04:00
2017-04-17 02:35:56 -04:00
resolveAll(mergeRequestId, discussionId) {
const discussion = CommentsStore.state[discussionId];
2017-04-17 02:35:56 -04:00
discussion.loading = true;
return this.discussionResource.save({
mergeRequestId,
2017-07-12 10:47:09 -04:00
discussionId,
2017-04-17 02:35:56 -04:00
}, {});
}
2017-04-17 02:35:56 -04:00
unResolveAll(mergeRequestId, discussionId) {
const discussion = CommentsStore.state[discussionId];
2016-07-29 06:52:08 -04:00
2017-04-17 02:35:56 -04:00
discussion.loading = true;
2017-04-17 02:35:56 -04:00
return this.discussionResource.delete({
mergeRequestId,
2017-07-12 10:47:09 -04:00
discussionId,
2017-04-17 02:35:56 -04:00
}, {});
}
2017-04-17 02:35:56 -04:00
}
2017-04-17 02:35:56 -04:00
gl.DiffNotesResolveServiceClass = ResolveServiceClass;