mirror of
https://github.com/vercel/commerce.git
synced 2025-07-26 19:51:23 +00:00
query all products for vendors & paths, improve search
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { getConfig, ShopifyConfig } from '../api'
|
||||
import fetchAllProducts from '../api/utils/fetch-all-products'
|
||||
import { ProductEdge } from '../schema'
|
||||
import getAllProductsPathsQuery from '../utils/queries/get-all-products-paths-query'
|
||||
|
||||
@@ -9,21 +10,19 @@ type ReturnType = {
|
||||
const getAllProductPaths = async (options?: {
|
||||
variables?: any
|
||||
config?: ShopifyConfig
|
||||
previe?: boolean
|
||||
preview?: boolean
|
||||
}): Promise<ReturnType> => {
|
||||
let { config, variables = { first: 250 } } = options ?? {}
|
||||
config = getConfig(config)
|
||||
|
||||
const { data } = await config.fetch(getAllProductsPathsQuery, {
|
||||
const products = await fetchAllProducts({
|
||||
config,
|
||||
query: getAllProductsPathsQuery,
|
||||
variables,
|
||||
})
|
||||
|
||||
const edges = data?.products?.edges
|
||||
const productInfo = data?.products?.productInfo
|
||||
const hasNextPage = productInfo?.hasNextPage
|
||||
|
||||
return {
|
||||
products: edges?.map(({ node: { handle } }: ProductEdge) => ({
|
||||
products: products?.map(({ node: { handle } }: ProductEdge) => ({
|
||||
node: {
|
||||
path: `/${handle}`,
|
||||
},
|
||||
|
@@ -1,22 +1,22 @@
|
||||
import useCommerceSearch from '@commerce/products/use-search'
|
||||
import getAllProductsQuery from '@framework/utils/queries/get-all-products-query'
|
||||
import {
|
||||
getAllProductsQuery,
|
||||
getCollectionProductsQuery,
|
||||
} from '@framework/utils/queries'
|
||||
|
||||
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 getSearchVariables from '@framework/utils/get-search-variables'
|
||||
|
||||
import sortBy from '@framework/utils/get-sort-variables'
|
||||
import { normalizeProduct } from '@framework/lib/normalize'
|
||||
|
||||
export type SearchProductsInput = {
|
||||
search?: string
|
||||
categoryPath?: string
|
||||
categoryId?: string
|
||||
brandId?: string
|
||||
sort?: string
|
||||
}
|
||||
|
||||
@@ -32,22 +32,20 @@ export type SearchProductsData = {
|
||||
export const fetcher: HookFetcher<
|
||||
SearchRequestProductsData,
|
||||
SearchProductsInput
|
||||
> = (options, { search, categoryPath, sort }, fetch) => {
|
||||
> = (options, input, fetch) => {
|
||||
return fetch({
|
||||
query: options?.query,
|
||||
method: options?.method,
|
||||
variables: {
|
||||
...searchByProductType(search),
|
||||
...searchByTag(categoryPath),
|
||||
...sortBy(sort),
|
||||
...getSearchVariables(input),
|
||||
},
|
||||
}).then(
|
||||
({ products }): SearchProductsData => {
|
||||
(resp): SearchProductsData => {
|
||||
const edges = resp.products?.edges
|
||||
|
||||
return {
|
||||
products: products?.edges?.map(({ node: p }: ProductEdge) =>
|
||||
normalizeProduct(p)
|
||||
),
|
||||
found: !!products?.edges?.length,
|
||||
products: edges?.map(({ node: p }: ProductEdge) => normalizeProduct(p)),
|
||||
found: !!edges?.length,
|
||||
}
|
||||
}
|
||||
)
|
||||
@@ -64,7 +62,8 @@ export function extendHook(
|
||||
},
|
||||
[
|
||||
['search', input.search],
|
||||
['categoryPath', input.categoryPath],
|
||||
['categoryId', input.categoryId],
|
||||
['brandId', input.brandId],
|
||||
['sort', input.sort],
|
||||
],
|
||||
customFetcher,
|
||||
|
Reference in New Issue
Block a user