diff --git a/framework/elasticpath/cart/use-add-item.tsx b/framework/elasticpath/cart/use-add-item.tsx index 7f3d1061f..cbc779419 100644 --- a/framework/elasticpath/cart/use-add-item.tsx +++ b/framework/elasticpath/cart/use-add-item.tsx @@ -1,17 +1,30 @@ +import { useCallback } from 'react' + import useAddItem, { UseAddItem } from '@commerce/cart/use-add-item' import { MutationHook } from '@commerce/utils/types' +import useCart from './use-cart' +import epClient from '../utils/ep-client' +import normalizeCart from '../utils/normalize-cart' export default useAddItem as UseAddItem export const handler: MutationHook = { fetchOptions: { query: '', }, - async fetcher({ input, options, fetch }) {}, - useHook: - ({ fetch }) => - () => { - return async function addItem() { - return {} - } - }, -} + async fetcher({ input, options, fetch }) { + return epClient.Cart().AddProduct(input.productId); + }, + useHook: ({ fetch }) => () => { + const { mutate } = useCart() + + return useCallback( + async function addItem(input) { + const data = await fetch({ input }) + const cartData = normalizeCart(data, await epClient.Cart().Items()); + await mutate(cartData, true) + return cartData + }, + [fetch, mutate] + ) + }, +} \ No newline at end of file diff --git a/framework/elasticpath/cart/use-cart.tsx b/framework/elasticpath/cart/use-cart.tsx index b3e509a21..4496c8fd2 100644 --- a/framework/elasticpath/cart/use-cart.tsx +++ b/framework/elasticpath/cart/use-cart.tsx @@ -1,6 +1,8 @@ import { useMemo } from 'react' import { SWRHook } from '@commerce/utils/types' import useCart, { UseCart } from '@commerce/cart/use-cart' +import epClient from '../utils/ep-client' +import normalizeCart from '../utils/normalize-cart' export default useCart as UseCart @@ -8,35 +10,27 @@ export const handler: SWRHook = { fetchOptions: { query: '', }, - async fetcher() { - return { - id: '', - createdAt: '', - currency: { code: '' }, - taxesIncluded: '', - lineItems: [], - lineItemsSubtotalPrice: '', - subtotalPrice: 0, - totalPrice: 0, - } + async fetcher({fetch}) { + const {data:cartData} = await epClient.Cart().Get(); + const {data:cartItems} = await epClient.Cart().Items(); + return normalizeCart(cartData, cartItems); }, - useHook: - ({ useData }) => - (input) => { - return useMemo( - () => - Object.create( - {}, - { - isEmpty: { - get() { - return true - }, - enumerable: true, - }, - } - ), - [] - ) + useHook: ({ useData }) => (input) => { + const response = useData({ + swrOptions: { revalidateOnFocus: false, ...input?.swrOptions }, + }) + console.log("checking data..", epClient.Cart()); + return useMemo( + () => + Object.create(response, { + isEmpty: { + get() { + return (response.data?.lineItems.length ?? 0) <= 0 + }, + enumerable: true, + }, + }), + [response] + ) }, } diff --git a/framework/elasticpath/cart/use-remove-item.tsx b/framework/elasticpath/cart/use-remove-item.tsx index b4ed583b8..ae7485f9f 100644 --- a/framework/elasticpath/cart/use-remove-item.tsx +++ b/framework/elasticpath/cart/use-remove-item.tsx @@ -1,5 +1,10 @@ +import { useCallback } from 'react' + import { MutationHook } from '@commerce/utils/types' import useRemoveItem, { UseRemoveItem } from '@commerce/cart/use-remove-item' +import useCart from './use-cart' +import epClient from '../utils/ep-client' +import normalizeCart from '../utils/normalize-cart' export default useRemoveItem as UseRemoveItem @@ -7,12 +12,20 @@ export const handler: MutationHook = { fetchOptions: { query: '', }, - async fetcher({ input, options, fetch }) {}, - useHook: - ({ fetch }) => - () => { - return async function removeItem(input) { - return {} - } - }, + async fetcher({ input }) { + return epClient.Cart().RemoveItem(input.id); + }, + useHook: ({ fetch }) => () => { + const { mutate } = useCart() + + return useCallback( + async function addItem(input) { + const data = await fetch({ input }) + const cartData = normalizeCart(data, await epClient.Cart().Items()); + await mutate(cartData, true) + return cartData + }, + [fetch, mutate] + ) + }, } diff --git a/framework/elasticpath/cart/use-update-item.tsx b/framework/elasticpath/cart/use-update-item.tsx index 06d703f70..19cfffbc4 100644 --- a/framework/elasticpath/cart/use-update-item.tsx +++ b/framework/elasticpath/cart/use-update-item.tsx @@ -1,5 +1,9 @@ import { MutationHook } from '@commerce/utils/types' import useUpdateItem, { UseUpdateItem } from '@commerce/cart/use-update-item' +import epClient from '../utils/ep-client' +import normalizeCart from '../utils/normalize-cart' +import { useCallback } from 'react' +import useCart from './use-cart' export default useUpdateItem as UseUpdateItem @@ -7,12 +11,27 @@ export const handler: MutationHook = { fetchOptions: { query: '', }, - async fetcher({ input, options, fetch }) {}, - useHook: - ({ fetch }) => - () => { - return async function addItem() { - return {} - } - }, + async fetcher({input}) { + return epClient.Cart().UpdateItem(input.id, input.quantity); + }, + useHook: ({ fetch }) => (ctx) => { + console.log(ctx); + const { mutate } = useCart() + + return useCallback( + async function addItem(input) { + const cartItemsRes = await fetch({ + input: { + ...ctx.item, + ...input + } + }); + const {data:cartObj} = await epClient.Cart().Get(); + const cartData = normalizeCart(cartObj, cartItemsRes.data); + await mutate(cartData, false); + return cartData + }, + [fetch, mutate] + ) + } } diff --git a/framework/elasticpath/utils/normalize-cart.js b/framework/elasticpath/utils/normalize-cart.js new file mode 100644 index 000000000..31089bfee --- /dev/null +++ b/framework/elasticpath/utils/normalize-cart.js @@ -0,0 +1,50 @@ +const normalizeLineItem = ({ + id, + name, + quantity, + product_id:productId, + sku, + image, + value, + unit_price +}) => { + const item = { + id, + variantId: productId, + productId, + name, + quantity, + variant: { + id: productId, + sku, + name, + image: { + url: image.href || '/', + }, + requiresShipping: false, + price: (unit_price.amount/100), + listPrice: (unit_price.amount/100), + }, + path: '', + discounts: [], + options: [], + } + return item +} + +const normalizeCart = async (cart, lineItems) => { + const {with_tax, without_tax} = cart.meta?.display_price; + + return { + id: cart.id, + createdAt: cart.meta.timestamps.created_at, + currency: { code: with_tax.currency }, + taxesIncluded: '', + lineItems: lineItems?.map(normalizeLineItem) ?? [], + lineItemsSubtotalPrice: cart.meta?.display_price.without_tax || 0, + subtotalPrice: (without_tax.amount/100) || 0, + totalPrice: (with_tax.amount/100) || 0, + }; +} + +export default normalizeCart; \ No newline at end of file diff --git a/public/assets/placeholder.png b/public/assets/placeholder.png new file mode 100644 index 000000000..37863fc79 Binary files /dev/null and b/public/assets/placeholder.png differ