forked from crowetic/commerce
Added sort option to the UI
This commit is contained in:
parent
398feac176
commit
cdb2cbdebd
@ -12,11 +12,12 @@ export type SearchProductsInput = {
|
||||
search?: string
|
||||
categoryId?: number
|
||||
brandId?: number
|
||||
sort?: string
|
||||
}
|
||||
|
||||
export const fetcher: HookFetcher<SearchProductsData, SearchProductsInput> = (
|
||||
options,
|
||||
{ search, categoryId, brandId },
|
||||
{ search, categoryId, brandId, sort },
|
||||
fetch
|
||||
) => {
|
||||
// Use a dummy base as we only care about the relative path
|
||||
@ -27,6 +28,7 @@ export const fetcher: HookFetcher<SearchProductsData, SearchProductsInput> = (
|
||||
url.searchParams.set('category', String(categoryId))
|
||||
if (Number.isInteger(categoryId))
|
||||
url.searchParams.set('brand', String(brandId))
|
||||
if (sort) url.searchParams.set('sort', sort)
|
||||
|
||||
return fetch({
|
||||
url: url.pathname + url.search,
|
||||
@ -45,6 +47,7 @@ export function extendHook(
|
||||
['search', input.search],
|
||||
['categoryId', input.categoryId],
|
||||
['brandId', input.brandId],
|
||||
['sort', input.sort],
|
||||
],
|
||||
customFetcher,
|
||||
{ revalidateOnFocus: false, ...swrOptions }
|
||||
|
@ -17,7 +17,7 @@ export async function getStaticProps({ preview }: GetStaticPropsContext) {
|
||||
}
|
||||
}
|
||||
|
||||
export default function Home({
|
||||
export default function Search({
|
||||
categories,
|
||||
brands,
|
||||
}: InferGetStaticPropsType<typeof getStaticProps>) {
|
||||
@ -39,6 +39,7 @@ export default function Home({
|
||||
search: typeof q === 'string' ? q : '',
|
||||
categoryId: activeCategory?.entityId,
|
||||
brandId: activeBrand?.entityId,
|
||||
sort: typeof sort === 'string' ? sort : '',
|
||||
})
|
||||
|
||||
return (
|
||||
@ -178,7 +179,7 @@ export default function Home({
|
||||
)
|
||||
}
|
||||
|
||||
Home.Layout = Layout
|
||||
Search.Layout = Layout
|
||||
|
||||
function useSearchMeta(asPath: string) {
|
||||
const [category, setCategory] = useState<string | undefined>()
|
||||
|
Loading…
x
Reference in New Issue
Block a user