mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
fixes after upstream changes
This commit is contained in:
@@ -1,72 +1,4 @@
|
||||
import useCommerceSearch from '@commerce/products/use-search'
|
||||
import { getAllProductsQuery } from '@framework/utils/queries'
|
||||
import useSearch, { UseSearch } from '@commerce/products/use-search'
|
||||
import type { ShopifyProvider } from '..'
|
||||
|
||||
import type { Product } from 'framework/bigcommerce/schema'
|
||||
import type { HookFetcher } from '@commerce/utils/types'
|
||||
import type { SwrOptions } from '@commerce/utils/use-data'
|
||||
import type { ProductConnection, ProductEdge } from '@framework/schema'
|
||||
|
||||
import getSearchVariables from '@framework/utils/get-search-variables'
|
||||
|
||||
import { normalizeProduct } from '@framework/lib/normalize'
|
||||
|
||||
export type SearchProductsInput = {
|
||||
search?: string
|
||||
categoryId?: string
|
||||
brandId?: string
|
||||
sort?: string
|
||||
}
|
||||
|
||||
export type SearchRequestProductsData = {
|
||||
products?: ProductEdge[]
|
||||
}
|
||||
|
||||
export type SearchProductsData = {
|
||||
products: Product[]
|
||||
found: boolean
|
||||
}
|
||||
|
||||
export const fetcher: HookFetcher<
|
||||
SearchProductsData,
|
||||
SearchProductsInput
|
||||
> = async (options, input, fetch) => {
|
||||
const resp = await fetch({
|
||||
query: options?.query,
|
||||
method: options?.method,
|
||||
variables: getSearchVariables(input),
|
||||
})
|
||||
const edges = resp.products?.edges
|
||||
return {
|
||||
products: edges?.map(({ node: p }: ProductEdge) => normalizeProduct(p)),
|
||||
found: !!edges?.length,
|
||||
}
|
||||
}
|
||||
|
||||
export function extendHook(
|
||||
customFetcher: typeof fetcher,
|
||||
swrOptions?: SwrOptions<SearchProductsData, SearchProductsInput>
|
||||
) {
|
||||
const useSearch = (input: SearchProductsInput = {}) => {
|
||||
const response = useCommerceSearch(
|
||||
{
|
||||
query: getAllProductsQuery,
|
||||
},
|
||||
[
|
||||
['search', input.search],
|
||||
['categoryId', input.categoryId],
|
||||
['brandId', input.brandId],
|
||||
['sort', input.sort],
|
||||
],
|
||||
customFetcher,
|
||||
{ revalidateOnFocus: false, ...swrOptions }
|
||||
)
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
useSearch.extend = extendHook
|
||||
|
||||
return useSearch
|
||||
}
|
||||
|
||||
export default extendHook(fetcher)
|
||||
export default useSearch as UseSearch<ShopifyProvider>
|
||||
|
Reference in New Issue
Block a user