diff --git a/packages/swell/src/api/operations/get-product.ts b/packages/swell/src/api/operations/get-product.ts index fff62570f..18141ea0c 100644 --- a/packages/swell/src/api/operations/get-product.ts +++ b/packages/swell/src/api/operations/get-product.ts @@ -20,10 +20,6 @@ export default function getProductOperation({ const product = await config.fetch('products', 'get', [variables.slug]) - if (product && product.variants) { - product.variants = product.variants?.results - } - return { product: product ? normalizeProduct(product) : null, } diff --git a/packages/swell/src/auth/use-login.tsx b/packages/swell/src/auth/use-login.tsx index b3fe9f52d..851c02e74 100644 --- a/packages/swell/src/auth/use-login.tsx +++ b/packages/swell/src/auth/use-login.tsx @@ -3,7 +3,6 @@ import type { MutationHook } from '@vercel/commerce/utils/types' import { CommerceError, ValidationError } from '@vercel/commerce/utils/errors' import useCustomer from '../customer/use-customer' import { - CustomerUserError, Mutation, MutationCheckoutCreateArgs, } from '../../schema' @@ -13,13 +12,12 @@ import { setCustomerToken } from '../utils' export default useLogin as UseLogin -const getErrorMessage = ({ code, message }: CustomerUserError) => { +const getErrorMessage = ( code?: string | null) => { switch (code) { case 'UNIDENTIFIED_CUSTOMER': - message = 'Cannot find an account that matches the provided credentials' - break + return 'Cannot find an account that matches the provided credentials' } - return message + return undefined; } export const handler: MutationHook = { @@ -35,7 +33,7 @@ export const handler: MutationHook = { }) } - const { customerAccessTokenCreate } = await fetch< + const response = await fetch< Mutation, MutationCheckoutCreateArgs >({ @@ -43,11 +41,17 @@ export const handler: MutationHook = { variables: [email, password], }) - const errors = customerAccessTokenCreate?.customerUserErrors + if (!response) { + throw new ValidationError({ + message: getErrorMessage('UNIDENTIFIED_CUSTOMER')! + }) + } + const { customerAccessTokenCreate } = response; + const errors = customerAccessTokenCreate?.customerUserErrors if (errors && errors.length) { throw new ValidationError({ - message: getErrorMessage(errors[0]), + message: getErrorMessage(errors[0].code) ?? errors[0].message, }) } const customerAccessToken = customerAccessTokenCreate?.customerAccessToken diff --git a/packages/swell/src/cart/use-add-item.tsx b/packages/swell/src/cart/use-add-item.tsx index 048a1f45d..b78c5acb4 100644 --- a/packages/swell/src/cart/use-add-item.tsx +++ b/packages/swell/src/cart/use-add-item.tsx @@ -3,7 +3,6 @@ import { CommerceError } from '@vercel/commerce/utils/errors' import useAddItem, { UseAddItem } from '@vercel/commerce/cart/use-add-item' import useCart from './use-cart' import { checkoutToCart } from './utils' -import { getCheckoutId } from '../utils' import { useCallback } from 'react' import { AddItemHook } from '../types/cart' @@ -26,10 +25,8 @@ export const handler: MutationHook = { const variables: { product_id: string | undefined variant_id?: string - checkoutId?: string quantity?: number } = { - checkoutId: getCheckoutId(), product_id: item.productId, quantity: item.quantity, } diff --git a/packages/swell/src/product/use-search.tsx b/packages/swell/src/product/use-search.tsx index b996099a4..a23864068 100644 --- a/packages/swell/src/product/use-search.tsx +++ b/packages/swell/src/product/use-search.tsx @@ -1,8 +1,10 @@ import { SWRHook } from '@vercel/commerce/utils/types' import useSearch, { UseSearch } from '@vercel/commerce/product/use-search' import { normalizeProduct } from '../utils' +import { getSubCategories } from '../utils/get-sub-categories' import { SwellProduct } from '../types' import type { SearchProductsHook } from '../types/product' +import { SwellCategory } from '../types/site' export default useSearch as UseSearch @@ -21,16 +23,36 @@ export const handler: SWRHook = { async fetcher({ input, options, fetch }) { const sortMap = new Map([ ['latest-desc', ''], - ['price-asc', 'price_asc'], - ['price-desc', 'price_desc'], + ['price-asc', 'price asc'], + ['price-desc', 'price desc'], ['trending-desc', 'popularity'], ]) - const { categoryId, search, sort = 'latest-desc' } = input + const { brandId, categoryId, search, sort = 'latest-desc' } = input const mappedSort = sortMap.get(sort) + + let subCategories: SwellCategory[] = []; + if (categoryId) { + const { results: categories } = await fetch({ + query: 'categories', + method: 'list', + variables: { + expand: 'children', + limit: 100 //maximum allowed + } + }) + + subCategories = getSubCategories(categoryId.toString(), categories) + } const { results, count: found } = await fetch({ - query: 'products', - method: 'list', - variables: { category: categoryId, search, sort: mappedSort }, + ...options, + variables: { + search, + sort: mappedSort, + $filters: { + brand: brandId, + category: subCategories.map((c) => c.id) + } + } }) const products = results.map((product: SwellProduct) => diff --git a/packages/swell/src/types.ts b/packages/swell/src/types.ts index b02bbd8ca..5366cd411 100644 --- a/packages/swell/src/types.ts +++ b/packages/swell/src/types.ts @@ -77,7 +77,7 @@ export interface SwellProduct { price: number images: any[] options: any[] - variants: any[] + variants: any } export type SwellCustomer = any diff --git a/packages/swell/src/types/site.ts b/packages/swell/src/types/site.ts index 96a2e476e..3d6d5abd6 100644 --- a/packages/swell/src/types/site.ts +++ b/packages/swell/src/types/site.ts @@ -1 +1,8 @@ export * from '@vercel/commerce/types/site' +import { Category } from "@vercel/commerce/types/site" + +export type SwellCategory = Category & { + children?: { + results: Category[] + } +} \ No newline at end of file diff --git a/packages/swell/src/utils/get-sub-categories.ts b/packages/swell/src/utils/get-sub-categories.ts new file mode 100644 index 000000000..f4832315f --- /dev/null +++ b/packages/swell/src/utils/get-sub-categories.ts @@ -0,0 +1,22 @@ +import { CommerceError } from "@vercel/commerce/utils/errors"; +import { SwellCategory } from "../types/site"; + +export const getSubCategories = ( + categoryId: string, + categories: SwellCategory[] +) => { + const result: SwellCategory[] = [] + const queue: string[] = [categoryId] + while (queue.length > 0) { + const curr = queue.shift()! + const category = categories.find((c) => c.id === curr) + if (!category) { + throw new CommerceError({ + message: `Category ${category} not found.` + }) + } + result.push(category) + queue.push(...(category.children?.results.map((c) => c.id) ?? [])) + } + return result +} \ No newline at end of file diff --git a/packages/swell/src/utils/normalize.ts b/packages/swell/src/utils/normalize.ts index 9bbad5961..b0f30699c 100644 --- a/packages/swell/src/utils/normalize.ts +++ b/packages/swell/src/utils/normalize.ts @@ -127,7 +127,7 @@ export function normalizeProduct(swellProduct: SwellProduct): Product { ? options.map((o) => normalizeProductOption(o)) : [] const productVariants = variants - ? normalizeProductVariants(variants, options) + ? normalizeProductVariants(variants.results, options) : [] const productImages = normalizeProductImages(images)