product list page and search working

This commit is contained in:
Oliver Heywood
2021-10-11 10:09:55 -05:00
parent d193708184
commit 3a3eb38ecc
6 changed files with 110 additions and 11 deletions

View File

@@ -1 +0,0 @@
export default function noopApi(...args: any[]): void {}

View File

@@ -1 +0,0 @@
export default function noopApi(...args: any[]): void {}

View File

@@ -0,0 +1,49 @@
import { Product } from '@commerce/types/product'
import { RawProduct } from '@framework/types/product'
import { normalize as normalizeProduct } from '@framework/utils/product'
import { ProductsEndpoint } from '.'
const SORT: { [key: string]: string | undefined } = {
latest: 'id',
trending: 'total_sold',
price: 'price',
}
// Return current cart info
const getProducts: ProductsEndpoint['handlers']['getProducts'] = async ({
req,
res,
body: { search, categoryId, brandId, sort },
config: { restBuyerFetch, cartCookie, tokenCookie },
}) => {
console.log("categoryId", categoryId)
//Use a dummy base as we only care about the relative path
const url = new URL('/me/products', 'http://a')
if (search) {
url.searchParams.set('search', search)
}
if (categoryId && Number.isInteger(Number(categoryId))) {
url.searchParams.set('categoryID', String(categoryId))
}
// Get token from cookies
const token = req.cookies[tokenCookie];
var rawProducts = await restBuyerFetch(
'GET',
url.pathname + url.search,
null,
{ token }
);
const products = rawProducts.Items.map(normalizeProduct);
const found = rawProducts?.Items?.length > 0;
res.status(200).json({ data: { products, found } })
}
export default getProducts

View File

@@ -0,0 +1,18 @@
import { createEndpoint, GetAPISchema } from "@commerce/api"
import { ProductsSchema } from "@commerce/types/product"
import { OrdercloudAPI } from "@framework/api"
import getProducts from "./get-products";
import productsEndpoint from '@commerce/api/endpoints/catalog/products'
export type ProductsAPI = GetAPISchema<OrdercloudAPI, ProductsSchema>
export type ProductsEndpoint = ProductsAPI['endpoint']
export const handlers: ProductsEndpoint['handlers'] = { getProducts }
const productsApi = createEndpoint<ProductsAPI>({
handler: productsEndpoint,
handlers,
})
export default productsApi

View File

@@ -3,7 +3,7 @@
"features": {
"wishlist": false,
"cart": true,
"search": false,
"search": true,
"customerAuth": false,
"customCheckout": true
}

View File

@@ -1,17 +1,51 @@
import { SWRHook } from '@commerce/utils/types'
import useSearch, { UseSearch } from '@commerce/product/use-search'
import { SearchProductsHook } from '@commerce/types/product'
export default useSearch as UseSearch<typeof handler>
export const handler: SWRHook<any> = {
export const handler: SWRHook<SearchProductsHook> = {
fetchOptions: {
query: '',
},
async fetcher({ input, options, fetch }) {},
useHook: () => () => {
return {
data: {
products: [],
url: '/api/catalog/products',
method: 'GET',
},
fetcher({ input: { search, categoryId, brandId, sort }, options, fetch }) {
// Use a dummy base as we only care about the relative path
console.log("url:", options.url);
const url = new URL(options.url!, 'http://a')
if (search) {
url.searchParams.set('search', search)
}
if (categoryId) {
url.searchParams.set('categoryId', String(categoryId))
}
if (brandId) {
url.searchParams.set('brandId', String(brandId))
}
if (sort) {
url.searchParams.set('sort', String(sort))
}
console.log("url:", url.pathname + url.search);
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,
},
})
},
}