-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddToCart.js
90 lines (81 loc) · 2.02 KB
/
AddToCart.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
import { useState } from "react";
import styled from "styled-components";
import CartAmountToggle from "./CartAmountToggle";
import { NavLink } from "react-router-dom";
import { Button } from "../styles/Button";
import { useCartContext } from "../context/cart_context";
const AddToCart = ({ product }) => {
const { addToCart } = useCartContext();
const { id, colors, stock } = product;
const [color, setColor] = useState(colors[0]);
const [amount, setAmount] = useState(1);
const setDecrease = () => {
amount > 1 ? setAmount(amount - 1) : setAmount(1);
};
const setIncrease = () => {
amount < stock ? setAmount(amount + 1) : setAmount(stock);
};
return (
<Wrapper>
<div>
<h3>No. of Items:</h3>
</div>
{/* add to cart */}
<CartAmountToggle
amount={amount}
setDecrease={setDecrease}
setIncrease={setIncrease}
/>
<NavLink to="/cart" onClick={() => addToCart(id, color, amount, product)}>
<Button className="btn">Add To Cart</Button>
</NavLink>
</Wrapper>
);
};
const Wrapper = styled.section`
.colors p {
display: flex;
justify-content: flex-start;
align-items: center;
}
.btnStyle {
width: 2rem;
height: 2rem;
background-color: #000;
border-radius: 50%;
margin-left: 1rem;
border: none;
outline: none;
opacity: 0.5;
cursor: pointer;
&:hover {
opacity: 1;
}
}
.active {
opacity: 1;
}
.checkStyle {
font-size: 1rem;
color: #fff;
}
/* we can use it as a global one too */
.amount-toggle {
margin-top: 3rem;
margin-bottom: 1rem;
display: flex;
justify-content: space-around;
align-items: center;
font-size: 1.4rem;
button {
border: none;
background-color: #fff;
cursor: pointer;
}
.amount-style {
font-size: 2.4rem;
color: ${({ theme }) => theme.colors.btn};
}
}
`;
export default AddToCart;