summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/batch_comments/components/preview_dropdown.vue
blob: 195e1b7ec5c6425c0a8ef14d8a0b3c92cae3e1b1 (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
<script>
import { mapActions, mapGetters, mapState } from 'vuex';
import { GlLoadingIcon } from '@gitlab/ui';
import { sprintf, n__ } from '~/locale';
import Icon from '~/vue_shared/components/icon.vue';
import DraftsCount from './drafts_count.vue';
import PublishButton from './publish_button.vue';
import PreviewItem from './preview_item.vue';

export default {
  components: {
    GlLoadingIcon,
    Icon,
    DraftsCount,
    PublishButton,
    PreviewItem,
  },
  computed: {
    ...mapGetters(['isNotesFetched']),
    ...mapGetters('batchComments', ['draftsCount', 'sortedDrafts']),
    ...mapState('batchComments', ['showPreviewDropdown']),
    dropdownTitle() {
      return sprintf(
        n__('%{count} pending comment', '%{count} pending comments', this.draftsCount),
        { count: this.draftsCount },
      );
    },
  },
  watch: {
    showPreviewDropdown() {
      if (this.showPreviewDropdown && this.$refs.dropdown) {
        this.$nextTick(() => this.$refs.dropdown.focus());
      }
    },
  },
  mounted() {
    document.addEventListener('click', this.onClickDocument);
  },
  beforeDestroy() {
    document.removeEventListener('click', this.onClickDocument);
  },
  methods: {
    ...mapActions('batchComments', ['toggleReviewDropdown']),
    isLast(index) {
      return index === this.sortedDrafts.length - 1;
    },
    onClickDocument({ target }) {
      if (
        this.showPreviewDropdown &&
        !target.closest('.review-preview-dropdown, .js-publish-draft-button')
      ) {
        this.toggleReviewDropdown();
      }
    },
  },
};
</script>

<template>
  <div
    class="dropdown float-right review-preview-dropdown"
    :class="{
      show: showPreviewDropdown,
    }"
  >
    <button
      ref="dropdown"
      type="button"
      class="btn btn-success review-preview-dropdown-toggle qa-review-preview-toggle"
      @click="toggleReviewDropdown"
    >
      {{ __('Finish review') }}
      <drafts-count />
      <icon name="angle-up" />
    </button>
    <div
      class="dropdown-menu dropdown-menu-large dropdown-menu-right dropdown-open-top"
      :class="{
        show: showPreviewDropdown,
      }"
    >
      <div class="dropdown-title">
        {{ dropdownTitle }}
        <button
          :aria-label="__('Close')"
          type="button"
          class="dropdown-title-button dropdown-menu-close"
          @click="toggleReviewDropdown"
        >
          <icon name="close" />
        </button>
      </div>
      <div class="dropdown-content">
        <ul v-if="isNotesFetched">
          <li v-for="(draft, index) in sortedDrafts" :key="draft.id">
            <preview-item :draft="draft" :is-last="isLast(index)" />
          </li>
        </ul>
        <gl-loading-icon v-else size="lg" class="prepend-top-default append-bottom-default" />
      </div>
      <div class="dropdown-footer">
        <publish-button
          :show-count="false"
          :should-publish="true"
          :label="__('Submit review')"
          class="float-right gl-mr-3"
        />
      </div>
    </div>
  </div>
</template>