summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/batch_comments/components/submit_dropdown.vue
blob: 6aa5bb715b2095a0e06d8d5038bc27bc8b4f2eca (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
<script>
import { GlDropdown, GlButton, GlIcon, GlForm, GlFormGroup, GlFormCheckbox } from '@gitlab/ui';
import { mapGetters, mapActions, mapState } from 'vuex';
import { createAlert } from '~/alert';
import MarkdownField from '~/vue_shared/components/markdown/field.vue';
import { scrollToElement } from '~/lib/utils/common_utils';
import Autosave from '~/autosave';

export default {
  components: {
    GlDropdown,
    GlButton,
    GlIcon,
    GlForm,
    GlFormGroup,
    GlFormCheckbox,
    MarkdownField,
    ApprovalPassword: () => import('ee_component/batch_comments/components/approval_password.vue'),
  },
  data() {
    return {
      isSubmitting: false,
      noteData: {
        noteable_type: '',
        noteable_id: '',
        note: '',
        approve: false,
        approval_password: '',
      },
    };
  },
  computed: {
    ...mapGetters(['getNotesData', 'getNoteableData', 'noteableType', 'getCurrentUserLastNote']),
    ...mapState('batchComments', ['shouldAnimateReviewButton']),
  },
  watch: {
    'noteData.approve': function noteDataApproveWatch() {
      setTimeout(() => {
        this.repositionDropdown();
      });
    },
  },
  mounted() {
    this.autosave = new Autosave(
      this.$refs.textarea,
      `submit_review_dropdown/${this.getNoteableData.id}`,
    );
    this.noteData.noteable_type = this.noteableType;
    this.noteData.noteable_id = this.getNoteableData.id;

    // We override the Bootstrap Vue click outside behaviour
    // to allow for clicking in the autocomplete dropdowns
    // without this override the submit dropdown will close
    // whenever a item in the autocomplete dropdown is clicked
    const originalClickOutHandler = this.$refs.submitDropdown.$refs.dropdown.clickOutHandler;
    this.$refs.submitDropdown.$refs.dropdown.clickOutHandler = (e) => {
      if (!e.target.closest('.atwho-container')) {
        originalClickOutHandler(e);
      }
    };
  },
  methods: {
    ...mapActions('batchComments', ['publishReview']),
    repositionDropdown() {
      this.$refs.submitDropdown?.$refs.dropdown?.updatePopper();
    },
    async submitReview() {
      this.isSubmitting = true;

      try {
        await this.publishReview(this.noteData);

        this.autosave.reset();

        if (window.mrTabs && (this.noteData.note || this.noteData.approve)) {
          if (this.noteData.note) {
            window.location.hash = `note_${this.getCurrentUserLastNote.id}`;
          }

          window.mrTabs.tabShown('show');

          setTimeout(() =>
            scrollToElement(document.getElementById(`note_${this.getCurrentUserLastNote.id}`)),
          );
        }
      } catch (e) {
        if (e.data?.message) {
          createAlert({ message: e.data.message, captureError: true });
        }
      }

      this.isSubmitting = false;
    },
  },
  restrictedToolbarItems: ['full-screen'],
};
</script>

<template>
  <gl-dropdown
    ref="submitDropdown"
    right
    dropup
    class="submit-review-dropdown"
    :class="{ 'submit-review-dropdown-animated': shouldAnimateReviewButton }"
    data-qa-selector="submit_review_dropdown"
    variant="info"
    category="primary"
  >
    <template #button-content>
      {{ __('Finish review') }}
      <gl-icon class="dropdown-chevron" name="chevron-up" />
    </template>
    <gl-form data-testid="submit-gl-form" @submit.prevent="submitReview">
      <gl-form-group label-for="review-note-body" label-class="gl-mb-2">
        <template #label>
          {{ __('Summary comment (optional)') }}
        </template>
        <div class="common-note-form gfm-form">
          <div class="comment-warning-wrapper-large gl-border-0 gl-bg-white">
            <markdown-field
              :is-submitting="isSubmitting"
              :add-spacing-classes="false"
              :textarea-value="noteData.note"
              :markdown-preview-path="getNoteableData.preview_note_path"
              :markdown-docs-path="getNotesData.markdownDocsPath"
              :quick-actions-docs-path="getNotesData.quickActionsDocsPath"
              :restricted-tool-bar-items="$options.restrictedToolbarItems"
              :force-autosize="false"
              class="js-no-autosize"
            >
              <template #textarea>
                <textarea
                  id="review-note-body"
                  ref="textarea"
                  v-model="noteData.note"
                  dir="auto"
                  :disabled="isSubmitting"
                  name="review[note]"
                  class="note-textarea js-gfm-input markdown-area"
                  data-supports-quick-actions="true"
                  data-testid="comment-textarea"
                  :aria-label="__('Comment')"
                  :placeholder="__('Write a comment or drag your files hereā€¦')"
                  @keydown.meta.enter="submitReview"
                  @keydown.ctrl.enter="submitReview"
                ></textarea>
              </template>
            </markdown-field>
          </div>
        </div>
      </gl-form-group>
      <template v-if="getNoteableData.current_user.can_approve">
        <gl-form-checkbox v-model="noteData.approve" data-testid="approve_merge_request">
          {{ __('Approve merge request') }}
        </gl-form-checkbox>
        <approval-password
          v-if="getNoteableData.require_password_to_approve"
          v-show="noteData.approve"
          v-model="noteData.approval_password"
          class="gl-mt-3"
          data-testid="approve_password"
        />
      </template>
      <div class="gl-display-flex gl-justify-content-start gl-mt-5">
        <gl-button
          :loading="isSubmitting"
          variant="confirm"
          type="submit"
          class="js-no-auto-disable"
          data-testid="submit-review-button"
          data-qa-selector="submit_review_button"
        >
          {{ __('Submit review') }}
        </gl-button>
      </div>
    </gl-form>
  </gl-dropdown>
</template>