mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
Implement Shopify Provider
This commit is contained in:
86
framework/shopify/product/use-search.tsx
Normal file
86
framework/shopify/product/use-search.tsx
Normal file
@@ -0,0 +1,86 @@
|
||||
import useCommerceSearch from '@commerce/products/use-search'
|
||||
|
||||
import toCommerceProducts from '@framework/utils/to-commerce-products'
|
||||
import getAllProductsQuery from '@framework/utils/queries/get-all-products-query'
|
||||
|
||||
import type { Product } from 'framework/bigcommerce/schema'
|
||||
import type { HookFetcher } from '@commerce/utils/types'
|
||||
import type { SwrOptions } from '@commerce/utils/use-data'
|
||||
import type { ProductEdge } from '@framework/schema'
|
||||
|
||||
import {
|
||||
searchByProductType,
|
||||
searchByTag,
|
||||
} from '@framework/utils/get-search-variables'
|
||||
|
||||
import sortBy from '@framework/utils/get-sort-variables'
|
||||
|
||||
export type CommerceProductEdge = {
|
||||
node: Product
|
||||
}
|
||||
|
||||
export type SearchProductsInput = {
|
||||
search?: string
|
||||
categoryPath?: string
|
||||
sort?: string
|
||||
}
|
||||
|
||||
export type SearchRequestProductsData = {
|
||||
products?: ProductEdge[]
|
||||
}
|
||||
|
||||
export type SearchProductsData = {
|
||||
products: Product[]
|
||||
found: boolean
|
||||
}
|
||||
|
||||
export const fetcher: HookFetcher<
|
||||
SearchRequestProductsData,
|
||||
SearchProductsInput
|
||||
> = (options, { search, categoryPath, sort }, fetch) => {
|
||||
return fetch({
|
||||
query: options?.query,
|
||||
method: options?.method,
|
||||
variables: {
|
||||
...searchByProductType(search),
|
||||
...searchByTag(categoryPath),
|
||||
...sortBy(sort),
|
||||
},
|
||||
}).then(
|
||||
({ products }): SearchProductsData => {
|
||||
return {
|
||||
products: toCommerceProducts(products.edges),
|
||||
found: !!products.edges.length,
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
export function extendHook(
|
||||
customFetcher: typeof fetcher,
|
||||
swrOptions?: SwrOptions<SearchProductsData, SearchProductsInput>
|
||||
) {
|
||||
const useSearch = (input: SearchProductsInput = {}) => {
|
||||
const response = useCommerceSearch(
|
||||
{
|
||||
query: getAllProductsQuery,
|
||||
method: 'POST',
|
||||
},
|
||||
[
|
||||
['search', input.search],
|
||||
['categoryPath', input.categoryPath],
|
||||
['sort', input.sort],
|
||||
],
|
||||
customFetcher,
|
||||
{ revalidateOnFocus: false, ...swrOptions }
|
||||
)
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
useSearch.extend = extendHook
|
||||
|
||||
return useSearch
|
||||
}
|
||||
|
||||
export default extendHook(fetcher)
|
Reference in New Issue
Block a user