-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathItem.jsx
93 lines (88 loc) · 2.65 KB
/
Item.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
import { useState } from "react";
import { useDispatch } from "react-redux";
import { IconButton, Box, Typography, useTheme, Button } from "@mui/material";
import AddIcon from "@mui/icons-material/Add";
import RemoveIcon from "@mui/icons-material/Remove";
import { shades } from "../theme";
import { addToCart } from "../state";
import { useNavigate } from "react-router-dom";
const Item = ({ item, width }) => {
const navigate = useNavigate();
const dispatch = useDispatch();
const [count, setCount] = useState(1);
const [isHovered, setIsHovered] = useState(false);
const {
palette: { neutral },
} = useTheme();
const { category, price, name, image } = item.attributes;
const {
data: {
attributes: {
formats: {
medium: { url },
},
},
},
} = image;
return (
<Box width={width}>
<Box
position="relative"
onMouseOver={() => setIsHovered(true)}
onMouseOut={() => setIsHovered(false)}
>
<img
alt={item.name}
width="300px"
height="400px"
src={`http://localhost:2000${url}`}
onClick={() => navigate(`/item/${item.id}`)}
style={{ cursor: "pointer" }}
/>
<Box
display={isHovered ? "block" : "none"}
position="absolute"
bottom="10%"
left="0"
width="100%"
padding="0 5%"
>
<Box display="flex" justifyContent="space-between">
<Box
display="flex"
alignItems="center"
backgroundColor={shades.neutral[100]}
borderRadius="3px"
>
<IconButton onClick={() => setCount(Math.max(count - 1, 1))}>
<RemoveIcon />
</IconButton>
<Typography color={shades.primary[300]}>{count}</Typography>
<IconButton onClick={() => setCount(count + 1)}>
<AddIcon />
</IconButton>
</Box>
<Button
onClick={() => {
dispatch(addToCart({ item: { ...item, count } }));
}}
sx={{ backgroundColor: shades.primary[300], color: "white" }}
>
Add to Cart
</Button>
</Box>
</Box>
</Box>
<Box mt="3px">
<Typography variant="subtitle2" color={neutral.dark}>
{category
.replace(/([A-Z])/g, " $1")
.replace(/^./, (str) => str.toUpperCase())}
</Typography>
<Typography>{name}</Typography>
<Typography fontWeight="bold">${price}</Typography>
</Box>
</Box>
);
};
export default Item;