summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/members/components/filter_sort/sort_dropdown.vue
blob: de7fbc4241cff8157e59f2ae97a26e2cdd72a13b (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
<script>
import { mapState } from 'vuex';
import { GlSorting, GlSortingItem } from '@gitlab/ui';
import { visitUrl } from '~/lib/utils/url_utility';
import { parseSortParam, buildSortHref } from '~/members/utils';
import { FIELDS } from '~/members/constants';

export default {
  name: 'SortDropdown',
  components: { GlSorting, GlSortingItem },
  computed: {
    ...mapState(['tableSortableFields', 'filteredSearchBar']),
    sort() {
      return parseSortParam(this.tableSortableFields);
    },
    activeOption() {
      return FIELDS.find(field => field.key === this.sort.sortByKey);
    },
    activeOptionLabel() {
      return this.activeOption?.label;
    },
    isAscending() {
      return !this.sort.sortDesc;
    },
    filteredOptions() {
      return FIELDS.filter(field => this.tableSortableFields.includes(field.key) && field.sort).map(
        field => ({
          key: field.key,
          label: field.label,
          href: buildSortHref({
            sortBy: field.key,
            sortDesc: false,
            filteredSearchBarTokens: this.filteredSearchBar.tokens,
            filteredSearchBarSearchParam: this.filteredSearchBar.searchParam,
          }),
        }),
      );
    },
  },
  methods: {
    isActive(key) {
      return this.activeOption.key === key;
    },
    handleSortDirectionChange() {
      visitUrl(
        buildSortHref({
          sortBy: this.activeOption.key,
          sortDesc: !this.sort.sortDesc,
          filteredSearchBarTokens: this.filteredSearchBar.tokens,
          filteredSearchBarSearchParam: this.filteredSearchBar.searchParam,
        }),
      );
    },
  },
};
</script>

<template>
  <gl-sorting
    class="gl-display-flex"
    dropdown-class="gl-w-full"
    data-testid="members-sort-dropdown"
    :text="activeOptionLabel"
    :is-ascending="isAscending"
    :sort-direction-tool-tip="__('Sort direction')"
    @sortDirectionChange="handleSortDirectionChange"
  >
    <gl-sorting-item
      v-for="option in filteredOptions"
      :key="option.key"
      :href="option.href"
      :active="isActive(option.key)"
    >
      {{ option.label }}
    </gl-sorting-item>
  </gl-sorting>
</template>