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

import $ from 'jquery';
import Vue from 'vue';

const CommentAndResolveBtn = Vue.extend({
  props: {
    discussionId: {
      type: String,
      required: true,
    },
  },
  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() {
    if (this.discussionId) {
      this.discussion = CommentsStore.state[this.discussionId];
    }
  },
  mounted: function() {
    if (!this.discussionId) return;

    const $textarea = $(
      `.js-discussion-note-form[data-discussion-id=${this.discussionId}] .note-textarea`,
    );
    this.textareaIsEmpty = $textarea.val() === '';

    $textarea.on('input.comment-and-resolve-btn', () => {
      this.textareaIsEmpty = $textarea.val() === '';
    });
  },
  destroyed: function() {
    if (!this.discussionId) return;

    $(`.js-discussion-note-form[data-discussion-id=${this.discussionId}] .note-textarea`).off(
      'input.comment-and-resolve-btn',
    );
  },
});

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