|
| 1 | +<script> |
| 2 | +import FilteredSearchAndSort from '~/groups_projects/components/filtered_search_and_sort.vue'; |
| 3 | +import { |
| 4 | + FILTERED_SEARCH_NAMESPACE, |
| 5 | + FILTERED_SEARCH_TERM_KEY, |
| 6 | + SORT_DIRECTION_ASC, |
| 7 | + SORT_DIRECTION_DESC, |
| 8 | + SORT_OPTION_CREATED_DATE, |
| 9 | + SORT_OPTIONS, |
| 10 | +} from '~/admin/groups/constants'; |
| 11 | +import { RECENT_SEARCHES_STORAGE_KEY_GROUPS } from '~/filtered_search/recent_searches_storage_keys'; |
| 12 | +import { objectToQuery, queryToObject, visitUrl } from '~/lib/utils/url_utility'; |
| 13 | +
|
| 14 | +export default { |
| 15 | + components: { |
| 16 | + FilteredSearchAndSort, |
| 17 | + }, |
| 18 | + computed: { |
| 19 | + defaultSortOption() { |
| 20 | + return SORT_OPTION_CREATED_DATE; |
| 21 | + }, |
| 22 | + defaultSortBy() { |
| 23 | + return `${this.defaultSortOption.value}_${SORT_DIRECTION_DESC}`; |
| 24 | + }, |
| 25 | + queryAsObject() { |
| 26 | + return queryToObject(document.location.search); |
| 27 | + }, |
| 28 | + queryAsObjectWithoutPagination() { |
| 29 | + const { page, ...queryAsObject } = this.queryAsObject; |
| 30 | + return queryAsObject; |
| 31 | + }, |
| 32 | + sortByQuery() { |
| 33 | + return this.queryAsObject.sort; |
| 34 | + }, |
| 35 | + sortBy() { |
| 36 | + return this.sortByQuery || this.defaultSortBy; |
| 37 | + }, |
| 38 | + sortOptions() { |
| 39 | + return SORT_OPTIONS; |
| 40 | + }, |
| 41 | + activeSortOption() { |
| 42 | + return ( |
| 43 | + this.sortOptions.find((option) => this.sortBy.includes(option.value)) || |
| 44 | + this.defaultSortOption |
| 45 | + ); |
| 46 | + }, |
| 47 | + isAscending() { |
| 48 | + return this.sortBy.endsWith(SORT_DIRECTION_ASC); |
| 49 | + }, |
| 50 | + }, |
| 51 | + methods: { |
| 52 | + visitUrlWithQueryObject(queryObject) { |
| 53 | + return visitUrl(`?${objectToQuery(queryObject)}`); |
| 54 | + }, |
| 55 | + onSortChange(sortBy, isAscending) { |
| 56 | + const sort = `${sortBy}_${isAscending ? SORT_DIRECTION_ASC : SORT_DIRECTION_DESC}`; |
| 57 | + this.visitUrlWithQueryObject({ ...this.queryAsObjectWithoutPagination, sort }); |
| 58 | + }, |
| 59 | + onSortDirectionChange(isAscending) { |
| 60 | + this.onSortChange(this.activeSortOption.value, isAscending); |
| 61 | + }, |
| 62 | + onSortByChange(sortBy) { |
| 63 | + this.onSortChange(sortBy, this.isAscending); |
| 64 | + }, |
| 65 | + onFilter(filtersQuery) { |
| 66 | + const queryObject = { ...filtersQuery }; |
| 67 | +
|
| 68 | + if (this.sortByQuery) { |
| 69 | + queryObject.sort = this.sortByQuery; |
| 70 | + } |
| 71 | +
|
| 72 | + this.visitUrlWithQueryObject(queryObject); |
| 73 | + }, |
| 74 | + }, |
| 75 | + filteredSearch: { |
| 76 | + recentSearchesStorageKey: RECENT_SEARCHES_STORAGE_KEY_GROUPS, |
| 77 | + namespace: FILTERED_SEARCH_NAMESPACE, |
| 78 | + termKey: FILTERED_SEARCH_TERM_KEY, |
| 79 | + tokens: [], |
| 80 | + }, |
| 81 | +}; |
| 82 | +</script> |
| 83 | + |
| 84 | +<template> |
| 85 | + <div class="gl-mb-4" data-testid="admin-groups-filtered-search-and-sort"> |
| 86 | + <filtered-search-and-sort |
| 87 | + :filtered-search-namespace="$options.filteredSearch.namespace" |
| 88 | + :filtered-search-tokens="$options.filteredSearch.tokens" |
| 89 | + :filtered-search-term-key="$options.filteredSearch.termKey" |
| 90 | + :filtered-search-recent-searches-storage-key=" |
| 91 | + $options.filteredSearch.recentSearchesStorageKey |
| 92 | + " |
| 93 | + :is-ascending="isAscending" |
| 94 | + :sort-options="sortOptions" |
| 95 | + :active-sort-option="activeSortOption" |
| 96 | + :filtered-search-query="queryAsObject" |
| 97 | + @filter="onFilter" |
| 98 | + @sort-direction-change="onSortDirectionChange" |
| 99 | + @sort-by-change="onSortByChange" |
| 100 | + /> |
| 101 | + </div> |
| 102 | +</template> |
0 commit comments