11/*eslint complexity: ["error", 6]*/
22
33import { useState } from 'react' ;
4- import { v4 as uuidv4 } from 'uuid' ;
54
65import SVGX from 'components/SVG/SVGX.component' ;
7- import { getUpdatedItems , paddedPrice } from 'utils/functions/functions' ;
6+ import { paddedPrice , handleQuantityChange } from 'utils/functions/functions' ;
87
98const CartItem = ( {
109 item,
@@ -16,40 +15,6 @@ const CartItem = ({
1615 const [ productCount , setProductCount ] = useState ( item . qty ) ;
1716 const totalPrice = paddedPrice ( item . totalPrice , 'kr' ) ;
1817
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- // Return if the previous update cart mutation request is still processing
31- if ( updateCartProcessing ) {
32- return ;
33- }
34- // 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 )
35- const newQty = event . target . value ? parseInt ( event . target . value , 10 ) : 1 ;
36-
37- // Set the new quantity 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 : uuidv4 ( ) ,
46- items : updatedItems ,
47- } ,
48- } ,
49- } ) ;
50- }
51- }
52- } ;
5318 return (
5419 < tr className = "bg-gray-100" >
5520 < td className = "px-4 py-2 border" >
@@ -77,7 +42,16 @@ const CartItem = ({
7742 type = "number"
7843 min = "1"
7944 defaultValue = { productCount }
80- onChange = { ( event ) => handleQuantityChange ( event , item . cartKey ) }
45+ onChange = { ( event ) =>
46+ handleQuantityChange (
47+ event ,
48+ item . cartKey ,
49+ products ,
50+ updateCart ,
51+ updateCartProcessing ,
52+ setProductCount
53+ )
54+ }
8155 />
8256 </ td >
8357 < td className = "px-4 py-2 border" >
0 commit comments