debian-mirror-gitlab/app/assets/javascripts/diff_notes/components/resolve_btn.js

146 lines
3.1 KiB
JavaScript
Raw Normal View History

2017-08-17 22:00:37 +05:30
/* global CommentsStore */
/* global ResolveService */
2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2017-08-17 22:00:37 +05:30
import Vue from 'vue';
2018-03-17 18:26:18 +05:30
import Flash from '../../flash';
2019-07-31 22:56:46 +05:30
import { sprintf, __ } from '~/locale';
2017-08-17 22:00:37 +05:30
const ResolveBtn = Vue.extend({
props: {
2018-11-08 19:23:39 +05:30
noteId: {
type: Number,
required: true,
},
discussionId: {
type: String,
required: true,
},
resolved: {
type: Boolean,
required: true,
},
canResolve: {
type: Boolean,
required: true,
},
resolvedBy: {
type: String,
required: true,
},
authorName: {
type: String,
required: true,
},
authorAvatar: {
type: String,
required: true,
},
noteTruncated: {
type: String,
required: true,
},
2017-08-17 22:00:37 +05:30
},
2018-11-08 19:23:39 +05:30
data() {
2017-08-17 22:00:37 +05:30
return {
discussions: CommentsStore.state,
2018-11-08 19:23:39 +05:30
loading: false,
2017-08-17 22:00:37 +05:30
};
},
computed: {
2018-11-08 19:23:39 +05:30
discussion() {
2017-08-17 22:00:37 +05:30
return this.discussions[this.discussionId];
},
2018-11-08 19:23:39 +05:30
note() {
2017-08-17 22:00:37 +05:30
return this.discussion ? this.discussion.getNote(this.noteId) : {};
},
2018-11-08 19:23:39 +05:30
buttonText() {
2017-08-17 22:00:37 +05:30
if (this.isResolved) {
2019-07-31 22:56:46 +05:30
return sprintf(__('Resolved by %{resolvedByName}'), {
resolvedByName: this.resolvedByName,
});
2017-08-17 22:00:37 +05:30
} else if (this.canResolve) {
2019-07-31 22:56:46 +05:30
return __('Mark as resolved');
2017-08-17 22:00:37 +05:30
}
2018-11-08 19:23:39 +05:30
2019-07-31 22:56:46 +05:30
return __('Unable to resolve');
2017-08-17 22:00:37 +05:30
},
2018-11-08 19:23:39 +05:30
isResolved() {
2017-08-17 22:00:37 +05:30
if (this.note) {
return this.note.resolved;
}
2018-11-08 19:23:39 +05:30
return false;
2017-08-17 22:00:37 +05:30
},
2018-11-08 19:23:39 +05:30
resolvedByName() {
2017-08-17 22:00:37 +05:30
return this.note.resolved_by;
},
},
2018-11-08 19:23:39 +05:30
watch: {
discussions: {
handler: 'updateTooltip',
deep: true,
},
},
mounted() {
$(this.$refs.button).tooltip({
container: 'body',
});
},
beforeDestroy() {
CommentsStore.delete(this.discussionId, this.noteId);
},
created() {
CommentsStore.create({
discussionId: this.discussionId,
noteId: this.noteId,
canResolve: this.canResolve,
resolved: this.resolved,
resolvedBy: this.resolvedBy,
authorName: this.authorName,
authorAvatar: this.authorAvatar,
noteTruncated: this.noteTruncated,
});
},
2017-08-17 22:00:37 +05:30
methods: {
2018-11-08 19:23:39 +05:30
updateTooltip() {
2017-08-17 22:00:37 +05:30
this.$nextTick(() => {
$(this.$refs.button)
.tooltip('hide')
2018-11-08 19:23:39 +05:30
.tooltip('_fixTitle');
2017-08-17 22:00:37 +05:30
});
},
2018-11-08 19:23:39 +05:30
resolve() {
2017-08-17 22:00:37 +05:30
if (!this.canResolve) return;
let promise;
this.loading = true;
if (this.isResolved) {
2018-11-08 19:23:39 +05:30
promise = ResolveService.unresolve(this.noteId);
2017-08-17 22:00:37 +05:30
} else {
2018-11-08 19:23:39 +05:30
promise = ResolveService.resolve(this.noteId);
2017-08-17 22:00:37 +05:30
}
2017-09-10 17:25:29 +05:30
promise
.then(resp => resp.json())
2018-11-08 19:23:39 +05:30
.then(data => {
2017-09-10 17:25:29 +05:30
this.loading = false;
2017-08-17 22:00:37 +05:30
2018-11-08 19:23:39 +05:30
const resolvedBy = data ? data.resolved_by : null;
2017-08-17 22:00:37 +05:30
2018-11-08 19:23:39 +05:30
CommentsStore.update(this.discussionId, this.noteId, !this.isResolved, resolvedBy);
2017-08-17 22:00:37 +05:30
this.discussion.updateHeadline(data);
gl.mrWidget.checkStatus();
2017-09-10 17:25:29 +05:30
this.updateTooltip();
})
2018-11-08 19:23:39 +05:30
.catch(
2019-07-31 22:56:46 +05:30
() =>
new Flash(__('An error occurred when trying to resolve a comment. Please try again.')),
2018-11-08 19:23:39 +05:30
);
},
2017-08-17 22:00:37 +05:30
},
});
Vue.component('resolve-btn', ResolveBtn);