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

sort_discussion.vue « components « notes « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 60b531d75970b2fa26713399abb7ca7dcbae3014 (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
gs
<script>
import { GlIcon } from '@gitlab/ui';
import { mapActions, mapGetters } from 'vuex';
import { __ } from '~/locale';
import LocalStorageSync from '~/vue_shared/components/local_storage_sync.vue';
import Tracking from '~/tracking';
import { ASC, DESC } from '../constants';

const SORT_OPTIONS = [
  { key: DESC, text: __('Newest first'), cls: 'js-newest-first' },
  { key: ASC, text: __('Oldest first'), cls: 'js-oldest-first' },
];

export default {
  SORT_OPTIONS,
  components: {
    GlIcon,
    LocalStorageSync,
  },
  mixins: [Tracking.mixin()],
  computed: {
    ...mapGetters(['sortDirection', 'noteableType']),
    selectedOption() {
      return SORT_OPTIONS.find(({ key }) => this.sortDirection === key);
    },
    dropdownText() {
      return this.selectedOption.text;
    },
    storageKey() {
      return `sort_direction_${this.noteableType.toLowerCase()}`;
    },
  },
  methods: {
    ...mapActions(['setDiscussionSortDirection']),
    fetchSortedDiscussions(direction) {
      if (this.isDropdownItemActive(direction)) {
        return;
      }

      this.setDiscussionSortDirection(direction);
      this.track('change_discussion_sort_direction', { property: direction });
    },
    isDropdownItemActive(sortDir) {
      return sortDir === this.sortDirection;
    },
  },
};
</script>

<template>
  <div
    data-testid="sort-discussion-filter"
    class="gl-mr-2 gl-display-inline-block gl-vertical-align-bottom full-width-mobile"
  >
    <local-storage-sync
      :value="sortDirection"
      :storage-key="storageKey"
      @input="setDiscussionSortDirection"
    />
    <button class="btn btn-sm js-dropdown-text" data-toggle="dropdown" aria-expanded="false">
      {{ dropdownText }}
      <gl-icon name="chevron-down" />
    </button>
    <div ref="dropdownMenu" class="dropdown-menu dropdown-menu-selectable dropdown-menu-right">
      <div class="dropdown-content">
        <ul>
          <li v-for="{ text, key, cls } in $options.SORT_OPTIONS" :key="key">
            <button
              :class="[cls, { 'is-active': isDropdownItemActive(key) }]"
              type="button"
              @click="fetchSortedDiscussions(key)"
            >
              {{ text }}
            </button>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>