mirror of
https://github.com/vercel/commerce.git
synced 2025-07-23 04:36:49 +00:00
fix reset search on filters
Signed-off-by: Chloe <pinkcloudvnn@gmail.com>
This commit is contained in:
24
lib/utils.ts
24
lib/utils.ts
@@ -1,6 +1,7 @@
|
||||
import clsx, { ClassValue } from 'clsx';
|
||||
import { ReadonlyURLSearchParams } from 'next/navigation';
|
||||
import { twMerge } from 'tailwind-merge';
|
||||
import { MAKE_FILTER_ID, MODEL_FILTER_ID, YEAR_FILTER_ID } from './constants';
|
||||
import { Menu, Product, ProductVariant } from './shopify/types';
|
||||
|
||||
export function cx(...args: ClassValue[]) {
|
||||
@@ -161,3 +162,26 @@ export const getSelectedProductVariant = ({
|
||||
|
||||
return variant || product.variants[0];
|
||||
};
|
||||
|
||||
const YMM_KEYS = [MAKE_FILTER_ID, YEAR_FILTER_ID, MODEL_FILTER_ID];
|
||||
|
||||
export const getInitialSearchParams = (searchParams: URLSearchParams) => {
|
||||
const { q, sort, collection, ...rest } = Object.fromEntries(searchParams);
|
||||
|
||||
const initialFilters: { [key: string]: string } = {
|
||||
...(q && { q }),
|
||||
...(sort && { sort }),
|
||||
...(collection && { collection })
|
||||
};
|
||||
|
||||
Object.keys(rest)
|
||||
.filter((key) => YMM_KEYS.includes(key))
|
||||
.forEach((key) => {
|
||||
const value = searchParams.get(key);
|
||||
if (value) {
|
||||
initialFilters[key] = value;
|
||||
}
|
||||
});
|
||||
|
||||
return initialFilters;
|
||||
};
|
||||
|
Reference in New Issue
Block a user