diff --git a/packages/react-shopper-hooks/src/cart/cart-provider.tsx b/packages/react-shopper-hooks/src/cart/cart-provider.tsx index 26f17498..1056e8b8 100644 --- a/packages/react-shopper-hooks/src/cart/cart-provider.tsx +++ b/packages/react-shopper-hooks/src/cart/cart-provider.tsx @@ -1,40 +1,16 @@ import React, { createContext, ReactNode } from "react" -import { - Cart, - CartIncluded, - ResourceIncluded, - CartItem, - CartItemsResponse, -} from "@moltin/sdk" +import { Cart, CartIncluded, ResourceIncluded, CartItem } from "@moltin/sdk" import { CartState } from "./types/cart-types" import { enhanceCartResponse } from "./util/enhance-cart-response" import { StoreEvent } from "../shared" -import { cartQueryKeys, useGetCart } from "./hooks/use-get-cart" -import { useUpdateCartItem } from "./hooks/use-update-cart-items" -import { useQueryClient } from "@tanstack/react-query" -import { useRemoveCartItem } from "./hooks/use-remove-cart-item" -import { - useAddBundleProductToCart, - useAddProductToCart, - useAddPromotionToCart, - useDeleteCartItems, -} from "./hooks" -import { useRemovePromotionCode } from "./hooks/use-remove-promotion" +import { useGetCart } from "./hooks/use-get-cart" +import { useElasticPath } from "../elasticpath" export const CartItemsContext = createContext< | ({ state: CartState | undefined - cartId?: string + cartId: string emit?: (event: StoreEvent) => void - useScopedUpdateCartItem: () => ReturnType - useScopedRemoveCartItem: () => ReturnType - useScopedAddPromotion: () => ReturnType - useScopedRemovePromotion: () => ReturnType - useScopedAddProductToCart: () => ReturnType - useScopedAddBundleProductToCart: () => ReturnType< - typeof useAddBundleProductToCart - > - useClearCart: () => ReturnType } & Omit, "data">) | undefined >(undefined) @@ -52,28 +28,16 @@ export function CartProvider({ initialState, children, emit, - cartId = "", + cartId: sourceCartId, }: CartProviderProps) { - const queryClient = useQueryClient() + const { client } = useElasticPath() + + const cartId = sourceCartId ?? client.Cart().cartId const { data: rawCartData, ...rest } = useGetCart(cartId, { initialData: initialState?.cart, }) - async function invalidateCartQuery() { - return queryClient.invalidateQueries({ - queryKey: cartQueryKeys.detail(cartId), - }) - } - - function setCartQueryData(updatedData: CartItemsResponse) { - // Updates the cart items in the query cache - return queryClient.setQueryData( - cartQueryKeys.detail(cartId), - createCartItemsUpdater(updatedData.data), - ) - } - const state = rawCartData && enhanceCartResponse({ @@ -81,75 +45,12 @@ export function CartProvider({ included: rest.included, }) - const updateCartItem = () => - useUpdateCartItem(cartId, { - onSuccess: (updatedData) => { - setCartQueryData(updatedData) - invalidateCartQuery() - }, - }) - - const addProductToCart = () => - useAddProductToCart(cartId, { - onSuccess: (updatedData) => { - setCartQueryData(updatedData) - invalidateCartQuery() - }, - }) - - const removeCartItem = () => - useRemoveCartItem(cartId, { - onSuccess: (updatedData) => { - setCartQueryData(updatedData) - invalidateCartQuery() - }, - }) - - const addPromotion = () => - useAddPromotionToCart(cartId, { - onSuccess: (updatedData) => { - setCartQueryData(updatedData) - invalidateCartQuery() - }, - }) - - const removePromotion = () => - useRemovePromotionCode(cartId, { - onSuccess: (updatedData) => { - setCartQueryData(updatedData) - invalidateCartQuery() - }, - }) - - const addBundleItemToCart = () => - useAddBundleProductToCart(cartId, { - onSuccess: (updatedData) => { - setCartQueryData(updatedData) - invalidateCartQuery() - }, - }) - - const clearCart = () => - useDeleteCartItems(cartId, { - onSuccess: async (updatedData) => { - setCartQueryData(updatedData) - await invalidateCartQuery() - }, - }) - return ( @@ -158,7 +59,7 @@ export function CartProvider({ ) } -function createCartItemsUpdater(updatedData: CartItem[]) { +export function createCartItemsUpdater(updatedData: CartItem[]) { return function cartItemsUpdater( oldData: ResourceIncluded, ) {