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])
|
const product = await config.fetch('products', 'get', [variables.slug])
|
||||||
|
|
||||||
if (product && product.variants) {
|
|
||||||
product.variants = product.variants?.results
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
product: product ? normalizeProduct(product) : null,
|
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 { CommerceError, ValidationError } from '@vercel/commerce/utils/errors'
|
||||||
import useCustomer from '../customer/use-customer'
|
import useCustomer from '../customer/use-customer'
|
||||||
import {
|
import {
|
||||||
CustomerUserError,
|
|
||||||
Mutation,
|
Mutation,
|
||||||
MutationCheckoutCreateArgs,
|
MutationCheckoutCreateArgs,
|
||||||
} from '../../schema'
|
} from '../../schema'
|
||||||
@ -13,13 +12,12 @@ import { setCustomerToken } from '../utils'
|
|||||||
|
|
||||||
export default useLogin as UseLogin<typeof handler>
|
export default useLogin as UseLogin<typeof handler>
|
||||||
|
|
||||||
const getErrorMessage = ({ code, message }: CustomerUserError) => {
|
const getErrorMessage = ( code?: string | null) => {
|
||||||
switch (code) {
|
switch (code) {
|
||||||
case 'UNIDENTIFIED_CUSTOMER':
|
case 'UNIDENTIFIED_CUSTOMER':
|
||||||
message = 'Cannot find an account that matches the provided credentials'
|
return 'Cannot find an account that matches the provided credentials'
|
||||||
break
|
|
||||||
}
|
}
|
||||||
return message
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const handler: MutationHook<LoginHook> = {
|
export const handler: MutationHook<LoginHook> = {
|
||||||
@ -35,7 +33,7 @@ export const handler: MutationHook<LoginHook> = {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const { customerAccessTokenCreate } = await fetch<
|
const response = await fetch<
|
||||||
Mutation,
|
Mutation,
|
||||||
MutationCheckoutCreateArgs
|
MutationCheckoutCreateArgs
|
||||||
>({
|
>({
|
||||||
@ -43,11 +41,17 @@ export const handler: MutationHook<LoginHook> = {
|
|||||||
variables: [email, password],
|
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) {
|
if (errors && errors.length) {
|
||||||
throw new ValidationError({
|
throw new ValidationError({
|
||||||
message: getErrorMessage(errors[0]),
|
message: getErrorMessage(errors[0].code) ?? errors[0].message,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
const customerAccessToken = customerAccessTokenCreate?.customerAccessToken
|
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 useAddItem, { UseAddItem } from '@vercel/commerce/cart/use-add-item'
|
||||||
import useCart from './use-cart'
|
import useCart from './use-cart'
|
||||||
import { checkoutToCart } from './utils'
|
import { checkoutToCart } from './utils'
|
||||||
import { getCheckoutId } from '../utils'
|
|
||||||
import { useCallback } from 'react'
|
import { useCallback } from 'react'
|
||||||
import { AddItemHook } from '../types/cart'
|
import { AddItemHook } from '../types/cart'
|
||||||
|
|
||||||
@ -26,10 +25,8 @@ export const handler: MutationHook<AddItemHook> = {
|
|||||||
const variables: {
|
const variables: {
|
||||||
product_id: string | undefined
|
product_id: string | undefined
|
||||||
variant_id?: string
|
variant_id?: string
|
||||||
checkoutId?: string
|
|
||||||
quantity?: number
|
quantity?: number
|
||||||
} = {
|
} = {
|
||||||
checkoutId: getCheckoutId(),
|
|
||||||
product_id: item.productId,
|
product_id: item.productId,
|
||||||
quantity: item.quantity,
|
quantity: item.quantity,
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
import { SWRHook } from '@vercel/commerce/utils/types'
|
import { SWRHook } from '@vercel/commerce/utils/types'
|
||||||
import useSearch, { UseSearch } from '@vercel/commerce/product/use-search'
|
import useSearch, { UseSearch } from '@vercel/commerce/product/use-search'
|
||||||
import { normalizeProduct } from '../utils'
|
import { normalizeProduct } from '../utils'
|
||||||
|
import { getSubCategories } from '../utils/get-sub-categories'
|
||||||
import { SwellProduct } from '../types'
|
import { SwellProduct } from '../types'
|
||||||
import type { SearchProductsHook } from '../types/product'
|
import type { SearchProductsHook } from '../types/product'
|
||||||
|
import { SwellCategory } from '../types/site'
|
||||||
|
|
||||||
export default useSearch as UseSearch<typeof handler>
|
export default useSearch as UseSearch<typeof handler>
|
||||||
|
|
||||||
@ -21,16 +23,36 @@ export const handler: SWRHook<SearchProductsHook> = {
|
|||||||
async fetcher({ input, options, fetch }) {
|
async fetcher({ input, options, fetch }) {
|
||||||
const sortMap = new Map([
|
const sortMap = new Map([
|
||||||
['latest-desc', ''],
|
['latest-desc', ''],
|
||||||
['price-asc', 'price_asc'],
|
['price-asc', 'price asc'],
|
||||||
['price-desc', 'price_desc'],
|
['price-desc', 'price desc'],
|
||||||
['trending-desc', 'popularity'],
|
['trending-desc', 'popularity'],
|
||||||
])
|
])
|
||||||
const { categoryId, search, sort = 'latest-desc' } = input
|
const { brandId, categoryId, search, sort = 'latest-desc' } = input
|
||||||
const mappedSort = sortMap.get(sort)
|
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({
|
const { results, count: found } = await fetch({
|
||||||
query: 'products',
|
...options,
|
||||||
method: 'list',
|
variables: {
|
||||||
variables: { category: categoryId, search, sort: mappedSort },
|
search,
|
||||||
|
sort: mappedSort,
|
||||||
|
$filters: {
|
||||||
|
brand: brandId,
|
||||||
|
category: subCategories.map((c) => c.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const products = results.map((product: SwellProduct) =>
|
const products = results.map((product: SwellProduct) =>
|
||||||
|
@ -77,7 +77,7 @@ export interface SwellProduct {
|
|||||||
price: number
|
price: number
|
||||||
images: any[]
|
images: any[]
|
||||||
options: any[]
|
options: any[]
|
||||||
variants: any[]
|
variants: any
|
||||||
}
|
}
|
||||||
|
|
||||||
export type SwellCustomer = any
|
export type SwellCustomer = any
|
||||||
|
@ -1 +1,8 @@
|
|||||||
export * from '@vercel/commerce/types/site'
|
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))
|
? options.map((o) => normalizeProductOption(o))
|
||||||
: []
|
: []
|
||||||
const productVariants = variants
|
const productVariants = variants
|
||||||
? normalizeProductVariants(variants, options)
|
? normalizeProductVariants(variants.results, options)
|
||||||
: []
|
: []
|
||||||
|
|
||||||
const productImages = normalizeProductImages(images)
|
const productImages = normalizeProductImages(images)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user