mirror of
https://github.com/vercel/commerce.git
synced 2025-06-19 05:31:22 +00:00
Updated useSearch hook
This commit is contained in:
parent
f8d69323bd
commit
0e6372cf54
@ -12,7 +12,7 @@ const LIMIT = 12
|
|||||||
// Return current cart info
|
// Return current cart info
|
||||||
const getProducts: ProductsEndpoint['handlers']['getProducts'] = async ({
|
const getProducts: ProductsEndpoint['handlers']['getProducts'] = async ({
|
||||||
res,
|
res,
|
||||||
body: { search, category, brand, sort },
|
body: { search, categoryId, brandId, sort },
|
||||||
config,
|
config,
|
||||||
commerce,
|
commerce,
|
||||||
}) => {
|
}) => {
|
||||||
@ -24,11 +24,11 @@ const getProducts: ProductsEndpoint['handlers']['getProducts'] = async ({
|
|||||||
|
|
||||||
if (search) url.searchParams.set('keyword', search)
|
if (search) url.searchParams.set('keyword', search)
|
||||||
|
|
||||||
if (category && Number.isInteger(Number(category)))
|
if (categoryId && Number.isInteger(Number(categoryId)))
|
||||||
url.searchParams.set('categories:in', category)
|
url.searchParams.set('categories:in', categoryId)
|
||||||
|
|
||||||
if (brand && Number.isInteger(Number(brand)))
|
if (brandId && Number.isInteger(Number(brandId)))
|
||||||
url.searchParams.set('brand_id', brand)
|
url.searchParams.set('brand_id', brandId)
|
||||||
|
|
||||||
if (sort) {
|
if (sort) {
|
||||||
const [_sort, direction] = sort.split('-')
|
const [_sort, direction] = sort.split('-')
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { SWRHook } from '@commerce/utils/types'
|
import { SWRHook } from '@commerce/utils/types'
|
||||||
import useSearch, { UseSearch } from '@commerce/product/use-search'
|
import useSearch, { UseSearch } from '@commerce/product/use-search'
|
||||||
import type { SearchProductsData } from '../api/catalog/products'
|
import type { SearchProductsHook } from '../types/product'
|
||||||
|
|
||||||
export default useSearch as UseSearch<typeof handler>
|
export default useSearch as UseSearch<typeof handler>
|
||||||
|
|
||||||
@ -11,13 +11,9 @@ export type SearchProductsInput = {
|
|||||||
sort?: string
|
sort?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export const handler: SWRHook<
|
export const handler: SWRHook<SearchProductsHook> = {
|
||||||
SearchProductsData,
|
|
||||||
SearchProductsInput,
|
|
||||||
SearchProductsInput
|
|
||||||
> = {
|
|
||||||
fetchOptions: {
|
fetchOptions: {
|
||||||
url: '/api/bigcommerce/catalog/products',
|
url: '/api/catalog/products',
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
},
|
},
|
||||||
fetcher({ input: { search, categoryId, brandId, sort }, options, fetch }) {
|
fetcher({ input: { search, categoryId, brandId, sort }, options, fetch }) {
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
import { useHook, useSWRHook } from '../utils/use-hook'
|
import { useHook, useSWRHook } from '../utils/use-hook'
|
||||||
import { SWRFetcher } from '../utils/default-fetcher'
|
import { SWRFetcher } from '../utils/default-fetcher'
|
||||||
import type { HookFetcherFn, SWRHook } from '../utils/types'
|
import type { HookFetcherFn, SWRHook } from '../utils/types'
|
||||||
import type { SearchProductsData } from '../types'
|
import type { SearchProductsHook } from '../types/product'
|
||||||
import { Provider } from '..'
|
import type { Provider } from '..'
|
||||||
|
|
||||||
export type UseSearch<
|
export type UseSearch<
|
||||||
H extends SWRHook<any, any, any> = SWRHook<SearchProductsData>
|
H extends SWRHook<any> = SWRHook<SearchProductsHook>
|
||||||
> = ReturnType<H['useHook']>
|
> = ReturnType<H['useHook']>
|
||||||
|
|
||||||
export const fetcher: HookFetcherFn<SearchProductsData, any> = SWRFetcher
|
export const fetcher: HookFetcherFn<SearchProductsHook> = SWRFetcher
|
||||||
|
|
||||||
const fn = (provider: Provider) => provider.products?.useSearch!
|
const fn = (provider: Provider) => provider.products?.useSearch!
|
||||||
|
|
||||||
|
@ -42,8 +42,23 @@ export type Product = {
|
|||||||
options: ProductOption[]
|
options: ProductOption[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type SearchProductsBody = {
|
||||||
|
search?: string
|
||||||
|
categoryId?: string
|
||||||
|
brandId?: string
|
||||||
|
sort?: string
|
||||||
|
}
|
||||||
|
|
||||||
export type ProductTypes = {
|
export type ProductTypes = {
|
||||||
product: Product
|
product: Product
|
||||||
|
searchBody: SearchProductsBody
|
||||||
|
}
|
||||||
|
|
||||||
|
export type SearchProductsHook<T extends ProductTypes = ProductTypes> = {
|
||||||
|
data: T['product'][]
|
||||||
|
body: T['searchBody']
|
||||||
|
input: T['searchBody']
|
||||||
|
fetchInput: T['searchBody']
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ProductsSchema<T extends ProductTypes = ProductTypes> = {
|
export type ProductsSchema<T extends ProductTypes = ProductTypes> = {
|
||||||
@ -55,12 +70,7 @@ export type ProductsSchema<T extends ProductTypes = ProductTypes> = {
|
|||||||
products: T['product'][]
|
products: T['product'][]
|
||||||
found: boolean
|
found: boolean
|
||||||
}
|
}
|
||||||
body: {
|
body: SearchProductsHook<T>['body']
|
||||||
search?: string
|
|
||||||
category?: string
|
|
||||||
brand?: string
|
|
||||||
sort?: string
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user