From fa625b5d063e020b6067c4cd70eccf3dba65295b Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Tue, 13 Oct 2020 20:16:43 -0500 Subject: [PATCH] Filter by brand too --- .../api/catalog/handlers/get-products.ts | 6 +++++- lib/bigcommerce/api/catalog/products.ts | 5 ++++- lib/bigcommerce/products/use-search.tsx | 14 ++++++++++++-- pages/search.tsx | 16 +++++++++++----- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/lib/bigcommerce/api/catalog/handlers/get-products.ts b/lib/bigcommerce/api/catalog/handlers/get-products.ts index c8c42b862..6bc3a8fbf 100644 --- a/lib/bigcommerce/api/catalog/handlers/get-products.ts +++ b/lib/bigcommerce/api/catalog/handlers/get-products.ts @@ -4,13 +4,17 @@ import type { ProductsHandlers } from '../products' // Return current cart info const getProducts: ProductsHandlers['getProducts'] = async ({ res, - body: { search }, + body: { search, category, brand }, config, }) => { // Use a dummy base as we only care about the relative path const url = new URL('/v3/catalog/products', 'http://a') if (search) url.searchParams.set('keyword', search) + if (category && Number.isInteger(Number(category))) + url.searchParams.set('categories:in', category) + if (brand && Number.isInteger(Number(brand))) + url.searchParams.set('brand_id', brand) // We only want the id of each product url.searchParams.set('include_fields', 'id') diff --git a/lib/bigcommerce/api/catalog/products.ts b/lib/bigcommerce/api/catalog/products.ts index 31dc2d2e7..c4a223a35 100644 --- a/lib/bigcommerce/api/catalog/products.ts +++ b/lib/bigcommerce/api/catalog/products.ts @@ -13,7 +13,10 @@ export type SearchProductsData = { } export type ProductsHandlers = { - getProducts: BigcommerceHandler + getProducts: BigcommerceHandler< + SearchProductsData, + { search?: 'string'; category?: string; brand?: string } + > } const METHODS = ['GET'] diff --git a/lib/bigcommerce/products/use-search.tsx b/lib/bigcommerce/products/use-search.tsx index 345c7ffbc..34cc0ed8b 100644 --- a/lib/bigcommerce/products/use-search.tsx +++ b/lib/bigcommerce/products/use-search.tsx @@ -10,17 +10,23 @@ const defaultOpts = { export type SearchProductsInput = { search?: string + categoryId?: number + brandId?: number } export const fetcher: HookFetcher = ( options, - { search }, + { search, categoryId, brandId }, fetch ) => { // Use a dummy base as we only care about the relative path const url = new URL(options?.url ?? defaultOpts.url, 'http://a') if (search) url.searchParams.set('search', search) + if (Number.isInteger(categoryId)) + url.searchParams.set('category', String(categoryId)) + if (Number.isInteger(categoryId)) + url.searchParams.set('brand', String(brandId)) return fetch({ url: url.pathname + url.search, @@ -35,7 +41,11 @@ export function extendHook( const useSearch = (input: SearchProductsInput = {}) => { const response = useCommerceSearch( defaultOpts, - [['search', input.search]], + [ + ['search', input.search], + ['categoryId', input.categoryId], + ['brandId', input.brandId], + ], customFetcher, { revalidateOnFocus: false, ...swrOptions } ) diff --git a/pages/search.tsx b/pages/search.tsx index 6b3ae91e5..fbd8f9537 100644 --- a/pages/search.tsx +++ b/pages/search.tsx @@ -23,12 +23,18 @@ export default function Home({ }: InferGetStaticPropsType) { const router = useRouter() const { q } = router.query + const { category, brand } = useSearchMeta(router.asPath) + const activeCategory = categories.find( + (cat) => getSlug(cat.path) === category + ) + const activeBrand = brands.find( + (b) => getSlug(b.node.path) === `brands/${brand}` + )?.node const { data } = useSearch({ search: typeof q === 'string' ? q : '', + categoryId: activeCategory?.entityId, + brandId: activeBrand?.entityId, }) - const { category, brand } = useSearchMeta(router.asPath) - - console.log('Q', category, brand) return ( @@ -42,7 +48,7 @@ export default function Home({
  • @@ -59,7 +65,7 @@ export default function Home({