mirror of
https://github.com/vercel/commerce.git
synced 2025-07-04 04:01:21 +00:00
PLP with searching by category
This commit is contained in:
parent
a7a75e7f69
commit
c546d26bbe
@ -22,8 +22,9 @@ const createApiFetch: (
|
|||||||
return async (url, queryData = {}, fetchOptions = {}) => {
|
return async (url, queryData = {}, fetchOptions = {}) => {
|
||||||
console.log(
|
console.log(
|
||||||
'apiFetch called. query = ',
|
'apiFetch called. query = ',
|
||||||
url,
|
|
||||||
'url = ',
|
'url = ',
|
||||||
|
url,
|
||||||
|
'queryData = ',
|
||||||
queryData,
|
queryData,
|
||||||
'fetchOptions = ',
|
'fetchOptions = ',
|
||||||
fetchOptions
|
fetchOptions
|
||||||
|
@ -9,12 +9,15 @@ import type {
|
|||||||
import { errors } from '@spree/storefront-api-v2-sdk'
|
import { errors } from '@spree/storefront-api-v2-sdk'
|
||||||
import { requireConfigValue } from './isomorphicConfig'
|
import { requireConfigValue } from './isomorphicConfig'
|
||||||
import getSpreeSdkMethodFromEndpointPath from './utils/getSpreeSdkMethodFromEndpointPath'
|
import getSpreeSdkMethodFromEndpointPath from './utils/getSpreeSdkMethodFromEndpointPath'
|
||||||
// import { handleFetchResponse } from './utils'
|
import SpreeSdkMethodFromEndpointPathError from './errors/SpreeSdkMethodFromEndpointPathError'
|
||||||
|
import type { SpreeSdkVariables } from './types'
|
||||||
|
import { GraphQLFetcherResult } from '@commerce/api'
|
||||||
|
|
||||||
const client = makeClient({ host: requireConfigValue('spreeApiHost') })
|
const client = makeClient({ host: requireConfigValue('spreeApiHost') })
|
||||||
|
|
||||||
const fetcher: Fetcher = async (requestOptions) => {
|
const fetcher: Fetcher<GraphQLFetcherResult<any>, SpreeSdkVariables> = async (
|
||||||
console.log('Fetcher called')
|
requestOptions
|
||||||
|
) => {
|
||||||
// url?: string
|
// url?: string
|
||||||
// query?: string
|
// query?: string
|
||||||
// method?: string
|
// method?: string
|
||||||
@ -23,23 +26,35 @@ const fetcher: Fetcher = async (requestOptions) => {
|
|||||||
const { url, method, variables, query } = requestOptions
|
const { url, method, variables, query } = requestOptions
|
||||||
const { locale, ...vars } = variables ?? {}
|
const { locale, ...vars } = variables ?? {}
|
||||||
|
|
||||||
if (!url) {
|
|
||||||
// TODO: Create a custom type for this error.
|
|
||||||
throw new Error('Url not provider for fetcher.')
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
`Fetching products using options: ${JSON.stringify(requestOptions)}.`
|
'Fetcher called. Configuration: ',
|
||||||
|
'url = ',
|
||||||
|
url,
|
||||||
|
'requestOptions = ',
|
||||||
|
requestOptions
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: Not best to use url for finding the method, but should be good enough for now.
|
if (!variables) {
|
||||||
|
throw new SpreeSdkMethodFromEndpointPathError(
|
||||||
|
`Required SpreeSdkVariables not provided.`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
const storeResponse: ResultResponse<JsonApiResponse | JsonApiListResponse> =
|
const storeResponse: ResultResponse<JsonApiResponse | JsonApiListResponse> =
|
||||||
await getSpreeSdkMethodFromEndpointPath(client, url)(...variables.args) // TODO: Not the best to use variables here as it's type is any.
|
await getSpreeSdkMethodFromEndpointPath(
|
||||||
|
client,
|
||||||
|
variables.methodPath
|
||||||
|
)(...variables.arguments)
|
||||||
|
|
||||||
if (storeResponse.success()) {
|
if (storeResponse.success()) {
|
||||||
return storeResponse.success()
|
return {
|
||||||
|
data: storeResponse.success(),
|
||||||
|
res: storeResponse as any, //FIXME: MUST BE fetch() RESPONSE instead of axios.
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: Allow Spree SDK to use fetch instead of axios
|
||||||
|
// (https://github.com/spree/spree-storefront-api-v2-js-sdk/issues/189)
|
||||||
|
|
||||||
const storeResponseError = storeResponse.fail()
|
const storeResponseError = storeResponse.fail()
|
||||||
|
|
||||||
@ -50,8 +65,6 @@ const fetcher: Fetcher = async (requestOptions) => {
|
|||||||
throw storeResponseError
|
throw storeResponseError
|
||||||
}
|
}
|
||||||
|
|
||||||
// import { Fetcher } from '@commerce/utils/types'
|
|
||||||
|
|
||||||
// export const fetcher: Fetcher = async () => {
|
// export const fetcher: Fetcher = async () => {
|
||||||
// console.log('FETCHER')
|
// console.log('FETCHER')
|
||||||
// const res = await fetch('./data.json')
|
// const res = await fetch('./data.json')
|
||||||
|
@ -1,39 +1,72 @@
|
|||||||
import type { SWRHook, Fetcher } from '@commerce/utils/types'
|
import type { Fetcher, SWRHook } from '@commerce/utils/types'
|
||||||
import useSearch from '@commerce/product/use-search'
|
import useSearch from '@commerce/product/use-search'
|
||||||
|
import type { Product, SearchProductsHook } from '@commerce/types/product'
|
||||||
import type { UseSearch } from '@commerce/product/use-search'
|
import type { UseSearch } from '@commerce/product/use-search'
|
||||||
|
import normalizeProduct from '../utils/normalizeProduct'
|
||||||
|
import type { GraphQLFetcherResult } from '@commerce/api'
|
||||||
|
import { IProducts } from '@spree/storefront-api-v2-sdk/types/interfaces/Product'
|
||||||
|
|
||||||
export const handler: SWRHook<any> = {
|
export const handler: SWRHook<SearchProductsHook> = {
|
||||||
fetchOptions: {
|
fetchOptions: {
|
||||||
url: 'client.products.list', // Add custom option for method name later
|
url: '__UNUSED__',
|
||||||
query: '',
|
query: '',
|
||||||
},
|
},
|
||||||
async fetcher({ input, options, fetch }) {
|
async fetcher({ input, options, fetch }) {
|
||||||
// This method is only needed if the options need to be modified before calling the generic fetcher (created in createFetcher).
|
// This method is only needed if the options need to be modified before calling the generic fetcher (created in createFetcher).
|
||||||
// TODO: Actually filter by input and query.
|
// TODO: Actually filter by input and query.
|
||||||
|
|
||||||
console.log(
|
console.info(
|
||||||
`Calling useSearch fetcher with input: ${JSON.stringify(
|
'useSearch fetcher called. Configuration: ',
|
||||||
input
|
'input: ',
|
||||||
)} and options: ${JSON.stringify(options)}.`
|
input,
|
||||||
|
'options: ',
|
||||||
|
options
|
||||||
)
|
)
|
||||||
|
|
||||||
// FIXME: IMPLEMENT
|
const categoryOrBrandId = input.categoryId || input.brandId
|
||||||
|
|
||||||
return fetch({
|
const filter = categoryOrBrandId
|
||||||
url: options.url,
|
? {
|
||||||
variables: { args: [] }, // TODO: Actually provide args later.
|
filter: {
|
||||||
|
taxons: categoryOrBrandId,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
: {}
|
||||||
|
|
||||||
|
const { data: spreeSuccessResponse } = await fetch<
|
||||||
|
GraphQLFetcherResult<IProducts>
|
||||||
|
>({
|
||||||
|
variables: {
|
||||||
|
methodPath: 'products.list',
|
||||||
|
arguments: [
|
||||||
|
{
|
||||||
|
include: 'variants,images,option_types,variants.option_values',
|
||||||
|
per_page: 50,
|
||||||
|
...filter,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const normalizedProducts: Product[] = spreeSuccessResponse.data.map(
|
||||||
|
(spreeProduct) => normalizeProduct(spreeSuccessResponse, spreeProduct)
|
||||||
|
)
|
||||||
|
|
||||||
|
const found = spreeSuccessResponse.data.length > 0
|
||||||
|
|
||||||
|
return { products: normalizedProducts, found }
|
||||||
},
|
},
|
||||||
// useHook is used for both, SWR and mutation requests to the store.
|
// useHook is used for both, SWR and mutation requests to the store.
|
||||||
// useHook is called in React components. For example, after clicking `Add to cart`.
|
// useHook is called in React components. For example, after clicking `Add to cart`.
|
||||||
useHook:
|
useHook:
|
||||||
({ useData }) =>
|
({ useData }) =>
|
||||||
(input = {}) => {
|
(input = {}) => {
|
||||||
console.log('useHook called')
|
|
||||||
|
|
||||||
// useData calls the fetcher method (above).
|
// useData calls the fetcher method (above).
|
||||||
// The difference between useHook and calling fetcher directly is
|
// The difference between useHook and calling fetcher directly is
|
||||||
// useHook accepts swrOptions.
|
// useHook accepts swrOptions.
|
||||||
|
|
||||||
|
console.log('useSearch useHook called.')
|
||||||
|
|
||||||
return useData({
|
return useData({
|
||||||
input: [
|
input: [
|
||||||
['search', input.search],
|
['search', input.search],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user