-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
181 lines (143 loc) · 5.55 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const c = (el)=>document.querySelector(el);
const cs = (el) => document.querySelectorAll(el);
let modalQt = 1;
let modalKey = 0;
let cart = [];
//listagem das pizzas
pizzaJson.map((item, index)=>{
let pizzaItem = c('.models .pizza-item').cloneNode(true);
// Preencher as informações em pizza-item
pizzaItem.setAttribute('data-key', index);
pizzaItem.querySelector('.pizza-item--img img').src = item.img;
pizzaItem.querySelector('.pizza-item--name').innerHTML = item.name;
pizzaItem.querySelector('.pizza-item--desc').innerHTML = item.description;
pizzaItem.querySelector('.pizza-item--price').innerHTML = `R$ ${item.price.toFixed(2)}`;
pizzaItem.querySelector('.pizza-item--name').innerHTML = item.name;
pizzaItem.querySelector('a').addEventListener('click', (e)=>{
e.preventDefault();
let key = e.target.closest('.pizza-item').getAttribute('data-key');
modalQt = 1;
modalKey = key;
c('.pizzaBig img').src = pizzaJson[key].img;
c('.pizzaInfo h1').innerHTML = pizzaJson[key].name;
c('.pizzaInfo--desc').innerHTML = pizzaJson[key].description;
c('.pizzaInfo--actualPrice').innerHTML = `R$ ${pizzaJson[key].price.toFixed(2)}`;
c('.pizzaInfo--size.selected').classList.remove('selected');
cs('.pizzaInfo--size').forEach((size, sizeIndex)=>{
if(sizeIndex == 2) {
size.classList.add('selected');
}
size.querySelector('span').innerHTML = pizzaJson[key].sizes[sizeIndex];
});
c('.pizzaInfo--qt').innerHTML = modalQt;
c('.pizzaWindowArea').style.opacity = 0;
c('.pizzaWindowArea').style.display = 'flex';
setTimeout(()=>{
c('.pizzaWindowArea').style.opacity = 1;
},200);
})
c('.pizza-area').append( pizzaItem );
});
// eventos do Modal
function closeModal() {
c('.pizzaWindowArea').style.opacity = 0;
setTimeout(()=>{
c('.pizzaWindowArea').style.display = 'none';
}, 500)
}
cs ('.pizzaInfo--cancelButton, .pizzaInfo--cancelMobileButton').forEach((item)=>{
item.addEventListener('click', closeModal);
});
c('.pizzaInfo--qtmenos').addEventListener('click', ()=>{
if(modalQt > 1){
modalQt--;
c('.pizzaInfo--qt').innerHTML = modalQt;
}
});
c('.pizzaInfo--qtmais').addEventListener('click', ()=>{
modalQt++;
c('.pizzaInfo--qt').innerHTML = modalQt;
});
cs('.pizzaInfo--size').forEach((size, sizeIndex)=>{
size.addEventListener('click', (e)=>{
c('.pizzaInfo--size.selected').classList.remove('selected');
size.classList.add('selected');
});
});
c('.pizzaInfo--addButton').addEventListener('click',()=>{
let size = parseInt(c('.pizzaInfo--size.selected').getAttribute('data-key'));
let identifier = pizzaJson[modalKey].id+'@'+size;
let key = cart.findIndex((item)=>item.identifier == identifier);
if(key > -1) {
cart[key].qt += modalQt;
} else {
cart.push({
identifier,
id:pizzaJson[modalKey].id,
size:size,
qt:modalQt
});
}
updateCart();
closeModal();
});
c('.menu-openner').addEventListener('click', ()=>{
if(cart.length > 0) {
c('aside').style.left = '0';
}
});
c('.menu-closer').addEventListener('click', ()=>{
c('aside').style.left = '100vw';
})
function updateCart() {
c('.menu-openner span').innerHTML = cart.length;
if(cart.length > 0) {
c('aside').classList.add('show');
c('.cart').innerHTML = '';
let subtotal = 0;
let desconto = 0;
let total = 0;
for(let i in cart) {
let pizzaItem = pizzaJson.find((item)=>item.id == cart[i].id);
subtotal += pizzaItem.price * cart[i].qt;
let cartItem = c('.models .cart--item').cloneNode(true);
let pizzaSizeName;
switch(cart[i].size) {
case 0:
pizzaSizeName = 'P';
break;
case 1:
pizzaSizeName = 'M';
break;
case 2:
pizzaSizeName = 'G';
break;
}
let pizzaName = `${pizzaItem.name} (${pizzaSizeName})`;
cartItem.querySelector('img').src = pizzaItem.img;
cartItem.querySelector('.cart--item-nome').innerHTML = pizzaName;
cartItem.querySelector('.cart--item--qt').innerHTML = cart[i].qt;
cartItem.querySelector('.cart--item-qtmenos').addEventListener('click', ()=>{
if(cart[i].qt > 1) {
cart[i].qt--;
}else {
cart.splice(i, 1);
}
updateCart();
});
cartItem.querySelector('.cart--item-qtmais').addEventListener('click', ()=>{
cart[i].qt++;
updateCart();
});
c('.cart').append(cartItem);
}
desconto = subtotal * 0.1;
total = subtotal - desconto;
c('.subtotal span:last-child').innerHTML =`R$ ${subtotal.toFixed(2)}`;
c('.desconto span:last-child').innerHTML =`R$ ${desconto.toFixed(2)}`;
c('.total span:last-child').innerHTML =`R$ ${total.toFixed(2)}`;
}else {
c('aside').classList.remove('show');
c('aside').style.left = '100vw';
}
}