mirror of
https://github.com/vercel/commerce.git
synced 2025-06-09 01:36:57 +00:00
refactor category and brand filtering logic
Signed-off-by: Loan Laux <loan@outgrow.io>
This commit is contained in:
parent
2160264d60
commit
c2c43b76aa
@ -1,16 +1,30 @@
|
|||||||
import catalogItemsQuery from '@framework/utils/queries/catalog-items-query'
|
import catalogItemsQuery from '@framework/utils/queries/catalog-items-query'
|
||||||
import { normalizeProduct } from '@framework/utils'
|
import { normalizeProduct } from '@framework/utils'
|
||||||
import type { ProductsEndpoint } from './products'
|
import type { ProductsEndpoint } from './products'
|
||||||
|
import getSearchVariables from '../../utils/get-search-variables'
|
||||||
|
|
||||||
const getCart: ProductsEndpoint['handlers']['getProducts'] = async ({
|
const getCart: ProductsEndpoint['handlers']['getProducts'] = async ({
|
||||||
req,
|
req,
|
||||||
res,
|
res,
|
||||||
config,
|
config,
|
||||||
}) => {
|
}) => {
|
||||||
|
const {
|
||||||
|
brandId,
|
||||||
|
categoryId,
|
||||||
|
search,
|
||||||
|
sort,
|
||||||
|
} = req.query
|
||||||
|
|
||||||
const {
|
const {
|
||||||
data: { catalogItems },
|
data: { catalogItems },
|
||||||
} = await config.fetch(catalogItemsQuery, {
|
} = await config.fetch(catalogItemsQuery, {
|
||||||
variables: {
|
variables: {
|
||||||
|
...getSearchVariables({
|
||||||
|
brandId,
|
||||||
|
categoryId,
|
||||||
|
search,
|
||||||
|
sort,
|
||||||
|
}),
|
||||||
shopIds: [config.shopId],
|
shopIds: [config.shopId],
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
import { Provider, ReactionCommerceConfig } from '../'
|
import { Provider, ReactionCommerceConfig } from '../'
|
||||||
import { GetCollectionsQuery } from '../../schema'
|
|
||||||
import getCollectionsQuery from '../../utils/queries/get-all-collections-query'
|
import getCollectionsQuery from '../../utils/queries/get-all-collections-query'
|
||||||
import { OperationContext } from '@commerce/api/operations'
|
import { OperationContext } from '@commerce/api/operations'
|
||||||
import { Category } from '@commerce/types/site'
|
import { Category } from '@commerce/types/site'
|
||||||
import getCategories from '../../utils/get-categories'
|
import getCategories from '../utils/get-categories'
|
||||||
import getVendors from '../../utils/get-vendors'
|
import getVendors from '../utils/get-vendors'
|
||||||
|
|
||||||
export type GetSiteInfoResult<
|
export type GetSiteInfoResult<
|
||||||
T extends { categories: any[]; brands: any[] } = {
|
T extends { categories: any[]; brands: any[] } = {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { ReactionCommerceConfig } from '../api'
|
import { ReactionCommerceConfig } from '../'
|
||||||
import { CollectionEdge, TagEdge } from '../schema'
|
import { TagEdge } from '../../schema'
|
||||||
import getTagsQuery from './queries/get-all-collections-query'
|
import getTagsQuery from '../../utils/queries/get-all-collections-query'
|
||||||
|
|
||||||
export type Category = {
|
export type Category = {
|
||||||
entityId: string
|
entityId: string
|
||||||
@ -10,7 +10,7 @@ export type Category = {
|
|||||||
|
|
||||||
const getCategories = async (
|
const getCategories = async (
|
||||||
config: ReactionCommerceConfig
|
config: ReactionCommerceConfig
|
||||||
): Promise<Tag[]> => {
|
): Promise<TagEdge[]> => {
|
||||||
const { data } = await config.fetch(getTagsQuery, {
|
const { data } = await config.fetch(getTagsQuery, {
|
||||||
variables: {
|
variables: {
|
||||||
first: 250,
|
first: 250,
|
||||||
@ -21,9 +21,9 @@ const getCategories = async (
|
|||||||
return (
|
return (
|
||||||
data.tags?.edges?.map(
|
data.tags?.edges?.map(
|
||||||
({
|
({
|
||||||
node: { _id: entityId, displayTitle: name, slug: handle },
|
node: { _id: id, displayTitle: name, slug: handle },
|
||||||
}: TagEdge) => ({
|
}: TagEdge) => ({
|
||||||
entityId,
|
id,
|
||||||
name,
|
name,
|
||||||
path: `/${handle}`,
|
path: `/${handle}`,
|
||||||
slug: handle,
|
slug: handle,
|
@ -1,5 +1,12 @@
|
|||||||
import getSortVariables from './get-sort-variables'
|
import getSortVariables from './get-sort-variables'
|
||||||
import type { SearchProductsInput } from '../product/use-search'
|
|
||||||
|
export type SearchProductsInput = {
|
||||||
|
search?: string
|
||||||
|
categoryId?: number | string
|
||||||
|
brandId?: number
|
||||||
|
sort?: string
|
||||||
|
locale?: string
|
||||||
|
}
|
||||||
|
|
||||||
export const getSearchVariables = ({
|
export const getSearchVariables = ({
|
||||||
brandId,
|
brandId,
|
@ -1,6 +1,6 @@
|
|||||||
import { ReactionCommerceConfig } from '../api'
|
import { ReactionCommerceConfig } from '../'
|
||||||
import getAllProductVendors from './queries/get-all-product-vendors-query'
|
import { Vendor } from '../../schema'
|
||||||
import { Vendor } from '@framework/schema'
|
import getAllProductVendors from '../../utils/queries/get-all-product-vendors-query'
|
||||||
|
|
||||||
export type Brand = {
|
export type Brand = {
|
||||||
entityId: string
|
entityId: string
|
@ -3,34 +3,31 @@ import useSearch, { UseSearch } from '@commerce/product/use-search'
|
|||||||
|
|
||||||
export default useSearch as UseSearch<typeof handler>
|
export default useSearch as UseSearch<typeof handler>
|
||||||
|
|
||||||
export type SearchProductsInput = {
|
|
||||||
search?: string
|
|
||||||
categoryId?: number | string
|
|
||||||
brandId?: number
|
|
||||||
sort?: string
|
|
||||||
locale?: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export const handler: SWRHook<any> = {
|
export const handler: SWRHook<any> = {
|
||||||
fetchOptions: {
|
fetchOptions: {
|
||||||
url: '/api/catalog/products',
|
url: '/api/catalog/products',
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
},
|
},
|
||||||
fetcher({ input: { search, categoryId, brandId, sort }, options, fetch }) {
|
async fetcher({ input: { search, categoryId, brandId, sort }, options, fetch }) {
|
||||||
// Use a dummy base as we only care about the relative path
|
// Use a dummy base as we only care about the relative path
|
||||||
const url = new URL(options.url!, 'http://a')
|
const url = new URL(options.url!, 'http://a')
|
||||||
|
|
||||||
if (search) url.searchParams.set('search', search)
|
if (search) url.searchParams.set('search', search)
|
||||||
if (Number.isInteger(categoryId))
|
if (categoryId)
|
||||||
url.searchParams.set('categoryId', String(categoryId))
|
url.searchParams.set('categoryId', String(categoryId))
|
||||||
if (Number.isInteger(brandId))
|
if (brandId)
|
||||||
url.searchParams.set('brandId', String(brandId))
|
url.searchParams.set('brandId', String(brandId))
|
||||||
if (sort) url.searchParams.set('sort', sort)
|
if (sort) url.searchParams.set('sort', sort)
|
||||||
|
|
||||||
return fetch({
|
const results = await fetch({
|
||||||
url: url.pathname + url.search,
|
url: url.pathname + url.search,
|
||||||
method: options.method,
|
method: options.method,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
products: results?.products ?? [],
|
||||||
|
found: results?.products?.length > 0 ?? false,
|
||||||
|
}
|
||||||
},
|
},
|
||||||
useHook:
|
useHook:
|
||||||
({ useData }) =>
|
({ useData }) =>
|
||||||
|
@ -1,8 +1,4 @@
|
|||||||
export { default as handleFetchResponse } from './handle-fetch-response'
|
export { default as handleFetchResponse } from './handle-fetch-response'
|
||||||
export { default as getSearchVariables } from './get-search-variables'
|
|
||||||
export { default as getSortVariables } from './get-sort-variables'
|
|
||||||
export { default as getVendors } from './get-vendors'
|
|
||||||
export { default as getCategories } from './get-categories'
|
|
||||||
export { default as getAnonymousCartToken } from './get-anonymous-cart-token'
|
export { default as getAnonymousCartToken } from './get-anonymous-cart-token'
|
||||||
export { default as getAnonymousCartId } from './get-cart-id'
|
export { default as getAnonymousCartId } from './get-cart-id'
|
||||||
export * from './queries'
|
export * from './queries'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user