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

work_item_parent_with_edit.vue « components « work_items « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 75c49ed5027afd674567a4086dd45ca63aae441a (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<script>
import { GlButton, GlForm, GlLink, GlLoadingIcon, GlCollapsibleListbox } from '@gitlab/ui';
import { debounce } from 'lodash';
import * as Sentry from '~/sentry/sentry_browser_wrapper';

import { DEFAULT_DEBOUNCE_AND_THROTTLE_MS } from '~/lib/utils/constants';
import { s__ } from '~/locale';
import updateWorkItemMutation from '~/work_items/graphql/update_work_item.mutation.graphql';

import { removeHierarchyChild } from '../graphql/cache_utils';
import groupWorkItemsQuery from '../graphql/group_work_items.query.graphql';
import projectWorkItemsQuery from '../graphql/project_work_items.query.graphql';
import {
  I18N_WORK_ITEM_ERROR_UPDATING,
  sprintfWorkItem,
  SUPPORTED_PARENT_TYPE_MAP,
} from '../constants';

export default {
  inputId: 'work-item-parent-listbox-value',
  noWorkItemId: 'no-work-item-id',
  i18n: {
    assignParentLabel: s__('WorkItem|Assign parent'),
    parentLabel: s__('WorkItem|Parent'),
    none: s__('WorkItem|None'),
    noMatchingResults: s__('WorkItem|No matching results'),
    unAssign: s__('WorkItem|Unassign'),
    workItemsFetchError: s__(
      'WorkItem|Something went wrong while fetching items. Please try again.',
    ),
  },
  components: {
    GlButton,
    GlLoadingIcon,
    GlLink,
    GlForm,
    GlCollapsibleListbox,
  },
  inject: ['fullPath', 'isGroup'],
  props: {
    workItemId: {
      type: String,
      required: true,
    },
    parent: {
      type: Object,
      required: false,
      default: null,
    },
    workItemType: {
      type: String,
      required: false,
      default: '',
    },
    canUpdate: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  data() {
    return {
      isEditing: false,
      search: '',
      updateInProgress: false,
      searchStarted: false,
      availableWorkItems: [],
      localSelectedItem: this.parent?.id,
      oldParent: this.parent,
    };
  },
  computed: {
    hasParent() {
      return this.parent !== null;
    },
    isLoading() {
      return this.$apollo.queries.availableWorkItems.loading;
    },
    listboxText() {
      return (
        this.workItems.find(({ value }) => this.localSelectedItem === value)?.text ||
        this.parent?.title ||
        this.$options.i18n.none
      );
    },
    workItems() {
      return this.availableWorkItems.map(({ id, title }) => ({ text: title, value: id }));
    },
    parentType() {
      return SUPPORTED_PARENT_TYPE_MAP[this.workItemType];
    },
  },
  watch: {
    parent: {
      handler(newVal) {
        if (!this.isEditing) {
          this.localSelectedItem = newVal?.id;
        }
      },
    },
  },
  created() {
    this.debouncedSearchKeyUpdate = debounce(this.setSearchKey, DEFAULT_DEBOUNCE_AND_THROTTLE_MS);
  },
  apollo: {
    availableWorkItems: {
      query() {
        return this.isGroup ? groupWorkItemsQuery : projectWorkItemsQuery;
      },
      variables() {
        return {
          fullPath: this.fullPath,
          searchTerm: this.search,
          types: this.parentType,
          in: this.search ? 'TITLE' : undefined,
          iid: null,
          isNumber: false,
        };
      },
      skip() {
        return !this.searchStarted;
      },
      update(data) {
        return data.workspace.workItems.nodes.filter((wi) => this.workItemId !== wi.id) || [];
      },
      error() {
        this.$emit('error', this.$options.i18n.workItemsFetchError);
      },
    },
  },
  methods: {
    blurInput() {
      this.$refs.input.$el.blur();
    },
    handleFocus() {
      this.isEditing = true;
    },
    setSearchKey(value) {
      this.search = value;
    },
    async updateParent() {
      if (this.parent?.id === this.localSelectedItem) return;

      this.updateInProgress = true;
      try {
        const {
          data: {
            workItemUpdate: { errors },
          },
        } = await this.$apollo.mutate({
          mutation: updateWorkItemMutation,
          variables: {
            input: {
              id: this.workItemId,
              hierarchyWidget: {
                parentId:
                  this.localSelectedItem === this.$options.noWorkItemId
                    ? null
                    : this.localSelectedItem,
              },
            },
          },
          update: (cache) =>
            removeHierarchyChild({
              cache,
              fullPath: this.fullPath,
              iid: this.oldParent?.iid,
              isGroup: this.isGroup,
              workItem: { id: this.workItemId },
            }),
        });

        if (errors.length) {
          this.$emit('error', errors.join('\n'));
          this.localSelectedItem = this.parent?.id || this.$options.noWorkItemId;
        }
      } catch (error) {
        this.$emit('error', sprintfWorkItem(I18N_WORK_ITEM_ERROR_UPDATING, this.workItemType));
        Sentry.captureException(error);
      } finally {
        this.updateInProgress = false;
        this.isEditing = false;
      }
    },
    handleItemClick(item) {
      this.localSelectedItem = item;
      this.searchStarted = false;
      this.search = '';
      this.updateParent();
    },
    unassignParent() {
      this.localSelectedItem = this.$options.noWorkItemId;
      this.isEditing = false;
      this.updateParent();
    },
    onListboxShown() {
      this.searchStarted = true;
    },
    onListboxHide() {
      this.searchStarted = false;
      this.search = '';
      this.isEditing = false;
    },
  },
};
</script>

<template>
  <div>
    <div class="gl-display-flex gl-align-items-center">
      <!-- hide header when editing, since we then have a form label. Keep it reachable for screenreader nav  -->
      <h3 :class="{ 'gl-sr-only': isEditing }" class="gl-mb-0! gl-heading-scale-5">
        {{ __('Parent') }}
      </h3>
      <gl-loading-icon
        v-if="updateInProgress"
        data-testid="loading-icon-parent"
        size="sm"
        inline
        class="gl-ml-2 gl-my-0"
      />
      <gl-button
        v-if="canUpdate && !isEditing"
        data-testid="edit-parent"
        category="tertiary"
        size="small"
        class="gl-ml-auto gl-mr-2"
        :disabled="updateInProgress"
        @click="isEditing = true"
        >{{ __('Edit') }}</gl-button
      >
    </div>
    <gl-form v-if="isEditing" class="gl-flex-nowrap" data-testid="work-item-parent-form">
      <div class="gl-display-flex gl-justify-content-space-between gl-align-items-center">
        <label :for="$options.inputId" class="gl-mb-0">{{ __('Parent') }}</label>
        <gl-button
          data-testid="apply-parent"
          category="tertiary"
          size="small"
          class="gl-mr-2"
          :disabled="updateInProgress"
          @click="isEditing = false"
          >{{ __('Apply') }}</gl-button
        >
      </div>
      <div>
        <!-- wrapper for the form input so the borders fit inside the sidebar -->
        <div class="gl-pr-2 gl-relative">
          <gl-collapsible-listbox
            id="$options.inputId"
            ref="input"
            class="gl-display-block"
            data-testid="work-item-parent-listbox"
            block
            searchable
            start-opened
            is-check-centered
            category="primary"
            fluid-width
            :searching="isLoading"
            :header-text="$options.i18n.assignParentLabel"
            :no-results-text="$options.i18n.noMatchingResults"
            :loading="updateInProgress"
            :items="workItems"
            :toggle-text="listboxText"
            :selected="localSelectedItem"
            :reset-button-label="$options.i18n.unAssign"
            @reset="unassignParent"
            @search="debouncedSearchKeyUpdate"
            @select="handleItemClick"
            @shown="onListboxShown"
            @hidden="onListboxHide"
          >
            <template #list-item="{ item }">
              <div @click="handleItemClick(item.value, $event)">
                {{ item.text }}
              </div>
            </template>
          </gl-collapsible-listbox>
        </div>
      </div>
    </gl-form>
    <template v-else-if="hasParent">
      <gl-link
        data-testid="work-item-parent-link"
        class="gl-link gl-text-gray-900 gl-display-inline-block gl-max-w-full gl-white-space-nowrap gl-text-overflow-ellipsis gl-overflow-hidden"
        :href="parent.webUrl"
        >{{ listboxText }}</gl-link
      >
    </template>
    <template v-else>
      <div data-testid="work-item-parent-none" class="gl-text-secondary">{{ __('None') }}</div>
    </template>
  </div>
</template>