issue_notes_app.vue 3.8 KB
Newer Older
1
<script>
2
  /* global Flash */
3

4
  import { mapGetters, mapActions } from 'vuex';
5
  import store from '../stores/';
6
  import * as constants from '../constants';
7 8 9 10 11 12 13
  import issueNote from './issue_note.vue';
  import issueDiscussion from './issue_discussion.vue';
  import issueSystemNote from './issue_system_note.vue';
  import issueCommentForm from './issue_comment_form.vue';
  import placeholderNote from './issue_placeholder_note.vue';
  import placeholderSystemNote from './issue_placeholder_system_note.vue';
  import loadingIcon from '../../vue_shared/components/loading_icon.vue';
14

15 16
  export default {
    name: 'IssueNotes',
17 18 19 20 21 22 23 24 25
    props: {
      issueData: {
        type: Object,
        required: true,
      },
      notesData: {
        type: Object,
        required: true,
      },
26 27
      userData: {
        type: Object,
28
        required: false,
F
Filipa Lacerda 已提交
29
        default: {},
30
      },
31
    },
32 33 34 35 36
    store,
    data() {
      return {
        isLoading: true,
      };
37
    },
38 39 40 41 42 43 44 45
    components: {
      issueNote,
      issueDiscussion,
      issueSystemNote,
      issueCommentForm,
      loadingIcon,
      placeholderNote,
      placeholderSystemNote,
F
Fatih Acet 已提交
46
    },
47 48
    computed: {
      ...mapGetters([
49
        'notes',
50
        'notesById',
51
        'getNotesDataByProp',
52 53 54 55 56
      ]),
    },
    methods: {
      ...mapActions({
        actionFetchNotes: 'fetchNotes',
57
        poll: 'poll',
58
        actionToggleAward: 'toggleAward',
59
        scrollToNoteIfNeeded: 'scrollToNoteIfNeeded',
60 61 62
        setNotesData: 'setNotesData',
        setIssueData: 'setIssueData',
        setUserData: 'setUserData',
63 64
        setLastFetchedAt: 'setLastFetchedAt',
        setTargetNoteHash: 'setTargetNoteHash',
65 66 67 68 69 70 71 72 73 74
      }),
      getComponentName(note) {
        if (note.isPlaceholderNote) {
          if (note.placeholderType === constants.SYSTEM_NOTE) {
            return placeholderSystemNote;
          }
          return placeholderNote;
        } else if (note.individual_note) {
          return note.notes[0].system ? issueSystemNote : issueNote;
        }
75

76 77 78 79 80 81
        return issueDiscussion;
      },
      getComponentData(note) {
        return note.individual_note ? note.notes[0] : note;
      },
      fetchNotes() {
82
        this.actionFetchNotes(this.getNotesDataByProp('discussionsPath'))
83 84
          .then(() => {
            this.isLoading = false;
85

86
            // Scroll to note if we have hash fragment in the page URL
87
            this.$nextTick(() => {
88 89
              this.checkLocationHash();
            });
90
          })
F
Filipa Lacerda 已提交
91
          .catch(() => Flash('Something went wrong while fetching issue comments. Please try again.'));
92 93
      },
      initPolling() {
94
        this.setLastFetchedAt(this.getNotesDataByProp('lastFetchedAt'));
95

96
        this.poll();
97 98 99 100 101 102 103 104 105 106 107
      },
      checkLocationHash() {
        const hash = gl.utils.getLocationHash();
        const $el = $(`#${hash}`);

        if (hash && $el) {
          this.setTargetNoteHash(hash);
          this.scrollToNoteIfNeeded($el);
        }
      },
    },
108 109 110
    created() {
      this.setNotesData(this.notesData);
      this.setIssueData(this.issueData);
F
Filipa Lacerda 已提交
111
      this.setUserData(this.userData);
112
    },
113 114 115
    mounted() {
      this.fetchNotes();
      this.initPolling();
116 117 118 119 120

      this.$el.parentElement.addEventListener('toggleAward', (event) => {
        const { awardName, noteId } = event.detail;
        this.actionToggleAward({ awardName, noteId });
      });
121
    },
122
  };
123 124 125 126 127 128 129
</script>

<template>
  <div id="notes">
    <div
      v-if="isLoading"
      class="loading">
130
      <loading-icon />
131
    </div>
132

133
    <ul
134 135 136
      v-if="!isLoading"
      id="notes-list"
      class="notes main-notes-list timeline">
137

138
      <component
139
        v-for="note in notes"
140 141 142 143
        :is="getComponentName(note)"
        :note="getComponentData(note)"
        :key="note.id"
        />
144
    </ul>
145 146

    <issue-comment-form />
147 148
  </div>
</template>