-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy pathCartItem.js
More file actions
84 lines (79 loc) · 3.04 KB
/
CartItem.js
File metadata and controls
84 lines (79 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import React, { useEffect } from 'react';
import { BACKEND_API_GATEWAY_URL } from '../constants/appConstants';
import { useDispatch } from 'react-redux';
import { Row, Col, ListGroup, Image, Form, Button, Card } from 'react-bootstrap';
import { Link } from 'react-router-dom';
import { removeFromCartAction } from '../actions/cartActions.js';
import { getProductDetailApi } from '../service/RestApiCalls.js';
import FullPageLoader from './FullPageLoader.js';
import Message from '../components/Message';
import { useState } from 'react';
import { getErrorMessage } from '../service/CommonUtils';
const CartItem = ({ item, addToCart }) => {
const [product, setProduct] = useState('');
const [error, setError] = useState('');
const [loading, setLoading] = useState(false);
const dispatch = useDispatch();
useEffect(async () => {
try {
const productDetail = await getProductDetailApi(item.productId);
setProduct(productDetail);
setLoading(false);
} catch (err) {
setError(getErrorMessage(err));
}
}, [item]);
const removeFromCartHandler = async (cartItemId) => {
dispatch(removeFromCartAction(cartItemId));
};
return (
<>
{error ? (
<Message variant='danger'> {JSON.stringify(error.message)}</Message>
) : (
<ListGroup.Item key={item.productId}>
<Row>
<Col md={2}>
<Image src={`${BACKEND_API_GATEWAY_URL}/api/catalog/image/${product?.imageId}`} alt={item.productName} fluid rounded></Image>
</Col>
<Col md={3} className='pt-4 link-container'>
<Link to={`/product/${item.productId}`}>{item.productName}</Link>
</Col>
<Col md={2} className='pt-4'>
${item.itemPrice}
</Col>
<Col md={2} className='pt-3'>
{product && (
<>
<Form.Control as='select' value={item.quantity} onChange={(e) => addToCart(item.productId, e.target.value)}>
{product.availableItemCount > 11
? [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].map((x) => (
<option key={x + 1} value={x + 1}>
{x + 1}
</option>
))
: [...Array(product.availableItemCount).keys()].map((x) => (
<option key={x + 1} value={x + 1}>
{x + 1}
</option>
))}
</Form.Control>
</>
)}
</Col>
<Col md={1} className='pt-4'>
${item.extendedPrice}
</Col>
<Col md={2} className='pt-3 pl-5'>
<Button type='button' variant='light' onClick={() => removeFromCartHandler(item.cartItemId)}>
<i className='fas fa-trash'></i>
</Button>
</Col>
</Row>
</ListGroup.Item>
)}
{/* {loading && <FullPageLoader></FullPageLoader>} */}
</>
);
};
export default CartItem;