Skip to content

Commit 7120425

Browse files
authored
Merge pull request #1353 from w3bdesign/develop
Fix add to cart
2 parents f634c8e + 570271d commit 7120425

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

src/components/Product/AddToCart.component.tsx

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,13 @@ export interface IProductRootObject {
9090
* @param {number} variationId // Variation ID
9191
* @param {boolean} fullWidth // Whether the button should be full-width
9292
*/
93+
9394
const AddToCart = ({
9495
product,
9596
variationId,
9697
fullWidth = false,
9798
}: IProductRootObject) => {
98-
const { updateCart } = useContext(CartContext);
99+
const { setCart, isLoading: isCartLoading } = useContext(CartContext);
99100
const [requestError, setRequestError] = useState<boolean>(false);
100101

101102
const productId = product?.databaseId ? product?.databaseId : variationId;
@@ -106,14 +107,20 @@ const AddToCart = ({
106107
};
107108

108109
// Get cart data query
109-
const { refetch } = useQuery(GET_CART, {
110+
const { data, refetch } = useQuery(GET_CART, {
110111
notifyOnNetworkStatusChange: true,
111-
onCompleted: (data) => {
112-
// Update cart in the localStorage and React Context.
112+
onCompleted: () => {
113+
// Update cart in the localStorage.
113114
const updatedCart = getFormattedCart(data);
114-
if (updatedCart) {
115-
updateCart(updatedCart);
115+
116+
if (!updatedCart) {
117+
return;
116118
}
119+
120+
localStorage.setItem('woocommerce-cart', JSON.stringify(updatedCart));
121+
122+
// Update cart data in React Context.
123+
setCart(updatedCart);
117124
},
118125
});
119126

@@ -122,36 +129,33 @@ const AddToCart = ({
122129
variables: {
123130
input: productQueryInput,
124131
},
125-
onCompleted: (data) => {
126-
// Immediately update the cart with new values
127-
const updatedCart = getFormattedCart(data);
128-
if (updatedCart) {
129-
updateCart(updatedCart);
130-
}
132+
133+
onCompleted: () => {
134+
// Update the cart with new values in React context.
135+
refetch();
131136
},
137+
132138
onError: () => {
133139
setRequestError(true);
134140
},
135141
});
136142

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

147151
return (
148152
<>
149153
<Button
150-
handleButtonClick={handleAddToCart}
151-
buttonDisabled={addToCartLoading || requestError}
154+
handleButtonClick={() => handleAddToCart()}
155+
buttonDisabled={addToCartLoading || requestError || isCartLoading}
152156
fullWidth={fullWidth}
153157
>
154-
KJØP
158+
{isCartLoading ? 'Loading...' : 'KJØP'}
155159
</Button>
156160
</>
157161
);

src/stores/CartProvider.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@ interface ICartContext {
5151
cart: RootObject | null | undefined;
5252
setCart: React.Dispatch<React.SetStateAction<TRootObjectNull>>;
5353
updateCart: (newCart: RootObject) => void;
54+
isLoading: boolean;
5455
}
5556

5657
const CartState: ICartContext = {
5758
cart: null,
5859
setCart: () => {},
5960
updateCart: () => {},
61+
isLoading: true,
6062
};
6163

6264
export const CartContext = createContext<ICartContext>(CartState);
@@ -66,6 +68,7 @@ export const CartContext = createContext<ICartContext>(CartState);
6668
*/
6769
export const CartProvider = ({ children }: ICartProviderProps) => {
6870
const [cart, setCart] = useState<RootObject | null>();
71+
const [isLoading, setIsLoading] = useState(true);
6972

7073
useEffect(() => {
7174
// Check if we are client-side before we access the localStorage
@@ -75,6 +78,7 @@ export const CartProvider = ({ children }: ICartProviderProps) => {
7578
const cartData: RootObject = JSON.parse(localCartData);
7679
setCart(cartData);
7780
}
81+
setIsLoading(false);
7882
}
7983
}, []);
8084

@@ -86,8 +90,8 @@ export const CartProvider = ({ children }: ICartProviderProps) => {
8690
};
8791

8892
const contextValue = useMemo(() => {
89-
return { cart, setCart, updateCart };
90-
}, [cart]);
93+
return { cart, setCart, updateCart, isLoading };
94+
}, [cart, isLoading]);
9195

9296
return (
9397
<CartContext.Provider value={contextValue}>

0 commit comments

Comments
 (0)