summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/diff_notes/components/resolve_btn.js
blob: df4c72ba0ed442ab83b53f0712d436c48f65adce (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* eslint-disable comma-dangle, object-shorthand, func-names, quote-props, no-else-return, camelcase, max-len */
/* global CommentsStore */
/* global ResolveService */

import $ from 'jquery';
import Vue from 'vue';
import Flash from '../../flash';

const ResolveBtn = Vue.extend({
  props: {
    noteId: Number,
    discussionId: String,
    resolved: Boolean,
    canResolve: Boolean,
    resolvedBy: String,
    authorName: String,
    authorAvatar: String,
    noteTruncated: String,
  },
  data: function () {
    return {
      discussions: CommentsStore.state,
      loading: false
    };
  },
  watch: {
    'discussions': {
      handler: 'updateTooltip',
      deep: true
    }
  },
  computed: {
    discussion: function () {
      return this.discussions[this.discussionId];
    },
    note: function () {
      return this.discussion ? this.discussion.getNote(this.noteId) : {};
    },
    buttonText: function () {
      if (this.isResolved) {
        return `Resolved by ${this.resolvedByName}`;
      } else if (this.canResolve) {
        return 'Mark as resolved';
      } else {
        return 'Unable to resolve';
      }
    },
    isResolved: function () {
      if (this.note) {
        return this.note.resolved;
      } else {
        return false;
      }
    },
    resolvedByName: function () {
      return this.note.resolved_by;
    },
  },
  methods: {
    updateTooltip: function () {
      this.$nextTick(() => {
        $(this.$refs.button)
          .tooltip('hide')
          .tooltip('fixTitle');
      });
    },
    resolve: function () {
      if (!this.canResolve) return;

      let promise;
      this.loading = true;

      if (this.isResolved) {
        promise = ResolveService
          .unresolve(this.noteId);
      } else {
        promise = ResolveService
          .resolve(this.noteId);
      }

      promise
        .then(resp => resp.json())
        .then((data) => {
          this.loading = false;

          const resolved_by = data ? data.resolved_by : null;

          CommentsStore.update(this.discussionId, this.noteId, !this.isResolved, resolved_by);
          this.discussion.updateHeadline(data);
          gl.mrWidget.checkStatus();
          document.dispatchEvent(new CustomEvent('refreshVueNotes'));

          this.updateTooltip();
        })
        .catch(() => new Flash('An error occurred when trying to resolve a comment. Please try again.'));
    }
  },
  mounted: function () {
    $(this.$refs.button).tooltip({
      container: 'body'
    });
  },
  beforeDestroy: function () {
    CommentsStore.delete(this.discussionId, this.noteId);
  },
  created: function () {
    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,
    });
  }
});

Vue.component('resolve-btn', ResolveBtn);