-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHome.js
144 lines (136 loc) · 4.49 KB
/
Home.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { useState, useEffect } from "react";
import {
Box,
Flex,
Heading,
Text,
Stack,
Container,
Button,
Spacer,
Icon,
SimpleGrid,
Select,
} from "@chakra-ui/react";
import { FaRupeeSign } from "react-icons/fa";
import { MdDelete } from "react-icons/md";
import { getAllItem } from "../../api/menuListApi";
import Carousel from "./Carousel";
const ItemContent = ({ children }) => {
return (
<Stack
bg="white"
boxShadow={"lg"}
p={6}
rounded={"xl"}
align={"center"}
pos={"relative"}
>
{children}
</Stack>
);
};
export default function Home({ addToCart = () => {}, cartItems = [] }) {
const [menuList, setMenuList] = useState(null);
// API call
const getMenuList = async () => {
setMenuList(await getAllItem());
};
useEffect(() => {
getMenuList();
}, []);
return menuList ? (
<>
<Carousel />
{menuList.map((category) => {
return (
<Box bg="gray.50" spacing="4" key={category.id}>
<Container maxW={"7xl"} py={5} as={Stack} spacing={6}>
<Stack spacing={0} align={"center"}>
<Heading>{category.categoryname}</Heading>
<Text>{category.description}</Text>
</Stack>
<SimpleGrid
columns={[1, null, 4]}
spacing={{ base: 10, md: 4, lg: 10 }}
>
{category.items.map((item) => {
const itemInCart = cartItems.find(
(cartItem) => cartItem.id === item.id
);
return (
<Box key={item.id}>
<ItemContent>
<Heading size="md">{item.itemName}</Heading>
<Flex
minWidth="max-content"
alignItems="center"
gap="2"
>
<Flex p="1">
<Icon as={FaRupeeSign} />
<Text size="md">
<Text as="span"></Text>
{item.price}
</Text>
</Flex>
<Spacer />
{itemInCart ? (
<>
<Select
value={`${itemInCart.quantity}`}
maxW="64px"
aria-label="Select quantity"
onChange={(ev) => {
addToCart({
...item,
quantity: ev.target.value,
});
}}
>
{new Array(10).fill(0).map((_, index) => (
<option key={index} value={index}>
{index}
</option>
))}
</Select>
<Button
colorScheme="red"
variant="ghost"
_hover={{ bg: "red.500", color: " white" }}
onClick={() => {
addToCart({ ...item, quantity: 0 });
}}
>
<Text fontSize="xl">
<MdDelete />
</Text>
</Button>
</>
) : (
<Button
colorScheme="red"
variant="outline"
_hover={{ bg: "red.500", color: " white" }}
onClick={() => {
addToCart({ ...item, quantity: 1 });
}}
>
Add To cart
</Button>
)}
</Flex>
</ItemContent>
</Box>
);
})}
</SimpleGrid>
</Container>
</Box>
);
})}
</>
) : (
<h1>Loading....</h1>
);
}