From abd86329d5c862cc1467544d99b60fe1503a8dc4 Mon Sep 17 00:00:00 2001 From: Greg Hoskin Date: Sun, 4 Apr 2021 18:07:14 -0600 Subject: [PATCH] update remove item from cart, landing page fetch, image normalization --- framework/swell/cart/use-remove-item.tsx | 19 ++++++++++-------- framework/swell/product/get-all-products.ts | 5 +++-- framework/swell/utils/normalize.ts | 22 +++++++++++++++++---- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/framework/swell/cart/use-remove-item.tsx b/framework/swell/cart/use-remove-item.tsx index e2aef13d8..13d137da4 100644 --- a/framework/swell/cart/use-remove-item.tsx +++ b/framework/swell/cart/use-remove-item.tsx @@ -31,27 +31,30 @@ export default useRemoveItem as UseRemoveItem export const handler = { fetchOptions: { - query: checkoutLineItemRemoveMutation, + query: 'cart', + method: 'removeItem', }, async fetcher({ input: { itemId }, options, fetch, }: HookFetcherContext) { - const data = await fetch({ - ...options, - variables: { checkoutId: getCheckoutId(), lineItemIds: [itemId] }, - }) - return checkoutToCart(data.checkoutLineItemsRemove) + const response = await fetch( + { + ...options, + variables: [itemId], + } + ) + return checkoutToCart(response) }, useHook: ({ fetch, }: MutationHookContext) => < T extends LineItem | undefined = undefined >( - ctx: { item?: T } = {} + item ) => { - const { item } = ctx + // const { item } = ctx const { mutate } = useCart() const removeItem: RemoveItemFn = async (input) => { const itemId = input?.id ?? item?.id diff --git a/framework/swell/product/get-all-products.ts b/framework/swell/product/get-all-products.ts index 8ca85eecd..5150af993 100644 --- a/framework/swell/product/get-all-products.ts +++ b/framework/swell/product/get-all-products.ts @@ -21,9 +21,10 @@ const getAllProducts = async (options: { }): Promise => { let { config, variables = { first: 250 } } = options ?? {} config = getConfig(config) - const { results } = await config.fetchSwell('products', 'get') + const { results } = await config.fetchSwell('products', 'list', { + limit: variables.first, + }) const products = results.map((product) => normalizeProduct(product)) ?? [] - return { products, } diff --git a/framework/swell/utils/normalize.ts b/framework/swell/utils/normalize.ts index 2707489a6..5a95d94dc 100644 --- a/framework/swell/utils/normalize.ts +++ b/framework/swell/utils/normalize.ts @@ -46,11 +46,25 @@ const normalizeProductOption = ({ } } -const normalizeProductImages = (images) => - images?.map(({ file, id }) => ({ - url: file.url, - id, +type SwellImage = { + file: { + url: String + height: Number + width: Number + } + id: string +} +const normalizeProductImages = (images) => { + if (!images) { + return [{ url: '/' }] + } + return images?.map(({ file, ...rest }: SwellImage) => ({ + url: file?.url, + height: file.height, + width: file.width, + ...rest, })) +} const normalizeProductVariants = (variants) => { return variants?.map(({ id, name, values, price, stock_status }) => ({