Skip to content

Commit 02aa040

Browse files
committed
We can now update quantity in the cart
1 parent 91c2d04 commit 02aa040

File tree

4 files changed

+59
-9
lines changed

4 files changed

+59
-9
lines changed

components/Cart/CartPage/CartItem.component.jsx

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,56 @@
11
import { useState } from 'react';
2+
import { v4 } from 'uuid';
23

34
import SVGX from 'components/SVG/SVGX.component';
45

5-
const CartItem = ({ item, products, handleRemoveProductClick }) => {
6+
import { getUpdatedItems } from '../../../utils/functions/functions';
7+
8+
const CartItem = ({
9+
item,
10+
products,
11+
handleRemoveProductClick,
12+
updateCart,
13+
}) => {
614
const [productCount, setProductCount] = useState(item.qty);
715

16+
console.log("Update cart: ");
17+
console.log(updateCart);
18+
19+
/*
20+
* When user changes the quantity, update the cart in localStorage
21+
* Also update the cart in the global Context
22+
*
23+
* @param {Object} event cartKey
24+
*
25+
* @return {void}
26+
*/
27+
const handleQuantityChange = (event, cartKey) => {
28+
if (process.browser) {
29+
event.stopPropagation();
30+
// If the previous update cart mutation request is still processing, then return.
31+
32+
/*if (updateCartProcessing) {
33+
return;
34+
}*/
35+
// If the user tries to delete the count of product, set that to 1 by default ( This will not allow him to reduce it less than zero )
36+
const newQty = event.target.value ? parseInt(event.target.value) : 1;
37+
// Set the new qty in state.
38+
setProductCount(newQty);
39+
if (products.length) {
40+
const updatedItems = getUpdatedItems(products, newQty, cartKey);
41+
42+
updateCart({
43+
variables: {
44+
input: {
45+
clientMutationId: v4(),
46+
items: updatedItems,
47+
},
48+
},
49+
});
50+
}
51+
}
52+
};
53+
854
return (
955
<tr className="bg-gray-100">
1056
<td className="px-4 py-2 border">
@@ -35,9 +81,7 @@ const CartItem = ({ item, products, handleRemoveProductClick }) => {
3581
type="number"
3682
min="1"
3783
defaultValue={productCount}
38-
onChange={() => {
39-
console.log('Changed quantity ...');
40-
}}
84+
onChange={(event) => handleQuantityChange(event, item.cartKey)}
4185
/>
4286
</td>
4387

components/Cart/CartPage/CartItemsContainer.component.jsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,15 @@ const CartItemsContainer = () => {
9191
<PageTitle title="Handlekurv" />
9292
<RegularCart
9393
cart={cart}
94-
handleRemoveProductClick={handleRemoveProductClick}
94+
updateCartProcessing={updateCartProcessing}
95+
handleRemoveProductClick={handleRemoveProductClick}
96+
updateCart={updateCart}
9597
/>
9698
<MobileCart
9799
cart={cart}
98-
handleRemoveProductClick={handleRemoveProductClick}
100+
updateCartProcessing={updateCartProcessing}
101+
handleRemoveProductClick={handleRemoveProductClick}
102+
updateCart={updateCart}
99103
/>
100104
<div className="mt-4">
101105
<Link href="/kasse">

components/Cart/CartPage/MobileCart.component.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import MobileCartItem from './MobileCartItem.component';
22

3-
const MobileCart = ({ cart, handleRemoveProductClick }) => {
3+
const MobileCart = ({ cart, handleRemoveProductClick, updateCart }) => {
44
return (
55
<section
66
//className="container mx-auto bg-white md:hidden lg:hidden xl:hidden"
@@ -124,6 +124,7 @@ const MobileCart = ({ cart, handleRemoveProductClick }) => {
124124
item={item}
125125
products={cart.products}
126126
handleRemoveProductClick={handleRemoveProductClick}
127+
updateCart={updateCart}
127128
/>
128129
))}
129130
</div>

components/Cart/CartPage/RegularCart.component.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import CartItem from './CartItem.component';
22

3-
const RegularCart = ({ cart, handleRemoveProductClick }) => {
3+
const RegularCart = ({ cart, handleRemoveProductClick, updateCart }) => {
44
return (
55
<table className="hidden table-auto md:block lg:block xl:block">
66
<thead>
@@ -28,7 +28,8 @@ const RegularCart = ({ cart, handleRemoveProductClick }) => {
2828
key={item.productId}
2929
item={item}
3030
products={cart.products}
31-
handleRemoveProductClick={handleRemoveProductClick}
31+
handleRemoveProductClick={handleRemoveProductClick}
32+
updateCart={updateCart}
3233
/>
3334
))}
3435
</tbody>

0 commit comments

Comments
 (0)