summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/notes/components/issue_discussion.vue
blob: 0f13221b81e9ad561a65cc8cad15dedabedcaa16 (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<script>
  import { mapActions, mapGetters } from 'vuex';
  import Flash from '../../flash';
  import { SYSTEM_NOTE } from '../constants';
  import issueNote from './issue_note.vue';
  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 issueNoteSignedOutWidget from './issue_note_signed_out_widget.vue';
  import issueNoteEditedText from './issue_note_edited_text.vue';
  import issueNoteForm from './issue_note_form.vue';
  import placeholderNote from '../../vue_shared/components/notes/placeholder_note.vue';
  import placeholderSystemNote from '../../vue_shared/components/notes/placeholder_system_note.vue';
  import autosave from '../mixins/autosave';

  export default {
    props: {
      note: {
        type: Object,
        required: true,
      },
    },
    data() {
      return {
        isReplying: false,
      };
    },
    components: {
      issueNote,
      userAvatarLink,
      issueNoteHeader,
      issueNoteActions,
      issueNoteSignedOutWidget,
      issueNoteEditedText,
      issueNoteForm,
      placeholderNote,
      placeholderSystemNote,
    },
    mixins: [
      autosave,
    ],
    computed: {
      ...mapGetters([
        'getIssueData',
      ]),
      discussion() {
        return this.note.notes[0];
      },
      author() {
        return this.discussion.author;
      },
      canReply() {
        return this.getIssueData.current_user.can_create_note;
      },
      newNotePath() {
        return this.getIssueData.create_note_path;
      },
      lastUpdatedBy() {
        const { notes } = this.note;

        if (notes.length > 1) {
          return notes[notes.length - 1].author;
        }

        return null;
      },
      lastUpdatedAt() {
        const { notes } = this.note;

        if (notes.length > 1) {
          return notes[notes.length - 1].created_at;
        }

        return null;
      },
    },
    methods: {
      ...mapActions([
        'saveNote',
        'toggleDiscussion',
        'removePlaceholderNotes',
      ]),
      componentName(note) {
        if (note.isPlaceholderNote) {
          if (note.placeholderType === SYSTEM_NOTE) {
            return placeholderSystemNote;
          }
          return placeholderNote;
        }

        return issueNote;
      },
      componentData(note) {
        return note.isPlaceholderNote ? note.notes[0] : note;
      },
      toggleDiscussionHandler() {
        this.toggleDiscussion({ discussionId: this.note.id });
      },
      showReplyForm() {
        this.isReplying = true;
      },
      cancelReplyForm(shouldConfirm) {
        if (shouldConfirm && this.$refs.noteForm.isDirty) {
          // eslint-disable-next-line no-alert
          if (!confirm('Are you sure you want to cancel creating this comment?')) {
            return;
          }
        }

        this.resetAutoSave();
        this.isReplying = false;
      },
      saveReply(noteText, form, callback) {
        const replyData = {
          endpoint: this.newNotePath,
          flashContainer: this.$el,
          data: {
            in_reply_to_discussion_id: this.note.reply_id,
            target_type: 'issue',
            target_id: this.discussion.noteable_id,
            note: { note: noteText },
          },
        };
        this.isReplying = false;

        this.saveNote(replyData)
          .then(() => {
            this.resetAutoSave();
            callback();
          })
          .catch((err) => {
            this.removePlaceholderNotes();
            this.isReplying = true;
            this.$nextTick(() => {
              const msg = 'Your comment could not be submitted! Please check your network connection and try again.';
              Flash(msg, 'alert', this.$el);
              this.$refs.noteForm.note = noteText;
              callback(err);
            });
          });
      },
    },
    mounted() {
      if (this.isReplying) {
        this.initAutoSave();
      }
    },
    updated() {
      if (this.isReplying) {
        if (!this.autosave) {
          this.initAutoSave();
        } else {
          this.setAutoSave();
        }
      }
    },
  };
</script>

<template>
  <li class="note note-discussion timeline-entry">
    <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="discussion">
          <div class="discussion-header">
            <issue-note-header
              :author="author"
              :created-at="discussion.created_at"
              :note-id="discussion.id"
              :include-toggle="true"
              @toggleHandler="toggleDiscussionHandler"
              action-text="started a discussion"
              class="discussion"
              />
            <issue-note-edited-text
              v-if="lastUpdatedAt"
              :edited-at="lastUpdatedAt"
              :edited-by="lastUpdatedBy"
              action-text="Last updated"
              class-name="discussion-headline-light js-discussion-headline"
              />
            </div>
          </div>
          <div
            v-if="note.expanded"
            class="discussion-body">
            <div class="panel panel-default">
              <div class="discussion-notes">
                <ul class="notes">
                  <component
                    v-for="note in note.notes"
                    :is="componentName(note)"
                    :note="componentData(note)"
                    :key="note.id"
                    />
                </ul>
                <div
                  :class="{ 'is-replying': isReplying }"
                  class="discussion-reply-holder">
                  <button
                    v-if="canReply && !isReplying"
                    @click="showReplyForm"
                    type="button"
                    class="js-vue-discussion-reply btn btn-text-field"
                    title="Add a reply">Reply...</button>
                  <issue-note-form
                    v-if="isReplying"
                    save-button-title="Comment"
                    :discussion="note"
                    :is-editing="false"
                    @handleFormUpdate="saveReply"
                    @cancelFormEdition="cancelReplyForm"
                    ref="noteForm"
                    />
                  <issue-note-signed-out-widget v-if="!canReply" />
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </li>
</template>