-
Notifications
You must be signed in to change notification settings - Fork 418
Expand file tree
/
Copy pathmainContext.js
More file actions
111 lines (98 loc) · 3.05 KB
/
mainContext.js
File metadata and controls
111 lines (98 loc) · 3.05 KB
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
import { toast } from 'react-toastify'
import React from 'react'
const STORAGE_KEY = 'NEXT_ECOMMERCE_STARTER_'
const initialState = {
cart: [],
numberOfItemsInCart: 0,
total: 0
}
const SiteContext = React.createContext()
function calculateTotal(cart) {
const total = cart.reduce((acc, next) => {
const quantity = next.quantity
acc = acc + JSON.parse(next.price) * quantity
return acc
}, 0)
return total
}
class ContextProviderComponent extends React.Component {
componentDidMount() {
if (typeof window !== 'undefined') {
const storageState = window.localStorage.getItem(STORAGE_KEY)
if (!storageState) {
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(initialState))
}
}
}
setItemQuantity = (item) => {
const storageState = JSON.parse(window.localStorage.getItem(STORAGE_KEY))
const { cart } = storageState
const index = cart.findIndex(cartItem => cartItem.id === item.id)
cart[index].quantity = item.quantity
window.localStorage.setItem(STORAGE_KEY, JSON.stringify({
cart, numberOfItemsInCart: cart.length, total: calculateTotal(cart)
}))
this.forceUpdate()
}
addToCart = item => {
const storageState = JSON.parse(window.localStorage.getItem(STORAGE_KEY))
const { cart } = storageState
if (cart.length) {
const index = cart.findIndex(cartItem => cartItem.id === item.id)
if (index >= Number(0)) {
/* If this item is already in the cart, update the quantity */
cart[index].quantity = cart[index].quantity + item.quantity
} else {
/* If this item is not yet in the cart, add it */
cart.push(item)
}
} else {
/* If no items in the cart, add the first item. */
cart.push(item)
}
window.localStorage.setItem(STORAGE_KEY, JSON.stringify({
cart, numberOfItemsInCart: cart.length, total: calculateTotal(cart)
}))
toast("Successfully added item to cart!", {
position: 'top-left'
})
this.forceUpdate()
}
removeFromCart = (item) => {
const storageState = JSON.parse(window.localStorage.getItem(STORAGE_KEY))
let { cart } = storageState
cart = cart.filter(c => c.id !== item.id)
window.localStorage.setItem(STORAGE_KEY, JSON.stringify({
cart, numberOfItemsInCart: cart.length, total: calculateTotal(cart)
}))
this.forceUpdate()
}
clearCart = () => {
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(initialState))
this.forceUpdate()
}
render() {
let state = initialState
if (typeof window !== 'undefined') {
const storageState = window.localStorage.getItem(STORAGE_KEY)
if (storageState) {
state = JSON.parse(storageState)
}
}
return (
<SiteContext.Provider value={{
...state,
addToCart: this.addToCart,
clearCart: this.clearCart,
removeFromCart: this.removeFromCart,
setItemQuantity: this.setItemQuantity
}}>
{this.props.children}
</SiteContext.Provider>
)
}
}
export {
SiteContext,
ContextProviderComponent
}