@@ -9,12 +9,15 @@ import React, {
99import { atom , useAtom } from 'jotai' ;
1010import { useShopifyCheckoutKit } from 'react-native-shopify-checkout-kit' ;
1111import useShopify from '../hooks/useShopify' ;
12+ import { useConfig } from './Config' ;
13+ import { createBuyerIdentityCartInput } from '../utils' ;
1214
1315interface Context {
1416 cartId : string | undefined ;
1517 checkoutURL : string | undefined ;
1618 totalQuantity : number ;
1719 addingToCart : Set < string > ;
20+ clearCart : ( ) => void ;
1821 addToCart : ( variantId : string ) => Promise < void > ;
1922 removeFromCart : ( variantId : string ) => Promise < void > ;
2023}
@@ -28,6 +31,7 @@ const CartContext = createContext<Context>({
2831 addingToCart : new Set ( ) ,
2932 addToCart : async ( ) => { } ,
3033 removeFromCart : async ( ) => { } ,
34+ clearCart : ( ) => { } ,
3135} ) ;
3236
3337type AddingToCartAction =
@@ -63,26 +67,37 @@ export const CartProvider: React.FC<PropsWithChildren> = ({children}) => {
6367 // Maintain a loading state for items being added to the cart
6468 const defaultSet : Set < string > = new Set ( ) ;
6569 const [ addingToCart , dispatch ] = useReducer ( addingToCartReducer , defaultSet ) ;
70+ const { appConfig} = useConfig ( ) ;
6671
6772 const { mutations, queries} = useShopify ( ) ;
6873 const [ createCart ] = mutations . cartCreate ;
6974 const [ addLineItems ] = mutations . cartLinesAdd ;
7075 const [ removeLineItems ] = mutations . cartLinesRemove ;
7176 const [ fetchCart ] = queries . cart ;
7277
78+ const clearCart = useCallback ( ( ) => {
79+ setCartId ( defaultCartId ) ;
80+ setCheckoutURL ( undefined ) ;
81+ setTotalQuantity ( 0 ) ;
82+ } , [ setCartId , setCheckoutURL , setTotalQuantity ] ) ;
83+
7384 useEffect ( ( ) => {
7485 const subscription = ShopifyCheckoutKit . addEventListener (
7586 'completed' ,
7687 ( ) => {
7788 // Clear the cart ID and checkout URL when the checkout is completed
78- setCartId ( defaultCartId ) ;
79- setCheckoutURL ( undefined ) ;
80- setTotalQuantity ( 0 ) ;
89+ clearCart ( ) ;
8190 } ,
8291 ) ;
8392
8493 return subscription ?. remove ;
85- } , [ ShopifyCheckoutKit , setCartId , setCheckoutURL , setTotalQuantity ] ) ;
94+ } , [
95+ ShopifyCheckoutKit ,
96+ clearCart ,
97+ setCartId ,
98+ setCheckoutURL ,
99+ setTotalQuantity ,
100+ ] ) ;
86101
87102 useEffect ( ( ) => {
88103 async function getCart ( ) {
@@ -110,7 +125,8 @@ export const CartProvider: React.FC<PropsWithChildren> = ({children}) => {
110125 dispatch ( { type : 'add' , variantId} ) ;
111126
112127 if ( ! id ) {
113- const cart = await createCart ( ) ;
128+ const cartInput = createBuyerIdentityCartInput ( appConfig ) ;
129+ const cart = await createCart ( { variables : { input : cartInput } } ) ;
114130 id = cart . data . cartCreate . cart . id ;
115131
116132 if ( id ) {
@@ -147,6 +163,7 @@ export const CartProvider: React.FC<PropsWithChildren> = ({children}) => {
147163 addLineItems ,
148164 setCheckoutURL ,
149165 setTotalQuantity ,
166+ appConfig ,
150167 createCart ,
151168 setCartId ,
152169 ShopifyCheckoutKit ,
@@ -205,6 +222,7 @@ export const CartProvider: React.FC<PropsWithChildren> = ({children}) => {
205222 removeFromCart,
206223 totalQuantity,
207224 addingToCart,
225+ clearCart,
208226 } ) ,
209227 [
210228 cartId ,
@@ -213,6 +231,7 @@ export const CartProvider: React.FC<PropsWithChildren> = ({children}) => {
213231 removeFromCart ,
214232 totalQuantity ,
215233 addingToCart ,
234+ clearCart ,
216235 ] ,
217236 ) ;
218237
0 commit comments