summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/notes/components/issue_note.vue
blob: efe2bf73c40e9cd9786acdf5888b749492ed4a8e (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<script>
  /* global Flash */

  import { mapGetters, mapActions } from 'vuex';
  import userAvatarLink from '../../vue_shared/components/user_avatar/user_avatar_link.vue';
  import issueNoteHeader from './issue_note_header.vue';
  import issueNoteActions from './issue_note_actions.vue';
  import issueNoteBody from './issue_note_body.vue';
  import eventHub from '../event_hub';

  export default {
    props: {
      note: {
        type: Object,
        required: true,
      },
    },
    data() {
      return {
        isEditing: false,
        isDeleting: false,
        currentUserId: window.gon.current_user_id,
      };
    },
    components: {
      userAvatarLink,
      issueNoteHeader,
      issueNoteActions,
      issueNoteBody,
    },
    computed: {
      ...mapGetters([
        'targetNoteHash',
      ]),
      author() {
        return this.note.author;
      },
      classNameBindings() {
        return {
          'is-editing': this.isEditing,
          'disabled-content': this.isDeleting,
          'js-my-note': this.author.id === this.currentUserId,
          target: this.targetNoteHash === this.noteAnchorId,
        };
      },
      canReportAsAbuse() {
        return this.note.report_abuse_path && this.author.id !== this.currentUserId;
      },
      noteAnchorId() {
        return `note_${this.note.id}`;
      },
    },
    methods: {
      ...mapActions([
        'deleteNote',
        'updateNote',
        'scrollToNoteIfNeeded',
      ]),
      editHandler() {
        this.isEditing = true;
      },
      deleteHandler() {
        // eslint-disable-next-line no-alert
        const isConfirmed = confirm('Are you sure you want to delete this list?');

        if (isConfirmed) {
          this.isDeleting = true;
          this.deleteNote(this.note)
            .then(() => {
              this.isDeleting = false;
            })
            .catch(() => {
              Flash('Something went wrong while deleting your note. Please try again.');
              this.isDeleting = false;
            });
        }
      },
      formUpdateHandler(note) {
        const data = {
          endpoint: this.note.path,
          note: {
            full_data: true,
            target_type: 'issue',
            target_id: this.note.noteable_id,
            note,
          },
        };

        this.updateNote(data)
          .then(() => {
            this.isEditing = false;
            // TODO: this could be moved down, by setting a prop
            $(this.$refs.noteBody.$el).renderGFM();
          })
          .catch(() => Flash('Something went wrong while editing your comment. Please try again.'));
      },
      formCancelHandler(shouldConfirm, isDirty) {
        if (shouldConfirm && isDirty) {
          // eslint-disable-next-line no-alert
          if (!confirm('Are you sure you want to cancel editing this comment?')) return;
        }

        this.isEditing = false;
      },
    },
    created() {
      eventHub.$on('enterEditMode', ({ noteId }) => {
        if (noteId === this.note.id) {
          this.isEditing = true;
          this.scrollToNoteIfNeeded($(this.$el));
        }
      });
    },
  };
</script>

<template>
  <li
    class="note timeline-entry"
    :id="noteAnchorId"
    :class="classNameBindings"
    :note-id="note.id">
    <div class="timeline-entry-inner">
      <div class="timeline-icon">
        <user-avatar-link
          :link-href="author.path"
          :img-src="author.avatar_url"
          :img-alt="author.name"
          :img-size="40"
          />
      </div>
      <div class="timeline-content">
        <div class="note-header">
          <issue-note-header
            :author="author"
            :created-at="note.created_at"
            :note-id="note.id"
            action-text="commented"
            />
          <issue-note-actions
            :author-id="author.id"
            :note-id="note.id"
            :access-level="note.human_access"
            :can-edit="note.current_user.can_edit"
            :can-delete="note.current_user.can_edit"
            :can-report-as-abuse="canReportAsAbuse"
            :report-abuse-path="note.report_abuse_path"
            :edit-handler="editHandler"
            :delete-handler="deleteHandler"
            />
        </div>
        <issue-note-body
          :note="note"
          :can-edit="note.current_user.can_edit"
          :is-editing="isEditing"
          @handleFormUpdate="formUpdateHandler"
          @cancelFormEdition="formCancelHandler"
          ref="noteBody"
          />
      </div>
    </div>
  </li>
</template>