-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
63 lines (55 loc) · 1.49 KB
/
App.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { useState, useEffect } from "react";
import "./App.css";
import Card from "./Components/Card/Card";
import Cart from "./Components/Cart/Cart";
const { getData } = require("./db/db");
const foods = getData();
const tele = window.Telegram.WebApp;
function App() {
const [cartItems, setCartItems] = useState([]);
useEffect(() => {
tele.ready();
});
const onAdd = (food) => {
const exist = cartItems.find((x) => x.id === food.id);
if (exist) {
setCartItems(
cartItems.map((x) =>
x.id === food.id ? { ...exist, quantity: exist.quantity + 1 } : x
)
);
} else {
setCartItems([...cartItems, { ...food, quantity: 1 }]);
}
};
const onRemove = (food) => {
const exist = cartItems.find((x) => x.id === food.id);
if (exist.quantity === 1) {
setCartItems(cartItems.filter((x) => x.id !== food.id));
} else {
setCartItems(
cartItems.map((x) =>
x.id === food.id ? { ...exist, quantity: exist.quantity - 1 } : x
)
);
}
};
const onCheckout = () => {
tele.MainButton.text = "Pay :)";
tele.MainButton.show();
};
return (
<>
<h1 className="heading">Order Food</h1>
<Cart cartItems={cartItems} onCheckout={onCheckout}/>
<div className="cards__container">
{foods.map((food) => {
return (
<Card food={food} key={food.id} onAdd={onAdd} onRemove={onRemove} />
);
})}
</div>
</>
);
}
export default App;