-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathscript.js
146 lines (128 loc) · 4.39 KB
/
script.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
class CartItem{
constructor(name, desc, img, price){
this.name = name
this.desc = desc
this.img=img
this.price = price
this.quantity = 1
}
}
class LocalCart{
static key = "cartItems"
static getLocalCartItems(){
let cartMap = new Map()
const cart = localStorage.getItem(LocalCart.key)
if(cart===null || cart.length===0) return cartMap
return new Map(Object.entries(JSON.parse(cart)))
}
static addItemToLocalCart(id, item){
let cart = LocalCart.getLocalCartItems()
if(cart.has(id)){
let mapItem = cart.get(id)
mapItem.quantity +=1
cart.set(id, mapItem)
}
else
cart.set(id, item)
localStorage.setItem(LocalCart.key, JSON.stringify(Object.fromEntries(cart)))
updateCartUI()
}
static removeItemFromCart(id){
let cart = LocalCart.getLocalCartItems()
if(cart.has(id)){
let mapItem = cart.get(id)
if(mapItem.quantity>1)
{
mapItem.quantity -=1
cart.set(id, mapItem)
}
else
cart.delete(id)
}
if (cart.length===0)
localStorage.clear()
else
localStorage.setItem(LocalCart.key, JSON.stringify(Object.fromEntries(cart)))
updateCartUI()
}
}
const cartIcon = document.querySelector('.fa-cart-arrow-down')
const wholeCartWindow = document.querySelector('.whole-cart-window')
wholeCartWindow.inWindow = 0
const addToCartBtns = document.querySelectorAll('.add-to-cart-btn')
addToCartBtns.forEach( (btn)=>{
btn.addEventListener('click', addItemFunction)
} )
function addItemFunction(e){
const id = e.target.parentElement.parentElement.parentElement.getAttribute("data-id")
const img = e.target.parentElement.parentElement.previousElementSibling.src
const name = e.target.parentElement.previousElementSibling.textContent
const desc = e.target.parentElement.children[0].textContent
let price = e.target.parentElement.children[1].textContent
price = price.replace("Price: $", '')
const item = new CartItem(name, desc, img, price)
LocalCart.addItemToLocalCart(id, item)
console.log(price)
}
cartIcon.addEventListener('mouseover', ()=>{
if(wholeCartWindow.classList.contains('hide'))
wholeCartWindow.classList.remove('hide')
})
cartIcon.addEventListener('mouseleave', ()=>{
// if(wholeCartWindow.classList.contains('hide'))
setTimeout( () =>{
if(wholeCartWindow.inWindow===0){
wholeCartWindow.classList.add('hide')
}
} ,500 )
})
wholeCartWindow.addEventListener('mouseover', ()=>{
wholeCartWindow.inWindow=1
})
wholeCartWindow.addEventListener('mouseleave', ()=>{
wholeCartWindow.inWindow=0
wholeCartWindow.classList.add('hide')
})
function updateCartUI(){
const cartWrapper = document.querySelector('.cart-wrapper')
cartWrapper.innerHTML=""
const items = LocalCart.getLocalCartItems()
if(items === null) return
let count = 0
let total = 0
for(const [key, value] of items.entries()){
const cartItem = document.createElement('div')
cartItem.classList.add('cart-item')
let price = value.price*value.quantity
price = Math.round(price*100)/100
count+=1
total += price
total = Math.round(total*100)/100
cartItem.innerHTML =
`
<img src="${value.img}">
<div class="details">
<h3>${value.name}</h3>
<p>${value.desc}
<span class="quantity">Quantity: ${value.quantity}</span>
<span class="price">Price: $ ${price}</span>
</p>
</div>
<div class="cancel"><i class="fas fa-window-close"></i></div>
`
cartItem.lastElementChild.addEventListener('click', ()=>{
LocalCart.removeItemFromCart(key)
})
cartWrapper.append(cartItem)
}
if(count > 0){
cartIcon.classList.add('non-empty')
let root = document.querySelector(':root')
root.style.setProperty('--after-content', `"${count}"`)
const subtotal = document.querySelector('.subtotal')
subtotal.innerHTML = `SubTotal: $${total}`
}
else
cartIcon.classList.remove('non-empty')
}
document.addEventListener('DOMContentLoaded', ()=>{updateCartUI()})