-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cart.jsx
109 lines (104 loc) · 3.01 KB
/
Cart.jsx
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import React, { useContext, useState } from "react";
import classes from "./Cart.module.css";
import Modal from "../UI/Modal";
import CartContext from "../../Store/cart-context";
import CartItem from "./CartItem";
import Checkout from "./Checkout";
const Cart = (props) => {
const [isSubmitting, setIsSubmitting] = useState(false);
const [didSubmit, setDidSubmit] = useState(false);
const [showForm, setShowForm] = useState(false);
const cartCtx = useContext(CartContext);
const totalAmount = cartCtx.totalAmount.toFixed(2);
const cartItemRemoveHandler = (id) => {
cartCtx.removeItem(id);
};
const cartItemAddHandler = (item) => {
const curItem = { ...item, amount: 1 };
cartCtx.addItem(curItem);
};
const resetCartHandler = () => {
cartCtx.resetCart();
setShowForm(false);
};
const placeOrderHandler = async (userData) => {
setIsSubmitting(true);
const serverUrl = process.env.REACT_APP_ORDER_MEALS;
const response = await fetch(serverUrl, {
method: "POST",
body: JSON.stringify({
user: userData,
orderedItems: cartCtx.items,
}),
});
setIsSubmitting(false);
if (response.ok) {
setDidSubmit(true);
cartCtx.resetCart();
}
};
const cartItems = (
<ul className={classes["cart-items"]}>
{cartCtx.items.map((meal) => (
<CartItem
key={meal.id}
name={meal.name}
price={meal.price}
amount={meal.amount}
onRemove={cartItemRemoveHandler.bind(null, meal.id)}
onAdd={cartItemAddHandler.bind(null, meal)}
/>
))}
</ul>
);
const orderHandler = () => {
setShowForm(true);
};
const modalActions = (
<div className={classes.actions}>
<button onClick={resetCartHandler}>Reset</button>
<button className={classes["button--alt"]} onClick={props.onClose}>
Close
</button>
{cartCtx.items.length > 0 && (
<button className={classes.button} onClick={orderHandler}>
Order
</button>
)}
</div>
);
const orderModalContent = (
<React.Fragment>
{cartItems}
<div className={classes.total}>
<span>Total Amount</span>
<span>${totalAmount}</span>
</div>
{showForm && (
<Checkout onConfirm={placeOrderHandler} onCancel={props.onClose} />
)}
{!showForm && modalActions}
</React.Fragment>
);
return (
<Modal onClose={props.onClose}>
{!isSubmitting && !didSubmit && orderModalContent}
{isSubmitting && !didSubmit && (
<p className={classes.processing}>
Please wait...while we process your order...!
</p>
)}
{didSubmit && (
<React.Fragment>
<p className={classes.success}>Yayy, your food is on your way....!</p>
<div className={classes.actions}>
<button className={classes.button} onClick={props.onClose}>
Close
</button>
</div>
</React.Fragment>
)}
</Modal>
);
};
export default Cart;