summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/work_items/components/notes/work_item_note.vue
blob: 1d3319dbc90beb11b222eee364b83499fd0e426f (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<script>
import { GlAvatarLink, GlAvatar, GlDropdown, GlDropdownItem, GlTooltipDirective } from '@gitlab/ui';
import * as Sentry from '@sentry/browser';
import { __ } from '~/locale';
import { updateDraft, clearDraft } from '~/lib/utils/autosave';
import { renderMarkdown } from '~/notes/utils';
import EditedAt from '~/issues/show/components/edited.vue';
import TimelineEntryItem from '~/vue_shared/components/notes/timeline_entry_item.vue';
import NoteBody from '~/work_items/components/notes/work_item_note_body.vue';
import NoteHeader from '~/notes/components/note_header.vue';
import NoteActions from '~/work_items/components/notes/work_item_note_actions.vue';
import updateWorkItemNoteMutation from '../../graphql/notes/update_work_item_note.mutation.graphql';
import WorkItemCommentForm from './work_item_comment_form.vue';

export default {
  name: 'WorkItemNoteThread',
  i18n: {
    moreActionsText: __('More actions'),
    deleteNoteText: __('Delete comment'),
  },
  components: {
    TimelineEntryItem,
    NoteBody,
    NoteHeader,
    NoteActions,
    GlAvatar,
    GlAvatarLink,
    GlDropdown,
    GlDropdownItem,
    WorkItemCommentForm,
    EditedAt,
  },
  directives: {
    GlTooltip: GlTooltipDirective,
  },
  props: {
    note: {
      type: Object,
      required: true,
    },
    isFirstNote: {
      type: Boolean,
      required: false,
      default: false,
    },
    hasReplies: {
      type: Boolean,
      required: false,
      default: false,
    },
    workItemType: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      isEditing: false,
    };
  },
  computed: {
    author() {
      return this.note.author;
    },
    entryClass() {
      return {
        'note note-wrapper note-comment gl-mb-4': true,
        'gl-p-2 gl-mt-3 gl-pl-3': !this.isFirstNote,
      };
    },
    showReply() {
      return this.note.userPermissions.createNote && this.isFirstNote;
    },
    noteHeaderClass() {
      return {
        'note-header': true,
        'gl-pt-2': !this.isFirstNote,
      };
    },
    autosaveKey() {
      // eslint-disable-next-line @gitlab/require-i18n-strings
      return `${this.note.id}-comment`;
    },
    lastEditedBy() {
      return this.note.lastEditedBy;
    },
    hasAdminPermission() {
      return this.note.userPermissions.adminNote;
    },
    hasAwardEmojiPermission() {
      return this.note.userPermissions.awardEmoji;
    },
  },
  methods: {
    showReplyForm() {
      this.$emit('startReplying');
    },
    startEditing() {
      this.isEditing = true;
      updateDraft(this.autosaveKey, this.note.body);
    },
    async updateNote(newText) {
      this.isEditing = false;
      try {
        await this.$apollo.mutate({
          mutation: updateWorkItemNoteMutation,
          variables: {
            input: {
              id: this.note.id,
              body: newText,
            },
          },
          optimisticResponse: {
            updateNote: {
              errors: [],
              note: {
                ...this.note,
                bodyHtml: renderMarkdown(newText),
              },
            },
          },
        });
        clearDraft(this.autosaveKey);
      } catch (error) {
        updateDraft(this.autosaveKey, newText);
        this.isEditing = true;
        this.$emit('error', __('Something went wrong when updating a comment. Please try again'));
        Sentry.captureException(error);
      }
    },
  },
};
</script>

<template>
  <timeline-entry-item :class="entryClass">
    <div v-if="!isFirstNote" :key="note.id" class="timeline-avatar gl-float-left">
      <gl-avatar-link :href="author.webUrl">
        <gl-avatar
          :src="author.avatarUrl"
          :entity-name="author.username"
          :alt="author.name"
          :size="32"
        />
      </gl-avatar-link>
    </div>
    <work-item-comment-form
      v-if="isEditing"
      :work-item-type="workItemType"
      :aria-label="__('Edit comment')"
      :autosave-key="autosaveKey"
      :initial-value="note.body"
      :comment-button-text="__('Save comment')"
      :class="{ 'gl-pl-8': !isFirstNote }"
      @cancelEditing="isEditing = false"
      @submitForm="updateNote"
    />
    <div v-else class="timeline-content-inner" data-testid="note-wrapper">
      <div :class="noteHeaderClass">
        <note-header :author="author" :created-at="note.createdAt" :note-id="note.id">
          <span v-if="note.createdAt" class="d-none d-sm-inline">&middot;</span>
        </note-header>
        <div class="gl-display-inline-flex">
          <note-actions
            :show-award-emoji="hasAwardEmojiPermission"
            :show-reply="showReply"
            :show-edit="hasAdminPermission"
            :note-id="note.id"
            @startReplying="showReplyForm"
            @startEditing="startEditing"
            @error="($event) => $emit('error', $event)"
          />
          <!-- v-if condition should be moved to "delete" dropdown item as soon as we implement copying the link -->
          <gl-dropdown
            v-if="hasAdminPermission"
            v-gl-tooltip
            icon="ellipsis_v"
            text-sr-only
            right
            :text="$options.i18n.moreActionsText"
            :title="$options.i18n.moreActionsText"
            category="tertiary"
            no-caret
          >
            <gl-dropdown-item
              variant="danger"
              data-testid="delete-note-action"
              @click="$emit('deleteNote')"
            >
              {{ $options.i18n.deleteNoteText }}
            </gl-dropdown-item>
          </gl-dropdown>
        </div>
      </div>
      <div class="timeline-discussion-body">
        <note-body ref="noteBody" :note="note" :has-replies="hasReplies" />
      </div>
      <edited-at
        v-if="note.lastEditedBy"
        :updated-at="note.lastEditedAt"
        :updated-by-name="lastEditedBy.name"
        :updated-by-path="lastEditedBy.webPath"
        :class="isFirstNote ? 'gl-pl-3' : 'gl-pl-8'"
      />
    </div>
  </timeline-entry-item>
</template>