summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/members/components/filter_sort/members_filtered_search_bar.vue
blob: c1df0b9423400f6b4ce42eba942709705c3102a1 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
<script>
import { mapState } from 'vuex';
import { GlFilteredSearchToken } from '@gitlab/ui';
import { setUrlParams, queryToObject } from '~/lib/utils/url_utility';
import { getParameterByName } from '~/lib/utils/common_utils';
import { s__ } from '~/locale';
import FilteredSearchBar from '~/vue_shared/components/filtered_search_bar/filtered_search_bar_root.vue';
import { SEARCH_TOKEN_TYPE, SORT_PARAM } from '~/members/constants';

export default {
  name: 'MembersFilteredSearchBar',
  components: { FilteredSearchBar },
  availableTokens: [
    {
      type: 'two_factor',
      icon: 'lock',
      title: s__('Members|2FA'),
      token: GlFilteredSearchToken,
      unique: true,
      operators: [{ value: '=', description: 'is' }],
      options: [
        { value: 'enabled', title: s__('Members|Enabled') },
        { value: 'disabled', title: s__('Members|Disabled') },
      ],
      requiredPermissions: 'canManageMembers',
    },
    {
      type: 'with_inherited_permissions',
      icon: 'group',
      title: s__('Members|Membership'),
      token: GlFilteredSearchToken,
      unique: true,
      operators: [{ value: '=', description: 'is' }],
      options: [
        { value: 'exclude', title: s__('Members|Direct') },
        { value: 'only', title: s__('Members|Inherited') },
      ],
    },
  ],
  data() {
    return {
      initialFilterValue: [],
    };
  },
  computed: {
    ...mapState(['sourceId', 'filteredSearchBar', 'canManageMembers']),
    tokens() {
      return this.$options.availableTokens.filter(token => {
        if (
          Object.prototype.hasOwnProperty.call(token, 'requiredPermissions') &&
          !this[token.requiredPermissions]
        ) {
          return false;
        }

        return this.filteredSearchBar.tokens?.includes(token.type);
      });
    },
  },
  created() {
    const query = queryToObject(window.location.search);

    const tokens = this.tokens
      .filter(token => query[token.type])
      .map(token => ({
        type: token.type,
        value: {
          data: query[token.type],
          operator: '=',
        },
      }));

    if (query[this.filteredSearchBar.searchParam]) {
      tokens.push({
        type: SEARCH_TOKEN_TYPE,
        value: {
          data: query[this.filteredSearchBar.searchParam],
        },
      });
    }

    this.initialFilterValue = tokens;
  },
  methods: {
    handleFilter(tokens) {
      const params = tokens.reduce((accumulator, token) => {
        const { type, value } = token;

        if (!type || !value) {
          return accumulator;
        }

        if (type === SEARCH_TOKEN_TYPE) {
          if (value.data !== '') {
            return {
              ...accumulator,
              [this.filteredSearchBar.searchParam]: value.data,
            };
          }
        } else {
          return {
            ...accumulator,
            [type]: value.data,
          };
        }

        return accumulator;
      }, {});

      const sortParam = getParameterByName(SORT_PARAM);

      window.location.href = setUrlParams(
        { ...params, ...(sortParam && { sort: sortParam }) },
        window.location.href,
        true,
      );
    },
  },
};
</script>

<template>
  <filtered-search-bar
    :namespace="sourceId.toString()"
    :tokens="tokens"
    :recent-searches-storage-key="filteredSearchBar.recentSearchesStorageKey"
    :search-input-placeholder="filteredSearchBar.placeholder"
    :initial-filter-value="initialFilterValue"
    data-testid="members-filtered-search-bar"
    @onFilter="handleFilter"
  />
</template>