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

work_item_links_form.vue « work_item_links « 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: a01f4616cab6eab7db78f90d350c61fa9481a792 (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
<script>
import { GlAlert, GlFormGroup, GlForm, GlFormCombobox, GlButton, GlFormInput } from '@gitlab/ui';
import { getIdFromGraphQLId } from '~/graphql_shared/utils';
import { __, s__ } from '~/locale';
import projectWorkItemTypesQuery from '~/work_items/graphql/project_work_item_types.query.graphql';
import updateWorkItemMutation from '../../graphql/update_work_item.mutation.graphql';
import createWorkItemMutation from '../../graphql/create_work_item.mutation.graphql';
import { TASK_TYPE_NAME } from '../../constants';

export default {
  components: {
    GlAlert,
    GlForm,
    GlFormCombobox,
    GlButton,
    GlFormGroup,
    GlFormInput,
  },
  inject: ['projectPath', 'hasIterationsFeature'],
  props: {
    issuableGid: {
      type: String,
      required: false,
      default: null,
    },
    childrenIds: {
      type: Array,
      required: false,
      default: () => [],
    },
    parentConfidential: {
      type: Boolean,
      required: false,
      default: false,
    },
    parentIteration: {
      type: Object,
      required: false,
      default: () => {},
    },
  },
  apollo: {
    workItemTypes: {
      query: projectWorkItemTypesQuery,
      variables() {
        return {
          fullPath: this.projectPath,
        };
      },
      update(data) {
        return data.workspace?.workItemTypes?.nodes;
      },
    },
  },
  data() {
    return {
      availableWorkItems: [],
      search: '',
      error: null,
      childToCreateTitle: null,
    };
  },
  computed: {
    actionsList() {
      return [
        {
          label: this.$options.i18n.createChildOptionLabel,
          fn: () => {
            this.childToCreateTitle = this.search?.title || this.search;
          },
        },
      ];
    },
    addOrCreateButtonLabel() {
      return this.childToCreateTitle
        ? this.$options.i18n.createChildOptionLabel
        : this.$options.i18n.addTaskButtonLabel;
    },
    addOrCreateMethod() {
      return this.childToCreateTitle ? this.createChild : this.addChild;
    },
    taskWorkItemType() {
      return this.workItemTypes.find((type) => type.name === TASK_TYPE_NAME)?.id;
    },
    parentIterationId() {
      return this.parentIteration?.id;
    },
  },
  methods: {
    getIdFromGraphQLId,
    unsetError() {
      this.error = null;
    },
    addChild() {
      this.$apollo
        .mutate({
          mutation: updateWorkItemMutation,
          variables: {
            input: {
              id: this.issuableGid,
              hierarchyWidget: {
                childrenIds: [this.search.id],
              },
            },
          },
        })
        .then(({ data }) => {
          if (data.workItemUpdate?.errors?.length) {
            [this.error] = data.workItemUpdate.errors;
          } else {
            this.unsetError();
            this.$emit('addWorkItemChild', this.search);
          }
        })
        .catch(() => {
          this.error = this.$options.i18n.addChildErrorMessage;
        })
        .finally(() => {
          this.search = '';
        });
    },
    createChild() {
      this.$apollo
        .mutate({
          mutation: createWorkItemMutation,
          variables: {
            input: {
              title: this.search?.title || this.search,
              projectPath: this.projectPath,
              workItemTypeId: this.taskWorkItemType,
              hierarchyWidget: {
                parentId: this.issuableGid,
              },
              confidential: this.parentConfidential,
            },
          },
        })
        .then(({ data }) => {
          if (data.workItemCreate?.errors?.length) {
            [this.error] = data.workItemCreate.errors;
          } else {
            this.unsetError();
            this.$emit('addWorkItemChild', data.workItemCreate.workItem);
            /**
             * call update mutation only when there is an iteration associated with the issue
             */
            // TODO: setting the iteration should be moved to the creation mutation once the backend is done
            if (this.parentIterationId && this.hasIterationsFeature) {
              this.addIterationToWorkItem(data.workItemCreate.workItem.id);
            }
          }
        })
        .catch(() => {
          this.error = this.$options.i18n.createChildErrorMessage;
        })
        .finally(() => {
          this.search = '';
          this.childToCreateTitle = null;
        });
    },
    async addIterationToWorkItem(workItemId) {
      await this.$apollo.mutate({
        mutation: updateWorkItemMutation,
        variables: {
          input: {
            id: workItemId,
            iterationWidget: {
              iterationId: this.parentIterationId,
            },
          },
        },
      });
    },
  },
  i18n: {
    inputLabel: __('Title'),
    addTaskButtonLabel: s__('WorkItem|Add task'),
    addChildErrorMessage: s__(
      'WorkItem|Something went wrong when trying to add a child. Please try again.',
    ),
    createChildOptionLabel: s__('WorkItem|Create task'),
    createChildErrorMessage: s__(
      'WorkItem|Something went wrong when trying to create a child. Please try again.',
    ),
    placeholder: s__('WorkItem|Add a title'),
    fieldValidationMessage: __('Maximum of 255 characters'),
  },
};
</script>

<template>
  <gl-form
    class="gl-bg-white gl-mb-3 gl-p-4 gl-border gl-border-gray-100 gl-rounded-base"
    @submit.prevent="createChild"
  >
    <gl-alert v-if="error" variant="danger" class="gl-mb-3" @dismiss="unsetError">
      {{ error }}
    </gl-alert>
    <!-- Follow up issue to turn this functionality back on https://gitlab.com/gitlab-org/gitlab/-/issues/368757 -->
    <gl-form-combobox
      v-if="false"
      v-model="search"
      :token-list="availableWorkItems"
      match-value-to-attr="title"
      class="gl-mb-4"
      :label-text="$options.i18n.inputLabel"
      :action-list="actionsList"
      label-sr-only
      autofocus
    >
      <template #result="{ item }">
        <div class="gl-display-flex">
          <div class="gl-text-secondary gl-mr-4">{{ getIdFromGraphQLId(item.id) }}</div>
          <div>{{ item.title }}</div>
        </div>
      </template>
      <template #action="{ item }">
        <span class="gl-text-blue-500">{{ item.label }}</span>
      </template>
    </gl-form-combobox>
    <gl-form-group
      :label="$options.i18n.inputLabel"
      :description="$options.i18n.fieldValidationMessage"
    >
      <gl-form-input
        ref="wiTitleInput"
        v-model="search"
        :placeholder="$options.i18n.placeholder"
        maxlength="255"
        class="gl-mb-3"
        autofocus
      />
    </gl-form-group>
    <gl-button
      category="primary"
      variant="confirm"
      size="small"
      type="submit"
      :disabled="search.length === 0"
      data-testid="add-child-button"
      class="gl-mr-2"
    >
      {{ $options.i18n.createChildOptionLabel }}
    </gl-button>
    <gl-button category="secondary" size="small" @click="$emit('cancel')">
      {{ s__('WorkItem|Cancel') }}
    </gl-button>
  </gl-form>
</template>