summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/diff_notes/components/comment_resolve_btn.js
blob: dd60e2c7684d976f548bf4c0608f139bbca7f126 (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
/* global CommentsStore */

import $ from 'jquery';
import Vue from 'vue';
import { __ } from '~/locale';

const CommentAndResolveBtn = Vue.extend({
  props: {
    discussionId: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      textareaIsEmpty: true,
      discussion: {},
    };
  },
  computed: {
    showButton() {
      if (this.discussion) {
        return this.discussion.isResolvable();
      }
      return false;
    },
    isDiscussionResolved() {
      return this.discussion.isResolved();
    },
    buttonText() {
      if (this.textareaIsEmpty) {
        return this.isDiscussionResolved ? __('Unresolve thread') : __('Resolve thread');
      }
      return this.isDiscussionResolved
        ? __('Comment & unresolve thread')
        : __('Comment & resolve thread');
    },
  },
  created() {
    if (this.discussionId) {
      this.discussion = CommentsStore.state[this.discussionId];
    }
  },
  mounted() {
    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() {
    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);