1
1
import React , { useState } from 'react' ;
2
2
import { isEmpty } from "lodash" ;
3
3
import Image from '../image' ;
4
- import { updateCart } from '../../utils/cart' ;
4
+ import { deleteCartItem , updateCart } from '../../utils/cart' ;
5
5
6
6
const CartItem = ( {
7
7
item,
8
8
products,
9
- updateCartProcessing,
10
- handleRemoveProductClick,
11
9
setCart
12
10
} ) => {
13
11
14
12
const [ productCount , setProductCount ] = useState ( item . quantity ) ;
15
- const [ loading , setLoading ] = useState ( false ) ;
16
- const productImg = item ?. data ?. images ?. [ 0 ] ;
13
+ const [ updatingProduct , setUpdatingProduct ] = useState ( false ) ;
14
+ const [ removingProduct , setRemovingProduct ] = useState ( false ) ;
15
+ const productImg = item ?. data ?. images ?. [ 0 ] ?? '' ;
16
+
17
+ /*
18
+ * Handle remove product click.
19
+ *
20
+ * @param {Object } event event
21
+ * @param {Integer } Product Id.
22
+ *
23
+ * @return {void }
24
+ */
25
+ const handleRemoveProductClick = ( event , cartKey ) => {
26
+ event . stopPropagation ( ) ;
27
+
28
+ if ( ! updatingProduct ) {
29
+ deleteCartItem ( cartKey , setCart , setRemovingProduct ) ;
30
+ }
31
+ } ;
17
32
18
33
/*
19
34
* When user changes the qty from product input update the cart in localStorage
@@ -30,8 +45,8 @@ const CartItem = ( {
30
45
event . stopPropagation ( ) ;
31
46
let newQty ;
32
47
33
- // If the previous update cart mutation request is still processing , then return.
34
- if ( updateCartProcessing || ( 'decrement' === type && 1 === productCount ) ) {
48
+ // If the previous cart request is still updatingProduct , then return.
49
+ if ( updatingProduct || ( 'decrement' === type && 1 === productCount ) ) {
35
50
return ;
36
51
}
37
52
@@ -46,18 +61,7 @@ const CartItem = ( {
46
61
setProductCount ( newQty ) ;
47
62
48
63
if ( products . length ) {
49
-
50
- // const updatedItems = getUpdatedItems( products, newQty, cartKey );
51
- //
52
- // updateCart( {
53
- // variables: {
54
- // input: {
55
- // clientMutationId: v4(),
56
- // items: updatedItems
57
- // }
58
- // },
59
- // } );
60
- updateCart ( item ?. key , newQty , setCart , setLoading ) ;
64
+ updateCart ( item ?. key , newQty , setCart , setUpdatingProduct ) ;
61
65
}
62
66
63
67
}
@@ -89,7 +93,7 @@ const CartItem = ( {
89
93
{ /*<span className="cart-product-price">{ ( 'string' !== typeof item?.data?.price ) ? item?.data?.price.toFixed( 2 ) : item?.data?.price }</span>*/ }
90
94
< span className = "cart-total-price" > { item ?. currency } { item ?. line_subtotal } </ span >
91
95
</ div >
92
- { loading ? < span > Updating... </ span > : null }
96
+ { updatingProduct ? < img className = "woo-next-cart-item-spinner" width = "24" src = "/cart-spinner.gif" alt = "spinner" / > : null }
93
97
{ /*Qty*/ }
94
98
< div style = { { display : 'flex' , alignItems : 'center' } } >
95
99
< button className = "decrement-btn text-24px" onClick = { ( event ) => handleQtyChange ( event , item ?. cartKey , 'decrement' ) } > -</ button >
@@ -98,13 +102,11 @@ const CartItem = ( {
98
102
min = "1"
99
103
style = { { textAlign : 'center' , width : '50px' , paddingRight : '0' } }
100
104
data-cart-key = { item ?. data ?. cartKey }
101
- className = { `woo-next-cart-qty-input ml-3 ${ updateCartProcessing ? 'disabled' : '' } ` }
105
+ className = { `woo-next-cart-qty-input ml-3 ${ updatingProduct ? 'disabled' : '' } ` }
102
106
value = { productCount }
103
107
onChange = { ( event ) => handleQtyChange ( event , item ?. cartKey , '' ) }
104
108
/>
105
109
< button className = "increment-btn text-20px" onClick = { ( event ) => handleQtyChange ( event , item ?. cartKey , 'increment' ) } > +</ button >
106
- { updateCartProcessing ?
107
- < img className = "woo-next-cart-item-spinner" src = { cartSpinnerGif } alt = "spinner" /> : '' }
108
110
</ div >
109
111
</ footer >
110
112
</ div >
0 commit comments