From d0a04a8fe957fbd29d8fde84f3d826c8d0d82a04 Mon Sep 17 00:00:00 2001 From: Greg Hoskin Date: Sun, 4 Apr 2021 16:16:27 -0600 Subject: [PATCH] enable add and remove items (update) from cart. Update Search --- framework/swell/cart/use-add-item.tsx | 6 +--- framework/swell/cart/use-cart.tsx | 4 +-- framework/swell/cart/use-update-item.tsx | 36 ++++++++----------- .../swell/cart/utils/checkout-to-cart.ts | 19 ++-------- framework/swell/product/use-search.tsx | 5 ++- framework/swell/types.ts | 20 +++++++++++ framework/swell/utils/normalize.ts | 1 - 7 files changed, 41 insertions(+), 50 deletions(-) diff --git a/framework/swell/cart/use-add-item.tsx b/framework/swell/cart/use-add-item.tsx index d0f891148..34bdb3df8 100644 --- a/framework/swell/cart/use-add-item.tsx +++ b/framework/swell/cart/use-add-item.tsx @@ -23,11 +23,7 @@ export const handler: MutationHook = { message: 'The item quantity has to be a valid integer greater than 0', }) } - - const { checkoutLineItemsAdd } = await fetch< - Mutation, - MutationCheckoutLineItemsAddArgs - >({ + const response = await fetch({ ...options, variables: { checkoutId: getCheckoutId(), diff --git a/framework/swell/cart/use-cart.tsx b/framework/swell/cart/use-cart.tsx index c9589f484..183a9ad6d 100644 --- a/framework/swell/cart/use-cart.tsx +++ b/framework/swell/cart/use-cart.tsx @@ -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 -export const handler: SWRHook = { +export const handler: SWRHook = { fetchOptions: { query: 'cart', method: 'get', diff --git a/framework/swell/cart/use-update-item.tsx b/framework/swell/cart/use-update-item.tsx index 666ce3d08..3155778c6 100644 --- a/framework/swell/cart/use-update-item.tsx +++ b/framework/swell/cart/use-update-item.tsx @@ -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 extends LineItem @@ -25,7 +24,8 @@ export default useUpdateItem as UseUpdateItem 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( + { + ...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) => { - 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, diff --git a/framework/swell/cart/utils/checkout-to-cart.ts b/framework/swell/cart/utils/checkout-to-cart.ts index 03005f342..19a8bfd0b 100644 --- a/framework/swell/cart/utils/checkout-to-cart.ts +++ b/framework/swell/cart/utils/checkout-to-cart.ts @@ -17,26 +17,11 @@ export type CheckoutPayload = const checkoutToCart = (checkoutPayload?: Maybe): 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 diff --git a/framework/swell/product/use-search.tsx b/framework/swell/product/use-search.tsx index 4e55da22a..204c65352 100644 --- a/framework/swell/product/use-search.tsx +++ b/framework/swell/product/use-search.tsx @@ -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)) diff --git a/framework/swell/types.ts b/framework/swell/types.ts index 2878295e1..0d1e85657 100644 --- a/framework/swell/types.ts +++ b/framework/swell/types.ts @@ -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 diff --git a/framework/swell/utils/normalize.ts b/framework/swell/utils/normalize.ts index bbb974c5e..2707489a6 100644 --- a/framework/swell/utils/normalize.ts +++ b/framework/swell/utils/normalize.ts @@ -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,