-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCart.js
39 lines (35 loc) · 983 Bytes
/
Cart.js
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
import React from "react";
import { ListGroup, ListGroupItem, Alert } from "reactstrap";
import DeleteCartItem from "./DeleteCartItem";
import { connect } from "react-redux";
function CartItem({ cartItem }) {
return (
<ListGroupItem>
<div className="row">
<div className="col-auto mr-auto">{cartItem.product.name}</div>
<div className="col-auto">
<DeleteCartItem id={cartItem.id} />
</div>
</div>
</ListGroupItem>
);
}
export class Cart extends React.Component {
render() {
const { cartItems } = this.props;
if (!cartItems.ids.length) {
return <Alert color="primary">Cart is empty</Alert>;
}
return (
<ListGroup>
{Object.values(cartItems.byId).map(cartItem => (
<CartItem key={cartItem.id} cartItem={cartItem} />
))}
</ListGroup>
);
}
}
const mapStateToProps = state => ({
cartItems: state.cartItems
});
export default connect(mapStateToProps)(Cart);