|
| 1 | +import { Ref, ref, useContext } from '@nuxtjs/composition-api'; |
| 2 | +import { |
| 3 | + AgnosticFacetSearchParams, ComposableFunctionArgs, Logger, ProductsSearchParams } from '@vue-storefront/core'; |
| 4 | +import { FacetSearchResult, UseFacet, UseFacetErrors} from './useFacet'; |
| 5 | +import { GetProductSearchParams } from '@vue-storefront/magento-api/src/types/API'; |
| 6 | + |
| 7 | +const availableSortingOptions = [ |
| 8 | + { |
| 9 | + label: 'Sort: Default', |
| 10 | + value: '', |
| 11 | + }, |
| 12 | + { |
| 13 | + label: 'Sort: Name A-Z', |
| 14 | + value: 'name_ASC', |
| 15 | + }, |
| 16 | + { |
| 17 | + label: 'Sort: Name Z-A', |
| 18 | + value: 'name_DESC', |
| 19 | + }, |
| 20 | + { |
| 21 | + label: 'Sort: Price from low to high', |
| 22 | + value: 'price_ASC', |
| 23 | + }, { |
| 24 | + label: 'Sort: Price from high to low', |
| 25 | + value: 'price_DESC', |
| 26 | + }, |
| 27 | +]; |
| 28 | + |
| 29 | +const constructFilterObject = (inputFilters: Object) => { |
| 30 | + const filter = {}; |
| 31 | + |
| 32 | + Object.keys(inputFilters).forEach((key) => { |
| 33 | + if (key === 'price') { |
| 34 | + const price = { from: 0, to: 0 }; |
| 35 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument |
| 36 | + const flatPrices = inputFilters[key].flatMap((inputFilter) => inputFilter.split('_').map((str) => Number.parseFloat(str))).sort((a, b) => a - b); |
| 37 | + |
| 38 | + [price.from] = flatPrices; |
| 39 | + price.to = flatPrices[flatPrices.length - 1]; |
| 40 | + |
| 41 | + filter[key] = price; |
| 42 | + } else if (typeof inputFilters[key] === 'string') { |
| 43 | + filter[key] = { in: [inputFilters[key]] }; |
| 44 | + } else { |
| 45 | + filter[key] = { in: inputFilters[key] }; |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + return filter; |
| 50 | +}; |
| 51 | + |
| 52 | +const constructSortObject = (sortData: string) => { |
| 53 | + const baseData = sortData.split(/_/gi); |
| 54 | + |
| 55 | + return baseData.length > 0 ? Object.fromEntries([baseData]) : {}; |
| 56 | +}; |
| 57 | + |
| 58 | +export const useFacet = (): UseFacet => { |
| 59 | + const { app } = useContext(); |
| 60 | + const loading: Ref<boolean> = ref(false); |
| 61 | + const result: Ref<FacetSearchResult<any>> = ref({ data: null, input: null }); |
| 62 | + const error: Ref<UseFacetErrors> = ref({ |
| 63 | + search: null, |
| 64 | + }); |
| 65 | + |
| 66 | + const search = async (params?: ComposableFunctionArgs<AgnosticFacetSearchParams>) => { |
| 67 | + Logger.debug('useFacet/search', params); |
| 68 | + |
| 69 | + result.value.input = params; |
| 70 | + try { |
| 71 | + loading.value = true; |
| 72 | + |
| 73 | + const itemsPerPage = (params.itemsPerPage) ? params.itemsPerPage : 20; |
| 74 | + const inputFilters = (params.filters) ? params.filters : {}; |
| 75 | + const categoryId = (params.categoryId) ? { |
| 76 | + category_uid: { |
| 77 | + ...(Array.isArray(params.categoryId) |
| 78 | + ? { in: params.categoryId } |
| 79 | + : { eq: params.categoryId }), |
| 80 | + }, |
| 81 | + } : {}; |
| 82 | + |
| 83 | + const productParams: ProductsSearchParams = { |
| 84 | + filter: { |
| 85 | + ...categoryId, |
| 86 | + ...constructFilterObject({ |
| 87 | + ...inputFilters, |
| 88 | + }), |
| 89 | + }, |
| 90 | + perPage: itemsPerPage, |
| 91 | + offset: (params.page - 1) * itemsPerPage, |
| 92 | + page: params.page, |
| 93 | + search: (params.term) ? params.term : '', |
| 94 | + sort: constructSortObject(params.sort || ''), |
| 95 | + }; |
| 96 | + |
| 97 | + const productSearchParams: GetProductSearchParams = { |
| 98 | + pageSize: productParams.perPage, |
| 99 | + search: productParams.search, |
| 100 | + filter: productParams.filter, |
| 101 | + sort: productParams.sort, |
| 102 | + currentPage: productParams.page, |
| 103 | + }; |
| 104 | + |
| 105 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument |
| 106 | + const { data } = await app.context.$vsf.$magento.api.products(productSearchParams, params?.customQuery || { products: 'products' }); |
| 107 | + |
| 108 | + Logger.debug('[Result]:', { data }); |
| 109 | + |
| 110 | + result.value.data = { |
| 111 | + items: data?.products?.items || [], |
| 112 | + total: data?.products?.total_count, |
| 113 | + availableFilters: data?.products?.aggregations, |
| 114 | + category: { id: params.categoryId }, |
| 115 | + availableSortingOptions, |
| 116 | + perPageOptions: [10, 20, 50], |
| 117 | + itemsPerPage, |
| 118 | + }; |
| 119 | + error.value.search = null; |
| 120 | + } catch (err) { |
| 121 | + error.value.search = err; |
| 122 | + Logger.error(`useFacet/search`, err); |
| 123 | + } finally { |
| 124 | + loading.value = false; |
| 125 | + } |
| 126 | + }; |
| 127 | + |
| 128 | + return { |
| 129 | + result, |
| 130 | + loading, |
| 131 | + error, |
| 132 | + search, |
| 133 | + }; |
| 134 | +}; |
| 135 | + |
| 136 | +export default useFacet; |
0 commit comments