2020-12-03 06:09:47 +00:00
|
|
|
<script>
|
|
|
|
import { GlFilteredSearchToken } from '@gitlab/ui';
|
2021-02-14 18:09:20 +00:00
|
|
|
import { mapState } from 'vuex';
|
2021-04-23 12:09:52 +00:00
|
|
|
import { getParameterByName, urlParamsToObject } from '~/lib/utils/common_utils';
|
|
|
|
import { setUrlParams } from '~/lib/utils/url_utility';
|
2020-12-03 06:09:47 +00:00
|
|
|
import { s__ } from '~/locale';
|
|
|
|
import { SEARCH_TOKEN_TYPE, SORT_PARAM } from '~/members/constants';
|
2021-02-14 18:09:20 +00:00
|
|
|
import FilteredSearchBar from '~/vue_shared/components/filtered_search_bar/filtered_search_bar_root.vue';
|
2020-12-03 06:09:47 +00:00
|
|
|
|
|
|
|
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') },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
2021-04-13 21:11:25 +00:00
|
|
|
inject: ['namespace', 'sourceId', 'canManageMembers'],
|
2020-12-03 06:09:47 +00:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
initialFilterValue: [],
|
|
|
|
};
|
|
|
|
},
|
|
|
|
computed: {
|
2021-04-13 21:11:25 +00:00
|
|
|
...mapState({
|
|
|
|
filteredSearchBar(state) {
|
|
|
|
return state[this.namespace].filteredSearchBar;
|
|
|
|
},
|
|
|
|
}),
|
2020-12-03 06:09:47 +00:00
|
|
|
tokens() {
|
2020-12-23 21:10:24 +00:00
|
|
|
return this.$options.availableTokens.filter((token) => {
|
2020-12-03 06:09:47 +00:00
|
|
|
if (
|
|
|
|
Object.prototype.hasOwnProperty.call(token, 'requiredPermissions') &&
|
|
|
|
!this[token.requiredPermissions]
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.filteredSearchBar.tokens?.includes(token.type);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
created() {
|
2021-04-23 12:09:52 +00:00
|
|
|
const query = urlParamsToObject(window.location.search);
|
2020-12-03 06:09:47 +00:00
|
|
|
|
|
|
|
const tokens = this.tokens
|
2020-12-23 21:10:24 +00:00
|
|
|
.filter((token) => query[token.type])
|
|
|
|
.map((token) => ({
|
2020-12-03 06:09:47 +00:00
|
|
|
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 !== '') {
|
2021-04-23 12:09:52 +00:00
|
|
|
const { searchParam } = this.filteredSearchBar;
|
|
|
|
const { [searchParam]: searchParamValue } = accumulator;
|
|
|
|
|
2020-12-03 06:09:47 +00:00
|
|
|
return {
|
|
|
|
...accumulator,
|
2021-04-23 12:09:52 +00:00
|
|
|
[searchParam]: searchParamValue ? `${searchParamValue} ${value.data}` : value.data,
|
2020-12-03 06:09:47 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
} 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>
|