Skip to content

Commit

Permalink
Add product list page to OrderCloud provider (vercel#525)
Browse files Browse the repository at this point in the history
* product list page and search working

* categories working

* clean things up like console.log

* undo these

* don't need sort stuff. update comment

* turns out the if statements here are necesary

* very incomplete progress on sign-in

* Revert "very incomplete progress on sign-in"

This reverts commit a8dd2af.
  • Loading branch information
oliverheywood451 authored Oct 15, 2021
1 parent d193708 commit 55b9174
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 11 deletions.
1 change: 0 additions & 1 deletion framework/ordercloud/api/endpoints/catalog/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion framework/ordercloud/api/endpoints/catalog/products.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

import { normalize as normalizeProduct } from '@framework/utils/product'
import { ProductsEndpoint } from '.'

// Get products for the product list page. Search and category filter implemented. Sort and brand filter not implemented.
const getProducts: ProductsEndpoint['handlers']['getProducts'] = async ({
req,
res,
body: { search, categoryId, brandId, sort },
config: { restBuyerFetch, cartCookie, tokenCookie },
}) => {
//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) {
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

18 changes: 18 additions & 0 deletions framework/ordercloud/api/endpoints/catalog/products/index.ts
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion framework/ordercloud/commerce.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"features": {
"wishlist": false,
"cart": true,
"search": false,
"search": true,
"customerAuth": false,
"customCheckout": true
}
Expand Down
39 changes: 31 additions & 8 deletions framework/ordercloud/product/use-search.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
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: '',
url: '/api/catalog/products',
method: 'GET',
},
async fetcher({ input, options, fetch }) {},
useHook: () => () => {
return {
data: {
products: [],
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')


if (search) url.searchParams.set('search', String(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))

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,
},
}
})
},
}

0 comments on commit 55b9174

Please sign in to comment.