Skip to content

Commit b287733

Browse files
committed
Mobile cart is working!
1 parent 140c3ec commit b287733

File tree

3 files changed

+68
-62
lines changed

3 files changed

+68
-62
lines changed

components/Cart/CartPage/CartItem.component.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { useState } from 'react';
22
import { v4 } from 'uuid';
33

44
import SVGX from 'components/SVG/SVGX.component';
5-
65
import { getUpdatedItems } from 'utils/functions/functions';
76

87
const CartItem = ({ item, products, handleRemoveProductClick, updateCart }) => {

components/Cart/CartPage/MobileCart.component.jsx

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,57 +24,21 @@ const MobileCart = ({ cart, handleRemoveProductClick, updateCart }) => {
2424
</tr>
2525
))}
2626
</thead>
27-
2827
<tbody className="flex-1 sm:flex-none">
29-
{/*
30-
{cart.products.length &&
31-
cart.products.map((item) => (
32-
33-
<MobileCartItem
34-
key={item.productId}
35-
item={item}
36-
products={cart.products}
37-
handleRemoveProductClick={handleRemoveProductClick}
38-
updateCart={updateCart}
39-
/>
40-
))}
41-
42-
*/}
43-
4428
{cart.products.length &&
4529
cart.products.map((item) => (
46-
<tr className="flex flex-col mb-2 flex-no wrap sm:table-row sm:mb-0">
47-
<td className="h-12 p-3 border-2 border-gray-400 ">
48-
<SVGX />
49-
</td>
50-
<td className="h-12 p-3 border-l-2 border-r-2 border-gray-400 ">
51-
{item.name}
52-
</td>
53-
<td className="h-12 p-3 border-2 border-gray-400 ">
54-
kr{item.price.toFixed(2)}
55-
</td>
56-
<td className="h-12 p-3 border-l-2 border-r-2 border-gray-400 ">
57-
{item.qty}
58-
</td>
59-
<td className="h-12 p-3 border-2 border-gray-400 ">
60-
{item.totalPrice}
61-
</td>
62-
</tr>
30+
<MobileCartItem
31+
key={item.productId}
32+
item={item}
33+
products={cart.products}
34+
handleRemoveProductClick={handleRemoveProductClick}
35+
updateCart={updateCart}
36+
/>
6337
))}
6438
</tbody>
6539
</table>
6640
</div>
6741
</div>
68-
69-
{/*
70-
<MobileCartItem
71-
key={item.productId}
72-
item={item}
73-
products={cart.products}
74-
handleRemoveProductClick={handleRemoveProductClick}
75-
updateCart={updateCart}
76-
/>
77-
*/}
7842
</section>
7943
);
8044
};

components/Cart/CartPage/MobileCartItem.component.jsx

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,82 @@
11
import { useState } from 'react';
2+
import { v4 } from 'uuid';
23

34
import SVGX from 'components/SVG/SVGX.component';
5+
import { getUpdatedItems } from 'utils/functions/functions';
46

5-
const MobileCartItem = ({ item }) => {
7+
const MobileCartItem = ({
8+
item,
9+
products,
10+
handleRemoveProductClick,
11+
updateCart,
12+
}) => {
613
const [productCount, setProductCount] = useState(item.qty);
714

15+
/*
16+
* When user changes the quantity, update the cart in localStorage
17+
* Also update the cart in the global Context
18+
*
19+
* @param {Object} event cartKey
20+
*
21+
* @return {void}
22+
*/
23+
const handleQuantityChange = (event, cartKey) => {
24+
if (process.browser) {
25+
event.stopPropagation();
26+
// Return if the previous update cart mutation request is still processing
27+
/*
28+
if (updateCartProcessing) {
29+
return;
30+
}*/
31+
// 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 )
32+
const newQty = event.target.value ? parseInt(event.target.value) : 1;
33+
// Set the new quantity in state.
34+
setProductCount(newQty);
35+
if (products.length) {
36+
const updatedItems = getUpdatedItems(products, newQty, cartKey);
37+
updateCart({
38+
variables: {
39+
input: {
40+
clientMutationId: v4(),
41+
items: updatedItems,
42+
},
43+
},
44+
});
45+
}
46+
}
47+
};
48+
849
return (
950
<>
10-
<div className="inline-block pt-2">
11-
<SVGX />
12-
</div>
13-
14-
<div className="inline-block pt-2">{item.name}</div>
15-
16-
<div className="inline-block pt-2">
51+
<tr className="flex flex-col mb-2 flex-no wrap sm:table-row sm:mb-0">
52+
<td className="h-12 p-3 border-2 border-gray-400 ">
53+
<SVGX
54+
cartKey={item.cartKey}
55+
handleRemoveProductClick={handleRemoveProductClick}
56+
products={products}
57+
/>
58+
</td>
59+
<td className="h-12 p-3 border-l-2 border-r-2 border-gray-400 ">
60+
{item.name}
61+
</td>
62+
<td className="h-12 p-3 border-2 border-gray-400 ">
1763
{'string' !== typeof item.price ? item.price.toFixed(2) : item.price}
18-
</div>
19-
20-
<div className="inline-block pt-2">
64+
</td>
65+
<td className="h-12 p-3 border-l-2 border-r-2 border-gray-400 ">
2166
<input
2267
className="w-12"
2368
type="number"
2469
min="1"
2570
defaultValue={productCount}
26-
onChange={() => {
27-
console.log('Changed quantity ...');
28-
}}
71+
onChange={(event) => handleQuantityChange(event, item.cartKey)}
2972
/>
30-
</div>
31-
32-
<div className="inline-block pt-2">
73+
</td>
74+
<td className="h-12 p-3 border-2 border-gray-400 ">
3375
{'string' !== typeof item.totalPrice
3476
? item.totalPrice.toFixed(2)
3577
: item.totalPrice}
36-
</div>
78+
</td>
79+
</tr>
3780
</>
3881
);
3982
};

0 commit comments

Comments
 (0)