From 62bc848f9179515942218843631906c26941b2df Mon Sep 17 00:00:00 2001
From: varodv <72568818+alvarodE@users.noreply.github.com>
Date: Mon, 6 May 2024 13:32:35 +0200
Subject: [PATCH] feat: Create useFacets composable and use it on ClearFilters
component
---
.../facets/components/clear-filters.vue | 83 +++++++++++--------
.../src/x-modules/facets/composables/index.ts | 1 +
.../composables/use-facets.composable.ts | 76 +++++++++++++++++
.../src/x-modules/facets/index.ts | 1 +
4 files changed, 125 insertions(+), 36 deletions(-)
create mode 100644 packages/x-components/src/x-modules/facets/composables/index.ts
create mode 100644 packages/x-components/src/x-modules/facets/composables/use-facets.composable.ts
diff --git a/packages/x-components/src/x-modules/facets/components/clear-filters.vue b/packages/x-components/src/x-modules/facets/components/clear-filters.vue
index 7ce233b55d..b70b6939fa 100644
--- a/packages/x-components/src/x-modules/facets/components/clear-filters.vue
+++ b/packages/x-components/src/x-modules/facets/components/clear-filters.vue
@@ -12,55 +12,66 @@
diff --git a/packages/x-components/src/x-modules/facets/composables/index.ts b/packages/x-components/src/x-modules/facets/composables/index.ts
new file mode 100644
index 0000000000..6e791ec401
--- /dev/null
+++ b/packages/x-components/src/x-modules/facets/composables/index.ts
@@ -0,0 +1 @@
+export * from './use-facets.composable';
diff --git a/packages/x-components/src/x-modules/facets/composables/use-facets.composable.ts b/packages/x-components/src/x-modules/facets/composables/use-facets.composable.ts
new file mode 100644
index 0000000000..9d5b14c51b
--- /dev/null
+++ b/packages/x-components/src/x-modules/facets/composables/use-facets.composable.ts
@@ -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;
+ /** 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;
+ /** Get the selected filters from store. */
+ selectedFilters: ComputedRef;
+ };
+
+ /**
+ * 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(() => {
+ 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(() => !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(() => alwaysVisible || hasSelectedFilters.value);
+
+ return {
+ selectedFiltersByFacet,
+ selectedFilters,
+ hasSelectedFilters,
+ isVisible
+ };
+}
diff --git a/packages/x-components/src/x-modules/facets/index.ts b/packages/x-components/src/x-modules/facets/index.ts
index 4c2cb38079..6a60ad506d 100644
--- a/packages/x-components/src/x-modules/facets/index.ts
+++ b/packages/x-components/src/x-modules/facets/index.ts
@@ -4,6 +4,7 @@ export * from './wiring';
export * from './service';
export * from './entities';
export * from './components';
+export * from './composables';
export * from './events.types';
export * from './utils';
export { default as FacetsMixin } from './components/facets.mixin';