summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/diff_notes/components/comment_resolve_btn.js
blob: fc2f20e3bcbb72907dd1da21755c6ae2586f6b82 (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
/* eslint-disable comma-dangle, object-shorthand, func-names, no-else-return, quotes, no-lonely-if, max-len */
/* global CommentsStore */

import Vue from 'vue';

(() => {
  const CommentAndResolveBtn = Vue.extend({
    props: {
      discussionId: String,
    },
    data() {
      return {
        textareaIsEmpty: true,
        discussion: {},
      };
    },
    computed: {
      showButton: function () {
        if (this.discussion) {
          return this.discussion.isResolvable();
        } else {
          return false;
        }
      },
      isDiscussionResolved: function () {
        return this.discussion.isResolved();
      },
      buttonText: function () {
        if (this.isDiscussionResolved) {
          if (this.textareaIsEmpty) {
            return "Unresolve discussion";
          } else {
            return "Comment & unresolve discussion";
          }
        } else {
          if (this.textareaIsEmpty) {
            return "Resolve discussion";
          } else {
            return "Comment & resolve discussion";
          }
        }
      }
    },
    created() {
      this.discussion = CommentsStore.state[this.discussionId];
    },
    mounted: function () {
      const $textarea = $(`#new-discussion-note-form-${this.discussionId} .note-textarea`);
      this.textareaIsEmpty = $textarea.val() === '';

      $textarea.on('input.comment-and-resolve-btn', () => {
        this.textareaIsEmpty = $textarea.val() === '';
      });
    },
    destroyed: function () {
      $(`#new-discussion-note-form-${this.discussionId} .note-textarea`).off('input.comment-and-resolve-btn');
    }
  });

  Vue.component('comment-and-resolve-btn', CommentAndResolveBtn);
})(window);