Welcome to mirror list, hosted at ThFree Co, Russian Federation.

related_issues_root.vue « components « related_issues « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 38e1d6e9d4f9f4f79c513d1302b37ce759ad7e5a (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<script>
/*
`rawReferences` are separated by spaces.
Given `abc 123 zxc`, `rawReferences = ['abc', '123', 'zxc']`

Consider you are typing `abc 123 zxc` in the input and your caret position is
at position 4 right before the `123` `rawReference`. Then you type `#` and
it becomes a valid reference, `#123`, but we don't want to jump it straight into
`pendingReferences` because you could still want to type. Say you typed `999`
and now we have `#999123`. Only when you move your caret away from that `rawReference`
do we actually put it in the `pendingReferences`.

Your caret can stop touching a `rawReference` can happen in a variety of ways:

 - As you type, we only tokenize after you type a space or move with the arrow keys
 - On blur, we consider your caret not touching anything

---

 - When you click the "Add related issues"(in the `AddIssuableForm`),
   we submit the `pendingReferences` to the server and they come back as actual `relatedIssues`
 - When you click the "Cancel"(in the `AddIssuableForm`), we clear out `pendingReferences`
   and hide the `AddIssuableForm` area.

*/
import { createAlert } from '~/flash';
import { getIdFromGraphQLId, isGid } from '~/graphql_shared/utils';
import { __ } from '~/locale';
import {
  relatedIssuesRemoveErrorMap,
  pathIndeterminateErrorMap,
  addRelatedIssueErrorMap,
  issuableTypesMap,
  PathIdSeparator,
} from '../constants';
import RelatedIssuesService from '../services/related_issues_service';
import RelatedIssuesStore from '../stores/related_issues_store';
import RelatedIssuesBlock from './related_issues_block.vue';

export default {
  name: 'RelatedIssuesRoot',
  components: {
    RelatedIssuesBlock,
  },
  props: {
    endpoint: {
      type: String,
      required: true,
    },
    canAdmin: {
      type: Boolean,
      required: false,
      default: false,
    },
    canReorder: {
      type: Boolean,
      required: false,
      default: false,
    },
    helpPath: {
      type: String,
      required: false,
      default: '',
    },
    issuableType: {
      type: String,
      required: false,
      default: issuableTypesMap.ISSUE,
    },
    allowAutoComplete: {
      type: Boolean,
      required: false,
      default: true,
    },
    autoCompleteEpics: {
      type: Boolean,
      required: false,
      default: true,
    },
    autoCompleteIssues: {
      type: Boolean,
      required: false,
      default: true,
    },
    pathIdSeparator: {
      type: String,
      required: false,
      default: PathIdSeparator.Issue,
    },
    cssClass: {
      type: String,
      required: false,
      default: '',
    },
    showCategorizedIssues: {
      type: Boolean,
      required: false,
      default: true,
    },
  },
  data() {
    this.store = new RelatedIssuesStore();

    return {
      state: this.store.state,
      isFetching: false,
      isSubmitting: false,
      isFormVisible: false,
      inputValue: '',
    };
  },
  computed: {
    autoCompleteSources() {
      if (!this.allowAutoComplete) return {};
      return gl.GfmAutoComplete && gl.GfmAutoComplete.dataSources;
    },
  },
  created() {
    this.service = new RelatedIssuesService(this.endpoint);
    this.fetchRelatedIssues();
  },
  methods: {
    findRelatedIssueById(id) {
      return this.state.relatedIssues.find((issue) => issue.id === id);
    },
    onRelatedIssueRemoveRequest(idToRemove) {
      if (isGid(idToRemove)) {
        const deletedId = getIdFromGraphQLId(idToRemove);
        this.state.relatedIssues = this.state.relatedIssues.filter(
          (issue) => issue.id !== deletedId,
        );
        return;
      }

      const issueToRemove = this.findRelatedIssueById(idToRemove);

      if (issueToRemove) {
        RelatedIssuesService.remove(issueToRemove.relationPath)
          .then(({ data }) => {
            this.store.setRelatedIssues(data.issuables);
          })
          .catch((res) => {
            if (res && res.status !== 404) {
              createAlert({ message: relatedIssuesRemoveErrorMap[this.issuableType] });
            }
          });
      } else {
        createAlert({ message: pathIndeterminateErrorMap[this.issuableType] });
      }
    },
    onToggleAddRelatedIssuesForm() {
      this.isFormVisible = !this.isFormVisible;
    },
    onPendingIssueRemoveRequest(indexToRemove) {
      this.store.removePendingRelatedIssue(indexToRemove);
    },
    onPendingFormSubmit(event) {
      this.processAllReferences(event.pendingReferences);

      if (this.state.pendingReferences.length > 0) {
        this.isSubmitting = true;
        this.service
          .addRelatedIssues(this.state.pendingReferences, event.linkedIssueType)
          .then(({ data }) => {
            // We could potentially lose some pending issues in the interim here
            this.store.setPendingReferences([]);
            this.store.setRelatedIssues(data.issuables);

            // Close the form on submission
            this.isFormVisible = false;
          })
          .catch(({ response }) => {
            let errorMessage = addRelatedIssueErrorMap[this.issuableType];
            if (response && response.data && response.data.message) {
              errorMessage = response.data.message;
            }
            createAlert({ message: errorMessage });
          })
          .finally(() => {
            this.isSubmitting = false;
          });
      }
    },
    onPendingFormCancel() {
      this.isFormVisible = false;
      this.store.setPendingReferences([]);
      this.inputValue = '';
    },
    fetchRelatedIssues() {
      this.isFetching = true;
      this.service
        .fetchRelatedIssues()
        .then(({ data }) => {
          this.store.setRelatedIssues(data);
        })
        .catch(() => {
          this.store.setRelatedIssues([]);
          createAlert({ message: __('An error occurred while fetching issues.') });
        })
        .finally(() => {
          this.isFetching = false;
        });
    },
    saveIssueOrder({ issueId, beforeId, afterId, oldIndex, newIndex }) {
      const issueToReorder = this.findRelatedIssueById(issueId);

      if (issueToReorder) {
        RelatedIssuesService.saveOrder({
          endpoint: issueToReorder.relationPath,
          move_before_id: beforeId,
          move_after_id: afterId,
        })
          .then(({ data }) => {
            if (!data.message) {
              this.store.updateIssueOrder(oldIndex, newIndex);
            }
          })
          .catch(() => {
            createAlert({ message: __('An error occurred while reordering issues.') });
          });
      }
    },
    onInput({ untouchedRawReferences, touchedReference }) {
      this.store.addPendingReferences(untouchedRawReferences);

      this.formatInput(touchedReference);
    },
    formatInput(touchedReference = '') {
      const startsWithNumber = String(touchedReference).match(/^[0-9]/) !== null;

      if (startsWithNumber) {
        const { pathIdSeparator } = this;
        this.inputValue = `${pathIdSeparator}${touchedReference}`;
      } else {
        this.inputValue = `${touchedReference}`;
      }
    },
    onBlur(newValue) {
      this.processAllReferences(newValue);
    },
    processAllReferences(value = '') {
      const rawReferences = value.split(/\s+/).filter((reference) => reference.trim().length > 0);

      this.store.addPendingReferences(rawReferences);
      this.inputValue = '';
    },
  },
};
</script>

<template>
  <related-issues-block
    :class="cssClass"
    :help-path="helpPath"
    :is-fetching="isFetching"
    :is-submitting="isSubmitting"
    :related-issues="state.relatedIssues"
    :can-admin="canAdmin"
    :can-reorder="canReorder"
    :pending-references="state.pendingReferences"
    :is-form-visible="isFormVisible"
    :input-value="inputValue"
    :auto-complete-sources="autoCompleteSources"
    :auto-complete-epics="autoCompleteEpics"
    :auto-complete-issues="autoCompleteIssues"
    :issuable-type="issuableType"
    :path-id-separator="pathIdSeparator"
    :show-categorized-issues="showCategorizedIssues"
    @saveReorder="saveIssueOrder"
    @toggleAddRelatedIssuesForm="onToggleAddRelatedIssuesForm"
    @addIssuableFormInput="onInput"
    @addIssuableFormBlur="onBlur"
    @addIssuableFormSubmit="onPendingFormSubmit"
    @addIssuableFormCancel="onPendingFormCancel"
    @pendingIssuableRemoveRequest="onPendingIssueRemoveRequest"
    @relatedIssueRemoveRequest="onRelatedIssueRemoveRequest"
  />
</template>