UNPKG

@empathyco/x-components

Version:
39 lines (36 loc) 1.68 kB
import { isHierarchicalFacet } from '@empathyco/x-types'; import { createFetchAndSaveActions } from '../../../../store/utils/fetch-and-save-action.utils.js'; import { applyHierarchicalSelection, flattenAllFilters } from '../../utils.js'; const { fetchAndSave, cancelPrevious } = createFetchAndSaveActions({ async fetch({ dispatch, getters }) { return getters.request ? dispatch('fetchFacetsResponse', getters.request) : Promise.resolve(null); }, onSuccess({ commit, getters }, response) { if (response !== null) { const selectedFilters = getters.selectedFilters; const selectedIds = new Set(Object.values(selectedFilters ?? {}) .filter((f) => f.selected) .map((f) => f.id)); const facetsWithSelectedFilters = []; response.facets?.forEach((facet) => { if (isHierarchicalFacet(facet)) { applyHierarchicalSelection(facet.filters, selectedIds); } else { facet.filters.forEach((filter) => { filter.selected = selectedIds.has(filter.id); }); } facetsWithSelectedFilters.push(facet); commit('setFacet', facet); }); commit('setFilters', flattenAllFilters(facetsWithSelectedFilters)); } }, }); const fetchAndSaveFacetsResponse = fetchAndSave; const cancelFetchAndSaveFacetsResponse = cancelPrevious; export { cancelFetchAndSaveFacetsResponse, fetchAndSaveFacetsResponse }; //# sourceMappingURL=fetch-and-save-facets-response.action.js.map