forked from crowetic/commerce
enable add and remove items (update) from cart. Update Search
This commit is contained in:
parent
18936b7544
commit
d0a04a8fe9
@ -23,11 +23,7 @@ export const handler: MutationHook<Cart, {}, CartItemBody> = {
|
||||
message: 'The item quantity has to be a valid integer greater than 0',
|
||||
})
|
||||
}
|
||||
|
||||
const { checkoutLineItemsAdd } = await fetch<
|
||||
Mutation,
|
||||
MutationCheckoutLineItemsAddArgs
|
||||
>({
|
||||
const response = await fetch<Mutation, MutationCheckoutLineItemsAddArgs>({
|
||||
...options,
|
||||
variables: {
|
||||
checkoutId: getCheckoutId(),
|
||||
|
@ -1,12 +1,12 @@
|
||||
import useCart, { UseCart } from '@commerce/cart/use-cart'
|
||||
import { Customer } from '@commerce/types'
|
||||
import { Cart } from '@commerce/types'
|
||||
import { SWRHook } from '@commerce/utils/types'
|
||||
import { normalizeCart } from '../utils/normalize'
|
||||
// import { getCustomerQuery, getCustomerToken } from '../utils'
|
||||
|
||||
export default useCart as UseCart<typeof handler>
|
||||
|
||||
export const handler: SWRHook<Customer | null> = {
|
||||
export const handler: SWRHook<Cart | null> = {
|
||||
fetchOptions: {
|
||||
query: 'cart',
|
||||
method: 'get',
|
||||
|
@ -14,7 +14,6 @@ import useCart from './use-cart'
|
||||
import { handler as removeItemHandler } from './use-remove-item'
|
||||
import type { Cart, LineItem, UpdateCartItemBody } from '../types'
|
||||
import { checkoutToCart } from './utils'
|
||||
import { getCheckoutId, checkoutLineItemUpdateMutation } from '../utils'
|
||||
import { Mutation, MutationCheckoutLineItemsUpdateArgs } from '../schema'
|
||||
|
||||
export type UpdateItemInput<T = any> = T extends LineItem
|
||||
@ -25,7 +24,8 @@ export default useUpdateItem as UseUpdateItem<typeof handler>
|
||||
|
||||
export const handler = {
|
||||
fetchOptions: {
|
||||
query: checkoutLineItemUpdateMutation,
|
||||
query: 'cart',
|
||||
method: 'updateItem',
|
||||
},
|
||||
async fetcher({
|
||||
input: { itemId, item },
|
||||
@ -46,23 +46,14 @@ export const handler = {
|
||||
message: 'The item quantity has to be a valid integer',
|
||||
})
|
||||
}
|
||||
const { checkoutLineItemsUpdate } = await fetch<
|
||||
Mutation,
|
||||
MutationCheckoutLineItemsUpdateArgs
|
||||
>({
|
||||
...options,
|
||||
variables: {
|
||||
checkoutId: getCheckoutId(),
|
||||
lineItems: [
|
||||
{
|
||||
id: itemId,
|
||||
quantity: item.quantity,
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
const response = await fetch<Mutation, MutationCheckoutLineItemsUpdateArgs>(
|
||||
{
|
||||
...options,
|
||||
variables: [item.itemId, { quantity: item.quantity }],
|
||||
}
|
||||
)
|
||||
|
||||
return checkoutToCart(checkoutLineItemsUpdate)
|
||||
return checkoutToCart(response)
|
||||
},
|
||||
useHook: ({
|
||||
fetch,
|
||||
@ -75,13 +66,13 @@ export const handler = {
|
||||
} = {}
|
||||
) => {
|
||||
const { item } = ctx
|
||||
const { mutate } = useCart() as any
|
||||
const { mutate, data: cartData } = useCart() as any
|
||||
|
||||
return useCallback(
|
||||
debounce(async (input: UpdateItemInput<T>) => {
|
||||
const itemId = input.id ?? item?.id
|
||||
const productId = input.productId ?? item?.productId
|
||||
const variantId = input.productId ?? item?.variantId
|
||||
const itemId = cartData.lineItems[0].id
|
||||
const productId = cartData.lineItems[0].productId
|
||||
const variantId = cartData.lineItems[0].variant.id
|
||||
if (!itemId || !productId || !variantId) {
|
||||
throw new ValidationError({
|
||||
message: 'Invalid input used for this operation',
|
||||
@ -91,6 +82,7 @@ export const handler = {
|
||||
const data = await fetch({
|
||||
input: {
|
||||
item: {
|
||||
itemId,
|
||||
productId,
|
||||
variantId,
|
||||
quantity: input.quantity,
|
||||
|
@ -17,26 +17,11 @@ export type CheckoutPayload =
|
||||
const checkoutToCart = (checkoutPayload?: Maybe<CheckoutPayload>): Cart => {
|
||||
if (!checkoutPayload) {
|
||||
throw new CommerceError({
|
||||
message: 'Invalid response from Shopify',
|
||||
message: 'Invalid response from Swell',
|
||||
})
|
||||
}
|
||||
|
||||
const checkout = checkoutPayload?.checkout
|
||||
const userErrors = checkoutPayload?.userErrors
|
||||
|
||||
if (userErrors && userErrors.length) {
|
||||
throw new ValidationError({
|
||||
message: userErrors[0].message,
|
||||
})
|
||||
}
|
||||
|
||||
if (!checkout) {
|
||||
throw new CommerceError({
|
||||
message: 'Invalid response from Shopify',
|
||||
})
|
||||
}
|
||||
|
||||
return normalizeCart(checkout)
|
||||
return normalizeCart(checkoutPayload)
|
||||
}
|
||||
|
||||
export default checkoutToCart
|
||||
|
@ -28,12 +28,11 @@ export const handler: SWRHook<
|
||||
query: getAllProductsQuery,
|
||||
},
|
||||
async fetcher({ input, options, fetch }) {
|
||||
const { categoryId, brandId } = input
|
||||
|
||||
const { categoryId, search } = input
|
||||
const { results, count: found } = await fetch({
|
||||
query: 'products',
|
||||
method: 'list',
|
||||
variables: { category: categoryId },
|
||||
variables: { category: categoryId, search },
|
||||
})
|
||||
|
||||
const products = results.map((product) => normalizeProduct(product))
|
||||
|
@ -1,6 +1,26 @@
|
||||
import * as Core from '@commerce/types'
|
||||
import { CheckoutLineItem } from './schema'
|
||||
|
||||
export type SwellCart = {
|
||||
id: string
|
||||
account_id: number
|
||||
currency: string
|
||||
tax_included_total: number
|
||||
sub_total: number
|
||||
discount_total: number
|
||||
quantity: number
|
||||
items: {
|
||||
id: string
|
||||
product: object
|
||||
price: number
|
||||
variant: boolean
|
||||
quantity: number
|
||||
}
|
||||
date_created: string
|
||||
discounts?: { id: number; amount: number }[]
|
||||
// TODO: add missing fields
|
||||
}
|
||||
|
||||
export interface SwellProduct extends Core.Product {
|
||||
name: string
|
||||
slug: string
|
||||
|
@ -53,7 +53,6 @@ const normalizeProductImages = (images) =>
|
||||
}))
|
||||
|
||||
const normalizeProductVariants = (variants) => {
|
||||
// console.log('variant', variants);
|
||||
return variants?.map(({ id, name, values, price, stock_status }) => ({
|
||||
id,
|
||||
name,
|
||||
|
Loading…
x
Reference in New Issue
Block a user