-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create useFacets composable and use it on ClearFilters component
- Loading branch information
Showing
4 changed files
with
125 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/x-components/src/x-modules/facets/composables/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './use-facets.composable'; |
76 changes: 76 additions & 0 deletions
76
packages/x-components/src/x-modules/facets/composables/use-facets.composable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { Facet, Filter } from '@empathyco/x-types'; | ||
import { computed, ComputedRef } from 'vue'; | ||
import { useGetter } from '../../../composables/use-getter'; | ||
import { isArrayEmpty } from '../../../utils/array'; | ||
import { FiltersByFacet } from '../store/types'; | ||
|
||
/** | ||
* Composable to share Facets logic. | ||
* | ||
* @param params - Composable params. | ||
* @returns Composable. | ||
* | ||
* @public | ||
*/ | ||
export function useFacets({ | ||
facetsIds, | ||
alwaysVisible = false | ||
}: { | ||
/** Array of facets ids used to get the selected filters for those facets. */ | ||
facetsIds?: Array<Facet['id']>; | ||
/** Flag to render the component even if there are no filters selected. */ | ||
alwaysVisible?: boolean; | ||
}) { | ||
const { selectedFiltersByFacet, selectedFilters: selectedFiltersGetter } = useGetter('facets', [ | ||
'selectedFiltersByFacet', | ||
'selectedFilters' | ||
]) as { | ||
/** Dictionary of filters {@link FiltersByFacet} filtered by facet id. */ | ||
selectedFiltersByFacet: ComputedRef<FiltersByFacet>; | ||
/** Get the selected filters from store. */ | ||
selectedFilters: ComputedRef<Filter[]>; | ||
}; | ||
|
||
/** | ||
* Get selected filters. | ||
* If there are facets ids, get selected filters whose facet id match with some of facets ids. | ||
* If there aren't facets ids, get selected filters. | ||
* | ||
* @returns Array of selected filters depends on there are facets ids or not. | ||
*/ | ||
const selectedFilters = computed<Filter[]>(() => { | ||
if (facetsIds) { | ||
return (facetsIds as string[]).reduce( | ||
(selectedFilters, facetId) => [ | ||
...selectedFilters, | ||
...selectedFiltersByFacet.value[facetId] | ||
], | ||
[] as Filter[] | ||
); | ||
} | ||
|
||
return selectedFiltersGetter.value; | ||
}); | ||
|
||
/** | ||
* Check if there are selected filters. | ||
* | ||
* @returns True or false depends on if there are selected filters. | ||
*/ | ||
const hasSelectedFilters = computed<boolean>(() => !isArrayEmpty(selectedFilters.value)); | ||
|
||
/** | ||
* Flag representing if the component should be visible/rendered or not. | ||
* | ||
* @returns True whenever alwaysVisible is true or has selected filters. False | ||
* otherwise. | ||
*/ | ||
const isVisible = computed<boolean>(() => alwaysVisible || hasSelectedFilters.value); | ||
|
||
return { | ||
selectedFiltersByFacet, | ||
selectedFilters, | ||
hasSelectedFilters, | ||
isVisible | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters