mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 07:26:59 +00:00
Swell fixes
This commit is contained in:
parent
cf8ff6ccf2
commit
51ea5b8be4
@ -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,
|
||||
}
|
||||
|
@ -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<typeof handler>
|
||||
|
||||
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<LoginHook> = {
|
||||
@ -35,7 +33,7 @@ export const handler: MutationHook<LoginHook> = {
|
||||
})
|
||||
}
|
||||
|
||||
const { customerAccessTokenCreate } = await fetch<
|
||||
const response = await fetch<
|
||||
Mutation,
|
||||
MutationCheckoutCreateArgs
|
||||
>({
|
||||
@ -43,11 +41,17 @@ export const handler: MutationHook<LoginHook> = {
|
||||
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
|
||||
|
@ -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<AddItemHook> = {
|
||||
const variables: {
|
||||
product_id: string | undefined
|
||||
variant_id?: string
|
||||
checkoutId?: string
|
||||
quantity?: number
|
||||
} = {
|
||||
checkoutId: getCheckoutId(),
|
||||
product_id: item.productId,
|
||||
quantity: item.quantity,
|
||||
}
|
||||
|
@ -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<typeof handler>
|
||||
|
||||
@ -21,16 +23,36 @@ export const handler: SWRHook<SearchProductsHook> = {
|
||||
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)
|
||||
const { results, count: found } = await fetch({
|
||||
query: 'products',
|
||||
|
||||
let subCategories: SwellCategory[] = [];
|
||||
if (categoryId) {
|
||||
const { results: categories } = await fetch({
|
||||
query: 'categories',
|
||||
method: 'list',
|
||||
variables: { category: categoryId, search, sort: mappedSort },
|
||||
variables: {
|
||||
expand: 'children',
|
||||
limit: 100 //maximum allowed
|
||||
}
|
||||
})
|
||||
|
||||
subCategories = getSubCategories(categoryId.toString(), categories)
|
||||
}
|
||||
const { results, count: found } = await fetch({
|
||||
...options,
|
||||
variables: {
|
||||
search,
|
||||
sort: mappedSort,
|
||||
$filters: {
|
||||
brand: brandId,
|
||||
category: subCategories.map((c) => c.id)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const products = results.map((product: SwellProduct) =>
|
||||
|
@ -77,7 +77,7 @@ export interface SwellProduct {
|
||||
price: number
|
||||
images: any[]
|
||||
options: any[]
|
||||
variants: any[]
|
||||
variants: any
|
||||
}
|
||||
|
||||
export type SwellCustomer = any
|
||||
|
@ -1 +1,8 @@
|
||||
export * from '@vercel/commerce/types/site'
|
||||
import { Category } from "@vercel/commerce/types/site"
|
||||
|
||||
export type SwellCategory = Category & {
|
||||
children?: {
|
||||
results: Category[]
|
||||
}
|
||||
}
|
22
packages/swell/src/utils/get-sub-categories.ts
Normal file
22
packages/swell/src/utils/get-sub-categories.ts
Normal file
@ -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
|
||||
}
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user