|
1 | 1 | import { useState } from 'react';
|
| 2 | +import { v4 } from 'uuid'; |
2 | 3 |
|
3 | 4 | import SVGX from 'components/SVG/SVGX.component';
|
| 5 | +import { getUpdatedItems } from 'utils/functions/functions'; |
4 | 6 |
|
5 |
| -const MobileCartItem = ({ item }) => { |
| 7 | +const MobileCartItem = ({ |
| 8 | + item, |
| 9 | + products, |
| 10 | + handleRemoveProductClick, |
| 11 | + updateCart, |
| 12 | +}) => { |
6 | 13 | const [productCount, setProductCount] = useState(item.qty);
|
7 | 14 |
|
| 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 | + |
8 | 49 | return (
|
9 | 50 | <>
|
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 "> |
17 | 63 | {'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 "> |
21 | 66 | <input
|
22 | 67 | className="w-12"
|
23 | 68 | type="number"
|
24 | 69 | min="1"
|
25 | 70 | defaultValue={productCount}
|
26 |
| - onChange={() => { |
27 |
| - console.log('Changed quantity ...'); |
28 |
| - }} |
| 71 | + onChange={(event) => handleQuantityChange(event, item.cartKey)} |
29 | 72 | />
|
30 |
| - </div> |
31 |
| - |
32 |
| - <div className="inline-block pt-2"> |
| 73 | + </td> |
| 74 | + <td className="h-12 p-3 border-2 border-gray-400 "> |
33 | 75 | {'string' !== typeof item.totalPrice
|
34 | 76 | ? item.totalPrice.toFixed(2)
|
35 | 77 | : item.totalPrice}
|
36 |
| - </div> |
| 78 | + </td> |
| 79 | + </tr> |
37 | 80 | </>
|
38 | 81 | );
|
39 | 82 | };
|
|
0 commit comments