summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/releases/components/releases_sort_apollo_client.vue
blob: 7257b34bbf6ec3d88b1fd22dc8a558cf9a041d30 (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
<script>
import { GlSorting, GlSortingItem } from '@gitlab/ui';
import {
  ASCENDING_ORDER,
  DESCENDING_ORDER,
  SORT_OPTIONS,
  RELEASED_AT,
  CREATED_AT,
  RELEASED_AT_ASC,
  RELEASED_AT_DESC,
  CREATED_ASC,
  ALL_SORTS,
  SORT_MAP,
} from '../constants';

export default {
  name: 'ReleasesSortApolloclient',
  components: {
    GlSorting,
    GlSortingItem,
  },
  props: {
    value: {
      type: String,
      required: true,
      validator: (sort) => ALL_SORTS.includes(sort),
    },
  },
  computed: {
    orderBy() {
      if (this.value === RELEASED_AT_ASC || this.value === RELEASED_AT_DESC) {
        return RELEASED_AT;
      }

      return CREATED_AT;
    },
    direction() {
      if (this.value === RELEASED_AT_ASC || this.value === CREATED_ASC) {
        return ASCENDING_ORDER;
      }

      return DESCENDING_ORDER;
    },
    sortOptions() {
      return SORT_OPTIONS;
    },
    sortText() {
      return this.sortOptions.find((s) => s.orderBy === this.orderBy).label;
    },
    isDirectionAscending() {
      return this.direction === ASCENDING_ORDER;
    },
  },
  methods: {
    onDirectionChange() {
      const direction = this.isDirectionAscending ? DESCENDING_ORDER : ASCENDING_ORDER;
      this.emitInputEventIfChanged(this.orderBy, direction);
    },
    onSortItemClick(item) {
      this.emitInputEventIfChanged(item.orderBy, this.direction);
    },
    isActiveSortItem(item) {
      return this.orderBy === item.orderBy;
    },
    emitInputEventIfChanged(orderBy, direction) {
      const newSort = SORT_MAP[orderBy][direction];
      if (newSort !== this.value) {
        this.$emit('input', SORT_MAP[orderBy][direction]);
      }
    },
  },
};
</script>

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