summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/issues_list/components/issuables_list_app.vue
blob: 62b52afdaca889599aed8af5c263b1f1ee1a7c12 (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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<script>
import {
  GlEmptyState,
  GlPagination,
  GlDeprecatedSkeletonLoading as GlSkeletonLoading,
  GlSafeHtmlDirective as SafeHtml,
} from '@gitlab/ui';
import { toNumber, omit } from 'lodash';
import createFlash from '~/flash';
import axios from '~/lib/utils/axios_utils';
import { scrollToElement, historyPushState } from '~/lib/utils/common_utils';
import { setUrlParams, queryToObject, getParameterByName } from '~/lib/utils/url_utility';
import { __ } from '~/locale';
import initManualOrdering from '~/manual_ordering';
import FilteredSearchBar from '~/vue_shared/components/filtered_search_bar/filtered_search_bar_root.vue';
import {
  sortOrderMap,
  availableSortOptionsJira,
  RELATIVE_POSITION,
  PAGE_SIZE,
  PAGE_SIZE_MANUAL,
  LOADING_LIST_ITEMS_LENGTH,
} from '../constants';
import issueableEventHub from '../eventhub';
import { emptyStateHelper } from '../service_desk_helper';
import Issuable from './issuable.vue';

/**
 * @deprecated Use app/assets/javascripts/issuable_list/components/issuable_list_root.vue instead
 */
export default {
  LOADING_LIST_ITEMS_LENGTH,
  directives: {
    SafeHtml,
  },
  components: {
    GlEmptyState,
    GlPagination,
    GlSkeletonLoading,
    Issuable,
    FilteredSearchBar,
  },
  props: {
    canBulkEdit: {
      type: Boolean,
      required: false,
      default: false,
    },
    emptyStateMeta: {
      type: Object,
      required: true,
    },
    endpoint: {
      type: String,
      required: true,
    },
    projectPath: {
      type: String,
      required: false,
      default: '',
    },
    sortKey: {
      type: String,
      required: false,
      default: '',
    },
    type: {
      type: String,
      required: false,
      default: '',
    },
  },
  data() {
    return {
      availableSortOptionsJira,
      filters: {},
      isBulkEditing: false,
      issuables: [],
      loading: false,
      page: getParameterByName('page') !== null ? toNumber(getParameterByName('page')) : 1,
      selection: {},
      totalItems: 0,
    };
  },
  computed: {
    allIssuablesSelected() {
      // WARNING: Because we are only keeping track of selected values
      // this works, we will need to rethink this if we start tracking
      // [id]: false for not selected values.
      return this.issuables.length === Object.keys(this.selection).length;
    },
    emptyState() {
      if (this.issuables.length) {
        return {}; // Empty state shouldn't be shown here
      }

      if (this.isServiceDesk) {
        return emptyStateHelper(this.emptyStateMeta);
      }

      if (this.hasFilters) {
        return {
          title: __('Sorry, your filter produced no results'),
          svgPath: this.emptyStateMeta.svgPath,
          description: __('To widen your search, change or remove filters above'),
          primaryLink: this.emptyStateMeta.createIssuePath,
          primaryText: __('New issue'),
        };
      }

      if (this.filters.state === 'opened') {
        return {
          title: __('There are no open issues'),
          svgPath: this.emptyStateMeta.svgPath,
          description: __('To keep this project going, create a new issue'),
          primaryLink: this.emptyStateMeta.createIssuePath,
          primaryText: __('New issue'),
        };
      } else if (this.filters.state === 'closed') {
        return {
          title: __('There are no closed issues'),
          svgPath: this.emptyStateMeta.svgPath,
        };
      }

      return {
        title: __('There are no issues to show'),
        svgPath: this.emptyStateMeta.svgPath,
        description: __(
          'The Issue Tracker is the place to add things that need to be improved or solved in a project. You can register or sign in to create issues for this project.',
        ),
      };
    },
    hasFilters() {
      const ignored = ['utf8', 'state', 'scope', 'order_by', 'sort'];
      return Object.keys(omit(this.filters, ignored)).length > 0;
    },
    isManualOrdering() {
      return this.sortKey === RELATIVE_POSITION;
    },
    itemsPerPage() {
      return this.isManualOrdering ? PAGE_SIZE_MANUAL : PAGE_SIZE;
    },
    baseUrl() {
      return window.location.href.replace(/(\?.*)?(#.*)?$/, '');
    },
    paginationNext() {
      return this.page + 1;
    },
    paginationPrev() {
      return this.page - 1;
    },
    paginationProps() {
      const paginationProps = { value: this.page };

      if (this.totalItems) {
        return {
          ...paginationProps,
          perPage: this.itemsPerPage,
          totalItems: this.totalItems,
        };
      }

      return {
        ...paginationProps,
        prevPage: this.paginationPrev,
        nextPage: this.paginationNext,
      };
    },
    isServiceDesk() {
      return this.type === 'service_desk';
    },
    isJira() {
      return this.type === 'jira';
    },
    initialFilterValue() {
      const value = [];
      const { search } = this.getQueryObject();

      if (search) {
        value.push(search);
      }
      return value;
    },
    initialSortBy() {
      const { sort } = this.getQueryObject();
      return sort || 'created_desc';
    },
  },
  watch: {
    selection() {
      // We need to call nextTick here to wait for all of the boxes to be checked and rendered
      // before we query the dom in issuable_bulk_update_actions.js.
      this.$nextTick(() => {
        issueableEventHub.$emit('issuables:updateBulkEdit');
      });
    },
    issuables() {
      this.$nextTick(() => {
        initManualOrdering();
      });
    },
  },
  mounted() {
    if (this.canBulkEdit) {
      this.unsubscribeToggleBulkEdit = issueableEventHub.$on('issuables:toggleBulkEdit', (val) => {
        this.isBulkEditing = val;
      });
    }
    this.fetchIssuables();
  },
  beforeDestroy() {
    // eslint-disable-next-line @gitlab/no-global-event-off
    issueableEventHub.$off('issuables:toggleBulkEdit');
  },
  methods: {
    isSelected(issuableId) {
      return Boolean(this.selection[issuableId]);
    },
    setSelection(ids) {
      ids.forEach((id) => {
        this.select(id, true);
      });
    },
    clearSelection() {
      this.selection = {};
    },
    select(id, isSelect = true) {
      if (isSelect) {
        this.$set(this.selection, id, true);
      } else {
        this.$delete(this.selection, id);
      }
    },
    fetchIssuables(pageToFetch) {
      this.loading = true;

      this.clearSelection();

      this.setFilters();

      return axios
        .get(this.endpoint, {
          params: {
            ...this.filters,

            with_labels_details: true,
            page: pageToFetch || this.page,
            per_page: this.itemsPerPage,
          },
        })
        .then((response) => {
          this.loading = false;
          this.issuables = response.data;
          this.totalItems = Number(response.headers['x-total']);
          this.page = Number(response.headers['x-page']);
        })
        .catch(() => {
          this.loading = false;
          return createFlash({
            message: __('An error occurred while loading issues'),
          });
        });
    },
    getQueryObject() {
      return queryToObject(window.location.search, { gatherArrays: true });
    },
    onPaginate(newPage) {
      if (newPage === this.page) return;

      scrollToElement('#content-body');

      // NOTE: This allows for the params to be updated on pagination
      historyPushState(
        setUrlParams({ ...this.filters, page: newPage }, window.location.href, true),
      );

      this.fetchIssuables(newPage);
    },
    onSelectAll() {
      if (this.allIssuablesSelected) {
        this.selection = {};
      } else {
        this.setSelection(this.issuables.map(({ id }) => id));
      }
    },
    onSelectIssuable({ issuable, selected }) {
      if (!this.canBulkEdit) return;

      this.select(issuable.id, selected);
    },
    setFilters() {
      const {
        label_name: labels,
        milestone_title: milestoneTitle,
        'not[label_name]': excludedLabels,
        'not[milestone_title]': excludedMilestone,
        ...filters
      } = this.getQueryObject();

      // TODO: https://gitlab.com/gitlab-org/gitlab/-/issues/227880

      if (milestoneTitle) {
        filters.milestone = milestoneTitle;
      }
      if (Array.isArray(labels)) {
        filters.labels = labels.join(',');
      }
      if (!filters.state) {
        filters.state = 'opened';
      }

      if (excludedLabels) {
        filters['not[labels]'] = excludedLabels;
      }

      if (excludedMilestone) {
        filters['not[milestone]'] = excludedMilestone;
      }

      Object.assign(filters, sortOrderMap[this.sortKey]);

      this.filters = filters;
    },
    refetchIssuables() {
      const ignored = ['utf8'];
      const params = omit(this.filters, ignored);

      historyPushState(setUrlParams(params, window.location.href, true, true));
      this.fetchIssuables();
    },
    handleFilter(filters) {
      const searchTokens = [];

      filters.forEach((filter) => {
        if (filter.type === 'filtered-search-term') {
          if (filter.value.data) {
            searchTokens.push(filter.value.data);
          }
        }
      });

      if (searchTokens.length) {
        this.filters.search = searchTokens.join(' ');
      }
      this.page = 1;

      this.refetchIssuables();
    },
    handleSort(sort) {
      this.filters.sort = sort;
      this.page = 1;

      this.refetchIssuables();
    },
  },
};
</script>

<template>
  <div>
    <filtered-search-bar
      v-if="isJira"
      :namespace="projectPath"
      :search-input-placeholder="__('Search Jira issues')"
      :tokens="[]"
      :sort-options="availableSortOptionsJira"
      :initial-filter-value="initialFilterValue"
      :initial-sort-by="initialSortBy"
      class="row-content-block"
      @onFilter="handleFilter"
      @onSort="handleSort"
    />
    <ul v-if="loading" class="content-list">
      <li v-for="n in $options.LOADING_LIST_ITEMS_LENGTH" :key="n" class="issue gl-px-5! gl-py-5!">
        <gl-skeleton-loading />
      </li>
    </ul>
    <div v-else-if="issuables.length">
      <div v-if="isBulkEditing" class="issue px-3 py-3 border-bottom border-light">
        <input
          id="check-all-issues"
          type="checkbox"
          :checked="allIssuablesSelected"
          class="mr-2"
          @click="onSelectAll"
        />
        <strong>{{ __('Select all') }}</strong>
      </div>
      <ul
        class="content-list issuable-list issues-list"
        :class="{ 'manual-ordering': isManualOrdering }"
      >
        <issuable
          v-for="issuable in issuables"
          :key="issuable.id"
          class="pr-3"
          :class="{ 'user-can-drag': isManualOrdering }"
          :issuable="issuable"
          :is-bulk-editing="isBulkEditing"
          :selected="isSelected(issuable.id)"
          :base-url="baseUrl"
          @select="onSelectIssuable"
        />
      </ul>
      <div class="mt-3">
        <gl-pagination
          v-bind="paginationProps"
          class="gl-justify-content-center"
          @input="onPaginate"
        />
      </div>
    </div>
    <gl-empty-state
      v-else
      :title="emptyState.title"
      :svg-path="emptyState.svgPath"
      :primary-button-link="emptyState.primaryLink"
      :primary-button-text="emptyState.primaryText"
    >
      <template #description>
        <div v-safe-html="emptyState.description"></div>
      </template>
    </gl-empty-state>
  </div>
</template>