Skip to content

Commit

Permalink
feat: clean up cart provider
Browse files Browse the repository at this point in the history
  • Loading branch information
field123 committed Apr 30, 2024
1 parent c001018 commit 42959eb
Showing 1 changed file with 10 additions and 109 deletions.
119 changes: 10 additions & 109 deletions packages/react-shopper-hooks/src/cart/cart-provider.tsx
Original file line number Diff line number Diff line change
@@ -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<typeof useUpdateCartItem>
useScopedRemoveCartItem: () => ReturnType<typeof useRemoveCartItem>
useScopedAddPromotion: () => ReturnType<typeof useAddPromotionToCart>
useScopedRemovePromotion: () => ReturnType<typeof useRemovePromotionCode>
useScopedAddProductToCart: () => ReturnType<typeof useAddProductToCart>
useScopedAddBundleProductToCart: () => ReturnType<
typeof useAddBundleProductToCart
>
useClearCart: () => ReturnType<typeof useDeleteCartItems>
} & Omit<ReturnType<typeof useGetCart>, "data">)
| undefined
>(undefined)
Expand All @@ -52,104 +28,29 @@ 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({
data: rawCartData,
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 (
<CartItemsContext.Provider
value={{
state,
emit,
cartId: cartId ? cartId : undefined,
useScopedUpdateCartItem: updateCartItem,
useScopedRemoveCartItem: removeCartItem,
useScopedAddPromotion: addPromotion,
useScopedRemovePromotion: removePromotion,
useScopedAddProductToCart: addProductToCart,
useScopedAddBundleProductToCart: addBundleItemToCart,
useClearCart: clearCart,
cartId,
...rest,
}}
>
Expand All @@ -158,7 +59,7 @@ export function CartProvider({
)
}

function createCartItemsUpdater(updatedData: CartItem[]) {
export function createCartItemsUpdater(updatedData: CartItem[]) {
return function cartItemsUpdater(
oldData: ResourceIncluded<Cart, CartIncluded>,
) {
Expand Down

0 comments on commit 42959eb

Please sign in to comment.