From 300161105726513b73ccb94ac5a0ecdbf6ed78e1 Mon Sep 17 00:00:00 2001 From: Loan Laux Date: Tue, 6 Jul 2021 16:47:29 +0300 Subject: [PATCH] Add catalogItems API method Signed-off-by: Loan Laux --- .../api/endpoints/catalog/index.ts | 1 - .../api/endpoints/catalog/products.ts | 1 - .../catalog/products/get-products.ts | 23 +++++ .../api/endpoints/catalog/products/index.ts | 19 ++++ .../reactioncommerce/product/use-search.tsx | 97 +++++++------------ pages/api/catalog/products.ts | 2 +- pages/search.tsx | 1 - 7 files changed, 77 insertions(+), 67 deletions(-) delete mode 100644 framework/reactioncommerce/api/endpoints/catalog/index.ts delete mode 100644 framework/reactioncommerce/api/endpoints/catalog/products.ts create mode 100644 framework/reactioncommerce/api/endpoints/catalog/products/get-products.ts create mode 100644 framework/reactioncommerce/api/endpoints/catalog/products/index.ts diff --git a/framework/reactioncommerce/api/endpoints/catalog/index.ts b/framework/reactioncommerce/api/endpoints/catalog/index.ts deleted file mode 100644 index 491bf0ac9..000000000 --- a/framework/reactioncommerce/api/endpoints/catalog/index.ts +++ /dev/null @@ -1 +0,0 @@ -export default function noopApi(...args: any[]): void {} diff --git a/framework/reactioncommerce/api/endpoints/catalog/products.ts b/framework/reactioncommerce/api/endpoints/catalog/products.ts deleted file mode 100644 index 491bf0ac9..000000000 --- a/framework/reactioncommerce/api/endpoints/catalog/products.ts +++ /dev/null @@ -1 +0,0 @@ -export default function noopApi(...args: any[]): void {} diff --git a/framework/reactioncommerce/api/endpoints/catalog/products/get-products.ts b/framework/reactioncommerce/api/endpoints/catalog/products/get-products.ts new file mode 100644 index 000000000..4907a096b --- /dev/null +++ b/framework/reactioncommerce/api/endpoints/catalog/products/get-products.ts @@ -0,0 +1,23 @@ +import catalogItemsQuery from '@framework/utils/queries/catalog-items-query' +import { normalizeProduct } from '@framework/utils' +import type { ProductsEndpoint } from '.' + +const getCart: ProductsEndpoint['handlers']['getProducts'] = async ({ + req, + res, + config, +}) => { + const { + data: { catalogItems }, + } = await config.fetch(catalogItemsQuery, { + variables: { + shopIds: [config.shopId], + }, + }) + + const products = catalogItems.map((item) => normalizeProduct(item)) + + res.status(200).json({ data: products ?? null }) +} + +export default getCart diff --git a/framework/reactioncommerce/api/endpoints/catalog/products/index.ts b/framework/reactioncommerce/api/endpoints/catalog/products/index.ts new file mode 100644 index 000000000..20dbed952 --- /dev/null +++ b/framework/reactioncommerce/api/endpoints/catalog/products/index.ts @@ -0,0 +1,19 @@ +import { CommerceAPI, createEndpoint, GetAPISchema } from '@commerce/api' +import productsEndpoint from '@commerce/api/endpoints/catalog/products' +import type { ProductsSchema } from '@commerce/types/product' +import getProducts from './get-products' + +export type ProductsAPI = GetAPISchema + +export type ProductsEndpoint = ProductsAPI['endpoint'] + +export const handlers: ProductsEndpoint['handlers'] = { + getProducts, +} + +const productsApi = createEndpoint({ + handler: productsEndpoint, + handlers, +}) + +export default productsApi diff --git a/framework/reactioncommerce/product/use-search.tsx b/framework/reactioncommerce/product/use-search.tsx index 612601457..e61114335 100644 --- a/framework/reactioncommerce/product/use-search.tsx +++ b/framework/reactioncommerce/product/use-search.tsx @@ -1,80 +1,51 @@ import { SWRHook } from '@commerce/utils/types' import useSearch, { UseSearch } from '@commerce/product/use-search' -import { CatalogItemEdge } from '../schema' -import { - catalogItemsQuery, - getCollectionProductsQuery, - getSearchVariables, - normalizeProduct, -} from '../utils' - -import { Product } from '@commerce/types' - export default useSearch as UseSearch export type SearchProductsInput = { search?: string - categoryId?: string - brandId?: string + categoryId?: number | string + brandId?: number sort?: string - shopId?: string + locale?: string } -export type SearchProductsData = { - products: Product[] - found: boolean -} - -export const handler: SWRHook< - SearchProductsData, - SearchProductsInput, - SearchProductsInput -> = { +export const handler: SWRHook = { fetchOptions: { - query: catalogItemsQuery, + url: '/api/catalog/products', + method: 'GET', }, - async fetcher({ input, options, fetch }) { - const { brandId, shopId } = input + fetcher({ input: { search, categoryId, brandId, sort }, options, fetch }) { + // Use a dummy base as we only care about the relative path + const url = new URL(options.url!, 'http://a') - const data = await fetch({ - query: options.query, - method: options?.method, - variables: { - ...getSearchVariables(input), - shopIds: [shopId], - }, - }) + if (search) url.searchParams.set('search', search) + if (Number.isInteger(categoryId)) + url.searchParams.set('categoryId', String(categoryId)) + if (Number.isInteger(brandId)) + url.searchParams.set('brandId', String(brandId)) + if (sort) url.searchParams.set('sort', sort) - let edges - - edges = data.catalogItems?.edges ?? [] - if (brandId) { - edges = edges.filter( - ({ node: { vendor } }: CatalogItemEdge) => vendor === brandId - ) - } - - return { - products: edges.map(({ node }: CatalogItemEdge) => - normalizeProduct(node) - ), - found: !!edges.length, - } - }, - useHook: ({ useData }) => (input = {}) => { - return useData({ - input: [ - ['search', input.search], - ['categoryId', input.categoryId], - ['brandId', input.brandId], - ['sort', input.sort], - ['shopId', input.shopId], - ], - swrOptions: { - revalidateOnFocus: false, - ...input.swrOptions, - }, + return fetch({ + url: url.pathname + url.search, + method: options.method, }) }, + useHook: + ({ useData }) => + (input = {}) => { + return useData({ + input: [ + ['search', input.search], + ['categoryId', input.categoryId], + ['brandId', input.brandId], + ['sort', input.sort], + ], + swrOptions: { + revalidateOnFocus: false, + ...input.swrOptions, + }, + }) + }, } diff --git a/pages/api/catalog/products.ts b/pages/api/catalog/products.ts index 631bfd516..5f2be15a2 100644 --- a/pages/api/catalog/products.ts +++ b/pages/api/catalog/products.ts @@ -1,4 +1,4 @@ -import productsApi from '@framework/api/endpoints/catalog/products' +import productsApi from '@framework/api/endpoints/catalog/get-products' import commerce from '@lib/api/commerce' export default productsApi(commerce) diff --git a/pages/search.tsx b/pages/search.tsx index 9aad460b4..2c703f815 100644 --- a/pages/search.tsx +++ b/pages/search.tsx @@ -43,7 +43,6 @@ export async function getStaticProps({ pages, categories, brands, - shopId: config?.shopId, }, revalidate: 200, }