Skip to content

Commit 0aa3782

Browse files
committed
Add Remove cart function
1 parent 8322cb2 commit 0aa3782

File tree

4 files changed

+58
-61
lines changed

4 files changed

+58
-61
lines changed

public/cart-spinner.gif

33.2 KB
Loading

src/components/cart/cart-item.js

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
import React, { useState } from 'react';
22
import {isEmpty} from "lodash";
33
import Image from '../image';
4-
import { updateCart } from '../../utils/cart';
4+
import { deleteCartItem, updateCart } from '../../utils/cart';
55

66
const CartItem = ( {
77
item,
88
products,
9-
updateCartProcessing,
10-
handleRemoveProductClick,
119
setCart
1210
} ) => {
1311

1412
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+
};
1732

1833
/*
1934
* When user changes the qty from product input update the cart in localStorage
@@ -30,8 +45,8 @@ const CartItem = ( {
3045
event.stopPropagation();
3146
let newQty;
3247

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 ) ) {
3550
return;
3651
}
3752

@@ -46,18 +61,7 @@ const CartItem = ( {
4661
setProductCount( newQty );
4762

4863
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);
6165
}
6266

6367
}
@@ -89,7 +93,7 @@ const CartItem = ( {
8993
{/*<span className="cart-product-price">{ ( 'string' !== typeof item?.data?.price ) ? item?.data?.price.toFixed( 2 ) : item?.data?.price }</span>*/}
9094
<span className="cart-total-price">{item?.currency}{item?.line_subtotal}</span>
9195
</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 }
9397
{/*Qty*/}
9498
<div style={{ display: 'flex', alignItems: 'center' }}>
9599
<button className="decrement-btn text-24px" onClick={( event ) => handleQtyChange( event, item?.cartKey, 'decrement' )} >-</button>
@@ -98,13 +102,11 @@ const CartItem = ( {
98102
min="1"
99103
style={{ textAlign: 'center', width: '50px', paddingRight: '0' }}
100104
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' : '' } ` }
102106
value={ productCount }
103107
onChange={ ( event ) => handleQtyChange( event, item?.cartKey, '' ) }
104108
/>
105109
<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"/> : '' }
108110
</div>
109111
</footer>
110112
</div>

src/components/cart/cart-items-container.js

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,7 @@ import { clearCart } from '../../utils/cart';
88
const CartItemsContainer = () => {
99
const [ cart, setCart ] = useContext( AppContext );
1010
const { cartItems, totalPrice, totalQty } = cart || {};
11-
const [ isUpdateCartProcessing, setUpdateCartProcessing ] = useState( false );
1211
const [ isClearCartProcessing, setClearCartProcessing ] = useState( false );
13-
console.log( 'cartItems', cartItems );
14-
/*
15-
* Handle remove product click.
16-
*
17-
* @param {Object} event event
18-
* @param {Integer} Product Id.
19-
*
20-
* @return {void}
21-
*/
22-
const handleRemoveProductClick = ( event, cartKey, products ) => {
23-
event.stopPropagation();
24-
if ( products.length ) {
25-
// By passing the newQty to 0 in updateCart Mutation, it will remove the item.
26-
const newQty = 0;
27-
// const updatedItems = getUpdatedItems(products, newQty, cartKey);
28-
//
29-
// updateCart({
30-
// variables: {
31-
// input: {
32-
// clientMutationId: v4(),
33-
// items: updatedItems,
34-
// },
35-
// },
36-
// });
37-
}
38-
};
3912

4013
// Clear the entire cart.
4114
const handleClearCart = ( event ) => {
@@ -60,10 +33,8 @@ const CartItemsContainer = () => {
6033
<CartItem
6134
key={ item.product_id }
6235
item={ item }
63-
updateCartProcessing={ isUpdateCartProcessing }
6436
products={ cartItems }
6537
setCart={setCart}
66-
handleRemoveProductClick={ handleRemoveProductClick }
6738
/>
6839
) ) }
6940
</div>

src/utils/cart/index.js

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ export const addToCart = ( productId, qty = 1, setCart, setIsAddedToCart, setLoa
4343
* View Cart Request Handler
4444
*
4545
* @param {Function} setCart Set Cart Function.
46-
* @param {Function} setLoading Set Loading Function.
46+
* @param {Function} setProcessing Set Processing Function.
4747
*/
48-
export const viewCart = ( setCart, setLoading = () => {} ) => {
48+
export const viewCart = ( setCart, setProcessing = () => {} ) => {
4949

5050
const addOrViewCartConfig = getApiCartConfig();
5151

5252
axios.get( CART_ENDPOINT, addOrViewCartConfig )
5353
.then( ( res ) => {
5454
const formattedCartData = getFormattedCartData( res?.data ?? [] )
5555
setCart( formattedCartData );
56-
setLoading(false);
56+
setProcessing(false);
5757
} )
5858
.catch( err => {
5959
console.log( 'err', err );
@@ -63,20 +63,44 @@ export const viewCart = ( setCart, setLoading = () => {} ) => {
6363
/**
6464
* Update Cart Request Handler
6565
*/
66-
export const updateCart = ( cartKey, qty = 1, setCart, setLoading ) => {
66+
export const updateCart = ( cartKey, qty = 1, setCart, setUpdatingProduct ) => {
6767

6868
const addOrViewCartConfig = getApiCartConfig();
6969

70-
setLoading(true);
70+
setUpdatingProduct(true);
7171

7272
axios.put( `${CART_ENDPOINT}${cartKey}`, {
73-
// product_id: productId,
7473
quantity: qty,
7574
}, addOrViewCartConfig )
7675
.then( ( res ) => {
77-
// const formattedCartData = getFormattedCartData( res?.data ?? [] );
78-
// setCart( formattedCartData );
79-
viewCart( setCart, setLoading );
76+
viewCart( setCart, setUpdatingProduct );
77+
} )
78+
.catch( err => {
79+
console.log( 'err', err );
80+
} );
81+
};
82+
83+
/**
84+
* Delete a cart item Request handler.
85+
*
86+
* Deletes all products in the cart of a
87+
* specific product id ( by its cart key )
88+
* In a cart session, each product maintains
89+
* its data( qty etc ) with a specific cart key
90+
*
91+
* @param {String} cartKey Cart Key.
92+
* @param {Function} setCart SetCart Function.
93+
* @param {Function} setRemovingProduct Set Removing Product Function.
94+
*/
95+
export const deleteCartItem = ( cartKey, setCart, setRemovingProduct ) => {
96+
97+
const addOrViewCartConfig = getApiCartConfig();
98+
99+
setRemovingProduct(true);
100+
101+
axios.delete( `${CART_ENDPOINT}${cartKey}`, addOrViewCartConfig )
102+
.then( ( res ) => {
103+
viewCart( setCart, setRemovingProduct );
80104
} )
81105
.catch( err => {
82106
console.log( 'err', err );

0 commit comments

Comments
 (0)