-
Notifications
You must be signed in to change notification settings - Fork 9
/
AddToCart.js
167 lines (146 loc) · 4.8 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { useState, useEffect, useContext } from 'react'
import styled from 'styled-components'
import CartContext from '../../context/cartContext'
import { useRouter } from 'next/router'
export default function AddToCart({ id, available }) {
const router = useRouter()
const { cartBadgeToggle, setCartBadgeToggle } = useContext(CartContext)
const [ selectedAmount, setSelectedAmount ] = useState(1)
const [ isProductInCart, setIsProductInCart ] = useState(false)
const [ amountInCart, setAmountInCart ] = useState(null)
const checkIfProductIsInCart = () => {
if (localStorage.getItem('cartList') === null) {
setIsProductInCart(false)
} else {
const cartList = JSON.parse(localStorage.cartList)
const isProductAlreadyInCart = cartList.find(i => i.id === id)
if (isProductAlreadyInCart === undefined) {
setIsProductInCart(false)
} else {
const amountInCart = isProductAlreadyInCart.selectedAmount
setAmountInCart(amountInCart)
setIsProductInCart(true)
}
}
}
const chooseAmount = () => setSelectedAmount(parseInt(document.getElementById("items").value))
const addToCart = async (id, selectedAmount) => {
const addedProduct = { id, selectedAmount }
let cartList
if (localStorage.getItem('cartList') === null) {
cartList = []
cartList.push(addedProduct)
localStorage.setItem('cartList', JSON.stringify(cartList))
} else {
cartList = JSON.parse(localStorage.cartList)
cartList.push(addedProduct)
localStorage.setItem('cartList', JSON.stringify(cartList))
}
setAmountInCart(selectedAmount)
setIsProductInCart(true)
setCartBadgeToggle(!cartBadgeToggle)
}
const cancelAdding = id => {
const cartList = JSON.parse(localStorage.cartList)
const filteredCartList = cartList.filter(i => i.id !== id)
localStorage.setItem('cartList', JSON.stringify(filteredCartList))
setIsProductInCart(false)
setCartBadgeToggle(!cartBadgeToggle)
}
let options = []
for (let i = 1; i <= available; i++) {
options.push(<option value={i} key={i}>{i}</option>)
}
useEffect(() => {
checkIfProductIsInCart()
}, [router.asPath]) // triggers on route change - we need this to refresh <isProductInCart> state (inside checkIfProductIsInCart() function) to correctly show addToCart button (because this useEffect is not triggering when we transition between /product-page/[id].js routes (throught "You might also like" section))
return (
<AddToCartDiv className="add-to-cart">
<h3 className="button-wrapper-h3">
{
available <= 0
? (
<div className="out-of-stock">
<h3 className="out-of-stock-text text-danger">
OUT OF STOCK!
</h3>
</div>
)
: available > 0 && isProductInCart
? (
<div>
<h6>
<button type="button" className="btn btn-warning" onClick={() => cancelAdding(id)}>
Added (cancel)
</button>
</h6>
</div>
) : (
<div>
<label htmlFor="items">
In stock:
<select id="items" onChange={() => chooseAmount()}>
{options}
</select>
</label>
<h6>
<button
type="button"
className="btn btn-warning"
onClick={() => addToCart(id, selectedAmount)}
>
Add to cart
</button>
</h6>
</div>
)
}
</h3>
{
isProductInCart
? (
<div className="in-cart-amount">
<span>In cart: {amountInCart}</span>
</div>
) : (
<div className="invisible"></div>
)
}
</AddToCartDiv>
)
}
const AddToCartDiv = styled.div`
grid-area: 3 / 2 / 4 / 3;
display: flex;
align-content: flex-start;
align-items: flex-start;
align-self: flex-start;
padding-left: 1rem;
padding-right: 1rem;
margin-top: 1em;
> .button-wrapper-h3 > div {
display: flex;
flex-direction: column;
> h6 > button {
margin-right: 1em;
}
}
> .button-wrapper-h3 > .out-of-stock {
position: relative;
height: 100px;
width: 100px;
> .out-of-stock-text {
position: absolute;
top: 6px;
left: 5px;
transform: rotate(14deg);
}
}
> .in-cart-amount {
background: #dc3545;
color: white;
margin: 0 1.5em 0 1.5em;
padding: .2em .5em .2em .5em;
border-radius: 5px;
}
`