Skip to content

Commit bf093fe

Browse files
committed
.
1 parent fa5906c commit bf093fe

File tree

2 files changed

+38
-33
lines changed

2 files changed

+38
-33
lines changed

src/components/Product/AddToCart.component.tsx

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ const AddToCart = ({
9696
variationId,
9797
fullWidth = false,
9898
}: IProductRootObject) => {
99-
const { setCart } = useContext(CartContext);
99+
const { updateCart } = useContext(CartContext);
100100
const [requestError, setRequestError] = useState<boolean>(false);
101101

102102
const productId = product?.databaseId ? product?.databaseId : variationId;
@@ -107,20 +107,14 @@ const AddToCart = ({
107107
};
108108

109109
// Get cart data query
110-
const { data, refetch } = useQuery(GET_CART, {
110+
const { refetch } = useQuery(GET_CART, {
111111
notifyOnNetworkStatusChange: true,
112-
onCompleted: () => {
113-
// Update cart in the localStorage.
112+
onCompleted: (data) => {
113+
// Update cart in the localStorage and React Context.
114114
const updatedCart = getFormattedCart(data);
115-
116-
if (!updatedCart) {
117-
return;
115+
if (updatedCart) {
116+
updateCart(updatedCart);
118117
}
119-
120-
localStorage.setItem('woocommerce-cart', JSON.stringify(updatedCart));
121-
122-
// Update cart data in React Context.
123-
setCart(updatedCart);
124118
},
125119
});
126120

@@ -129,29 +123,33 @@ const AddToCart = ({
129123
variables: {
130124
input: productQueryInput,
131125
},
132-
133-
onCompleted: () => {
134-
// Update the cart with new values in React context.
135-
refetch();
126+
onCompleted: (data) => {
127+
// Immediately update the cart with new values
128+
const updatedCart = getFormattedCart(data);
129+
if (updatedCart) {
130+
updateCart(updatedCart);
131+
}
136132
},
137-
138133
onError: () => {
139134
setRequestError(true);
140135
},
141136
});
142137

143-
const handleAddToCart = () => {
144-
addToCart();
145-
// Refetch cart after 2 seconds
146-
setTimeout(() => {
147-
refetch();
148-
}, 2000);
138+
const handleAddToCart = async () => {
139+
try {
140+
await addToCart();
141+
// Refetch cart immediately after adding to cart
142+
await refetch();
143+
} catch (error) {
144+
console.error('Error adding to cart:', error);
145+
setRequestError(true);
146+
}
149147
};
150148

151149
return (
152150
<>
153151
<Button
154-
handleButtonClick={() => handleAddToCart()}
152+
handleButtonClick={handleAddToCart}
155153
buttonDisabled={addToCartLoading || requestError}
156154
fullWidth={fullWidth}
157155
>

src/stores/CartProvider.tsx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,37 +49,44 @@ export type TRootObjectNull = RootObject | null | undefined;
4949
interface ICartContext {
5050
cart: RootObject | null | undefined;
5151
setCart: React.Dispatch<React.SetStateAction<TRootObjectNull>>;
52+
updateCart: (newCart: RootObject) => void;
5253
}
5354

5455
const CartState = {
5556
cart: null,
5657
setCart: () => {},
58+
updateCart: () => {},
5759
};
5860

5961
export const CartContext = createContext<ICartContext>(CartState);
6062

6163
/**
6264
* Provides a global application context for the entire application with the cart contents
63-
6465
*/
6566
export const CartProvider = ({ children }: ICartProviderProps) => {
6667
const [cart, setCart] = useState<RootObject | null>();
6768

6869
useEffect(() => {
6970
// Check if we are client-side before we access the localStorage
70-
if (!process.browser) {
71-
return;
72-
}
73-
const localCartData = localStorage.getItem('woocommerce-cart');
71+
if (typeof window !== 'undefined') {
72+
const localCartData = localStorage.getItem('woocommerce-cart');
7473

75-
if (localCartData) {
76-
const cartData: RootObject = JSON.parse(localCartData);
77-
setCart(cartData);
74+
if (localCartData) {
75+
const cartData: RootObject = JSON.parse(localCartData);
76+
setCart(cartData);
77+
}
7878
}
7979
}, []);
8080

81+
const updateCart = (newCart: RootObject) => {
82+
setCart(newCart);
83+
if (typeof window !== 'undefined') {
84+
localStorage.setItem('woocommerce-cart', JSON.stringify(newCart));
85+
}
86+
};
87+
8188
return (
82-
<CartContext.Provider value={{ cart, setCart }}>
89+
<CartContext.Provider value={{ cart, setCart, updateCart }}>
8390
{children}
8491
</CartContext.Provider>
8592
);

0 commit comments

Comments
 (0)