forked from crowetic/commerce
* Initial work, copied from the Shopify provider * Added basis setup and type generation for the products queries * refactor: adjust the types * task: relax the Node.js constraint * fix: page/product properties * disable unknown fields * mention Saleor in the README * setup debugging for Next.js * Check nextjs-commerce bug if no images are added for a product * fix: client/server pecularities for env visibility Must prefix with `NEXT_PUBLIC_` so that the API URL is visible on the client * re: make search work with Saleor API (WIP) * task: update deps * task: move to Webpack 5.x * saleor: initial cart integration * update deps * saleor: shall the cart appear! * task: remove deprecated packages * saleor: adding/removing from the cart * saleor: preliminary signup process * saleor: fix the prices in the cart * update deps * update deps * Added the options for a variant to the product page * Mapped options to variants * Mapped options to variants * saleor: refine the auth process * saleor: remove unused code * saleor: handle customer find via refresh temporary solution * saleor: update deps * saleor: fix the session handling * saleor: fix the variants * saleor: simplify the naming for GraphQL statements * saleor: fix the type for collection * saleor: arrange the error codes * saleor: integrate collections * saleor: fix product sorting * saleor: set cookie location * saleor: update the schema * saleor: attach checkout to customer * saleor: fix the checkout flow * saleor: unify GraphQL naming approach * task: update deps * Add the env variables for saleor to the template * task: prettier * saleor: stub API for build/typescript compilation thanks @cond0r * task: temporarily disable for the `build` * saleor: refactor GraphQL queries * saleor: adjust the config * task: update dependencies * revert: Next.js to `10.0.9` * saleor: fix the checkout fetch query * task: update dependencies * saleor: adapt for displaying featured products * saleor: update the provider structure * saleor: make the home page representable * feature/cart: display the variant name (cond) Co-authored-by: Patryk Zawadzki <patrys@room-303.com> Co-authored-by: royderks <10717410+royderks@users.noreply.github.com>
75 lines
1.9 KiB
TypeScript
75 lines
1.9 KiB
TypeScript
import { SWRHook } from '@commerce/utils/types'
|
|
import { Product } from '@commerce/types/product'
|
|
import useSearch, { UseSearch } from '@commerce/product/use-search'
|
|
|
|
import { ProductCountableEdge } from '../schema'
|
|
import { getSearchVariables, normalizeProduct } from '../utils'
|
|
|
|
import * as query from '../utils/queries'
|
|
import { SearchProductsHook } from '@commerce/types/product'
|
|
|
|
export default useSearch as UseSearch<typeof handler>
|
|
|
|
export type SearchProductsInput = {
|
|
search?: string
|
|
categoryId?: string | number
|
|
brandId?: string | number
|
|
sort?: string
|
|
}
|
|
|
|
export type SearchProductsData = {
|
|
products: Product[]
|
|
found: boolean
|
|
}
|
|
|
|
export const handler: SWRHook<SearchProductsHook> = {
|
|
fetchOptions: {
|
|
query: query.ProductMany,
|
|
},
|
|
async fetcher({ input, options, fetch }) {
|
|
const { categoryId, brandId } = input
|
|
|
|
const data = await fetch({
|
|
query: categoryId ? query.CollectionOne : options.query,
|
|
method: options?.method,
|
|
variables: getSearchVariables(input),
|
|
})
|
|
|
|
let edges
|
|
|
|
if (categoryId) {
|
|
edges = data.collection?.products?.edges ?? []
|
|
// FIXME @zaiste, no `vendor` in Saleor
|
|
// if (brandId) {
|
|
// edges = edges.filter(
|
|
// ({ node: { vendor } }: ProductCountableEdge) =>
|
|
// vendor.replace(/\s+/g, '-').toLowerCase() === brandId
|
|
// )
|
|
// }
|
|
} else {
|
|
edges = data.products?.edges ?? []
|
|
}
|
|
|
|
return {
|
|
products: edges.map(({ node }: ProductCountableEdge) => normalizeProduct(node)),
|
|
found: !!edges.length,
|
|
}
|
|
},
|
|
useHook:
|
|
({ useData }) =>
|
|
(input = {}) => {
|
|
return useData({
|
|
input: [
|
|
['search', input.search],
|
|
['categoryId', input.categoryId],
|
|
['brandId', input.brandId],
|
|
['sort', input.sort],
|
|
],
|
|
swrOptions: {
|
|
revalidateOnFocus: false,
|
|
...input.swrOptions,
|
|
},
|
|
})
|
|
},
|
|
}
|