update product search hook. whitelist cdn

This commit is contained in:
Greg Hoskin
2021-03-29 17:49:14 -06:00
parent e4c1050880
commit 1ebf458fb2
8 changed files with 31 additions and 56 deletions

View File

@@ -21,16 +21,8 @@ const getAllProducts = async (options: {
}): Promise<ReturnType> => {
let { config, variables = { first: 250 } } = options ?? {}
config = getConfig(config)
const { data }: GraphQLFetcherResult = await config.fetch(
getAllProductsQuery,
{ variables }
)
const products =
data.products?.edges?.map(({ node: p }: ProductEdge) =>
normalizeProduct(p)
) ?? []
const { results } = await config.fetchSwell('products', 'get')
const products = results.map((product) => normalizeProduct(product)) ?? []
return {
products,

View File

@@ -1,13 +1,7 @@
import { SWRHook } from '@commerce/utils/types'
import useSearch, { UseSearch } from '@commerce/product/use-search'
import { ProductEdge } from '../schema'
import {
getAllProductsQuery,
getCollectionProductsQuery,
getSearchVariables,
normalizeProduct,
} from '../utils'
import { getAllProductsQuery, normalizeProduct } from '../utils'
import { Product } from '@commerce/types'
@@ -36,28 +30,17 @@ export const handler: SWRHook<
async fetcher({ input, options, fetch }) {
const { categoryId, brandId } = input
const data = await fetch({
query: categoryId ? getCollectionProductsQuery : options.query,
method: options?.method,
variables: getSearchVariables(input),
const { results, count: found } = await fetch({
query: 'products',
method: 'get',
// variables: { categoryId },
})
let edges
if (categoryId) {
edges = data.node?.products?.edges ?? []
if (brandId) {
edges = edges.filter(
({ node: { vendor } }: ProductEdge) => vendor === brandId
)
}
} else {
edges = data.products?.edges ?? []
}
const products = results.map((product) => normalizeProduct(product))
return {
products: edges.map(({ node }: ProductEdge) => normalizeProduct(node)),
found: !!edges.length,
products,
found,
}
},
useHook: ({ useData }) => (input = {}) => {