summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/work_items/components/notes/activity_filter.vue
blob: 6d5535797ef8cb74401750f24d51d7017d505655 (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
<script>
import { GlDropdown, GlDropdownItem } from '@gitlab/ui';
import { s__ } from '~/locale';
import Tracking from '~/tracking';
import LocalStorageSync from '~/vue_shared/components/local_storage_sync.vue';
import {
  WORK_ITEM_NOTES_FILTER_ALL_NOTES,
  WORK_ITEM_NOTES_FILTER_ONLY_COMMENTS,
  WORK_ITEM_NOTES_FILTER_ONLY_HISTORY,
  TRACKING_CATEGORY_SHOW,
  WORK_ITEM_NOTES_FILTER_KEY,
} from '~/work_items/constants';

const filterOptions = [
  {
    key: WORK_ITEM_NOTES_FILTER_ALL_NOTES,
    text: s__('WorkItem|All activity'),
  },
  {
    key: WORK_ITEM_NOTES_FILTER_ONLY_COMMENTS,
    text: s__('WorkItem|Comments only'),
    testid: 'comments-activity',
  },
  {
    key: WORK_ITEM_NOTES_FILTER_ONLY_HISTORY,
    text: s__('WorkItem|History only'),
    testid: 'history-activity',
  },
];

export default {
  filterOptions,
  components: {
    GlDropdown,
    GlDropdownItem,
    LocalStorageSync,
  },
  mixins: [Tracking.mixin()],
  props: {
    loading: {
      type: Boolean,
      default: false,
      required: false,
    },
    workItemType: {
      type: String,
      required: true,
    },
    discussionFilter: {
      type: String,
      default: WORK_ITEM_NOTES_FILTER_ALL_NOTES,
      required: false,
    },
  },
  computed: {
    tracking() {
      return {
        category: TRACKING_CATEGORY_SHOW,
        label: 'item_track_notes_filtering',
        property: `type_${this.workItemType}`,
      };
    },
    getDropdownSelectedText() {
      return this.selectedSortOption.text;
    },
    selectedSortOption() {
      return (
        filterOptions.find(({ key }) => this.discussionFilter === key) ||
        WORK_ITEM_NOTES_FILTER_ALL_NOTES
      );
    },
  },
  methods: {
    setDiscussionFilterOption(filterValue) {
      this.$emit('changeFilter', filterValue);
    },
    fetchFilteredDiscussions(filterValue) {
      if (this.isSortDropdownItemActive(filterValue)) {
        return;
      }
      this.track('work_item_notes_filter_changed');
      this.$emit('changeFilter', filterValue);
    },
    isSortDropdownItemActive(discussionFilter) {
      return discussionFilter === this.discussionFilter;
    },
  },
  WORK_ITEM_NOTES_FILTER_KEY,
};
</script>

<template>
  <div class="gl-display-inline-block gl-vertical-align-bottom">
    <local-storage-sync
      :value="discussionFilter"
      :storage-key="$options.WORK_ITEM_NOTES_FILTER_KEY"
      as-string
      @input="setDiscussionFilterOption"
    />
    <gl-dropdown
      class="gl-xs-w-full"
      size="small"
      :text="getDropdownSelectedText"
      :disabled="loading"
      right
    >
      <gl-dropdown-item
        v-for="{ text, key, testid } in $options.filterOptions"
        :key="text"
        :data-testid="testid"
        is-check-item
        :is-checked="isSortDropdownItemActive(key)"
        @click="fetchFilteredDiscussions(key)"
      >
        {{ text }}
      </gl-dropdown-item>
    </gl-dropdown>
  </div>
</template>