Skip to content

Instantly share code, notes, and snippets.

@deepu105
Created June 24, 2020 14:15
Show Gist options
  • Save deepu105/5853c6ebe43507f1aaf34d1494055259 to your computer and use it in GitHub Desktop.
Save deepu105/5853c6ebe43507f1aaf34d1494055259 to your computer and use it in GitHub Desktop.
//import ...;
export interface ICartProp extends StateProps, DispatchProps {}
export const Cart = (props: ICartProp) => {
useEffect(() => {
props.getEntityForCurrentUser();
}, []);
const remove = id => () => {
props.removeOrder(id);
};
const { isAuthenticated, cart, loading } = props;
return (
<Row className="d-flex justify-content-center">
<Col lg="9" md="12">
{isAuthenticated ? (
<>
<h2>Your shopping cart</h2>
<p className="lead">You have {cart?.orders?.length} items in your cart</p>
{cart.orders && cart.orders.length > 0 ? (
<>
<div className="list-group">
{cart.orders.map((order, i) => (
<a className="list-group-item list-group-item-action flex-column align-items-start">
<div className="row">{/*... list content */}</div>
</a>
))}
</div>
<div className="d-flex justify-content-between py-4">
<h3>
Total price: <TextFormat value={cart.totalPrice as any} type="number" format={'$ 0,0.00'} />
</h3>
<Button tag={Link} to={`/checkout`} color="primary" size="lg">
<FontAwesomeIcon icon="cart-arrow-down" /> <span className="d-none d-md-inline">Checkout</span>
</Button>
</div>
</>
) : (
!loading && <div className="alert alert-warning">No items found</div>
)}
</>
) : (
<div>
<Alert color="warning">Not authorized. Please log in first</Alert>
</div>
)}
</Col>
</Row>
);
};
const mapStateToProps = ({ authentication, shoppingCart }: IRootState) => ({
isAuthenticated: authentication.isAuthenticated,
cart: shoppingCart.entity,
loading: shoppingCart.loading
});
const mapDispatchToProps = {
getEntityForCurrentUser,
removeOrder
};
type StateProps = ReturnType<typeof mapStateToProps>;
type DispatchProps = typeof mapDispatchToProps;
export default connect(mapStateToProps, mapDispatchToProps)(Cart);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment