summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/releases/components/releases_sort.vue
blob: d4210dad19c12b02d4935e77e9ae34a0dcf1dbf0 (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
<script>
import { GlSorting, GlSortingItem } from '@gitlab/ui';
import { mapState, mapActions } from 'vuex';
import { ASCENDING_ORDER, DESCENDING_ORDER, SORT_OPTIONS } from '../constants';

export default {
  name: 'ReleasesSort',
  components: {
    GlSorting,
    GlSortingItem,
  },
  computed: {
    ...mapState('index', {
      orderBy: (state) => state.sorting.orderBy,
      sort: (state) => state.sorting.sort,
    }),
    sortOptions() {
      return SORT_OPTIONS;
    },
    sortText() {
      const option = this.sortOptions.find((s) => s.orderBy === this.orderBy);
      return option.label;
    },
    isSortAscending() {
      return this.sort === ASCENDING_ORDER;
    },
  },
  methods: {
    ...mapActions('index', ['setSorting']),
    onDirectionChange() {
      const sort = this.isSortAscending ? DESCENDING_ORDER : ASCENDING_ORDER;
      this.setSorting({ sort });
      this.$emit('sort:changed');
    },
    onSortItemClick(item) {
      this.setSorting({ orderBy: item });
      this.$emit('sort:changed');
    },
    isActiveSortItem(item) {
      return this.orderBy === item;
    },
  },
};
</script>

<template>
  <gl-sorting
    :text="sortText"
    :is-ascending="isSortAscending"
    data-testid="releases-sort"
    @sortDirectionChange="onDirectionChange"
  >
    <gl-sorting-item
      v-for="item in sortOptions"
      :key="item.orderBy"
      :active="isActiveSortItem(item.orderBy)"
      @click="onSortItemClick(item.orderBy)"
    >
      {{ item.label }}
    </gl-sorting-item>
  </gl-sorting>
</template>