Swell fixes

This commit is contained in:
Samuel 2022-05-08 11:04:40 -03:00
parent cf8ff6ccf2
commit 51ea5b8be4
8 changed files with 71 additions and 23 deletions

View File

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

View File

@ -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

View File

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

View File

@ -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)
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) =>

View File

@ -77,7 +77,7 @@ export interface SwellProduct {
price: number
images: any[]
options: any[]
variants: any[]
variants: any
}
export type SwellCustomer = any

View File

@ -1 +1,8 @@
export * from '@vercel/commerce/types/site'
import { Category } from "@vercel/commerce/types/site"
export type SwellCategory = Category & {
children?: {
results: Category[]
}
}

View 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
}

View File

@ -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)