-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
232 lines (156 loc) · 5.53 KB
/
main.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import menuArray from "./data.js";
const menu = document.getElementById("main-container");
const checkout = document.getElementById("checkout");
const cambio = document.getElementById("cambio");
const payTotal = document.getElementById("payTotal");
const screen = document.getElementById('screen');
const idForm = document.getElementById('id-form');
const name = document.getElementById('name');
const screenPay = document.getElementById('screen-pay');
const screenPayMain = document.getElementById('screen-pay-main');
const payPay = document.getElementById('pay-pay');
console.log(name.value);
let tipp = 0;
let itemArray = [];
let idBtn = 100;
idForm.addEventListener('submit', function (event) {
event.preventDefault();
screen.style.display = 'none';
screenPay.style.display = 'inline';
renderPay();
})
document.querySelector('#exit').addEventListener('click', function () {
location.reload();
});
function renderMenu() {
menuArray.forEach(item => {
const menuContainer = document.createElement("div");
menuContainer.classList.add("menu-container");
const menuItem = document.createElement("div");
const menuItem1 = document.createElement("div");
const menuItem3 = document.createElement("div");
menuItem.classList.add("menu-item");
menuItem1.classList.add("menu-item1");
menuItem3.classList.add("menu-item3");
let p = document.createElement("p");
let p1 = document.createElement("p");
let p2 = document.createElement("p");
let p3 = document.createElement("p");
const button = document.createElement("button");
p.classList.add("p-emoji");
p1.classList.add("p-name", "p-name-title");
p2.classList.add("p-name", "p-name-ingredients");
p3.classList.add("p-name", "p-name-price");
p.textContent = `${item.emoji}`;
p1.textContent = `${item.name}`;
p2.textContent = `${item.ingredients}`;
p3.textContent = `$ ${item.price}`;
button.textContent = `Add to cart`;
button.classList.add("btn");
button.setAttribute("data-id", `${item.id}`);
menuItem.appendChild(p);
menuItem1.appendChild(p1);
menuItem1.appendChild(p2);
menuItem1.appendChild(p3);
menuItem3.appendChild(button);
menuContainer.appendChild(menuItem); // Agrega los elementos al contenedor
menuContainer.appendChild(menuItem1);
menuContainer.appendChild(menuItem3);
menu.appendChild(menuContainer);
});
}
renderMenu();
document.addEventListener("click", function (e) {
let actElem = 0;
idBtn += 1;
cambio.classList.remove("inactive");
cambio.classList.add("active");
const id = e.target.dataset.id;
const num = e.target.dataset.num;
const tip = e.target.dataset.tip;
const x = e.target.dataset.click;
if (id == 0) {
const arraySecond = [`${menuArray[id].name}`, `${menuArray[id].price}`, `${idBtn}`];
itemArray.push(arraySecond);
} else if (id == 1) {
const arraySecond = [`${menuArray[id].name}`, `${menuArray[id].price}`, `${idBtn}`];
itemArray.push(arraySecond);
} else if (id == 2) {
const arraySecond = [`${menuArray[id].name}`, `${menuArray[id].price}`, `${idBtn}`];
itemArray.push(arraySecond);
} else if (num) {
targetItem(num);
} else if (e.target.dataset.btn == 9999) {
screen.style.display = 'inline';
} else if (tip) {
if (actElem == 0) {
tipp = parseFloat(tip);
actElem = 1;
}
} else if (x) {
screen.style.display = 'none';
console.log(x)
}
renderFeed();
})
function targetItem(numId) {
const index = itemArray.findIndex(item => item[2] == numId);
itemArray.splice(index, 1);
tipp = 0;
renderFeed();
}
function getFeed() {
let sum = 0;
let feedhtml = ``;
itemArray.forEach(item => {
sum += parseInt(item[1]);
feedhtml += `
<div class = "feed-item">
<div class="p-one">
<p>${item[0]}</p>
</div>
<div class="p-two">
<button class = "btnRemove " data-num ="${item[2]}">Remove</button>
</div>
<div class="p-three">
<p>$ ${item[1]} </p>
</div>
</div>
`
})
feedhtml += `
<div class="tip-add">
<button class ="btn-tip" data-tip= " ${(sum * .10).toFixed(2)}">Tip 5%: <br>$ ${(sum * .10).toFixed(2)}</button>
<button class ="btn-tip" data-tip= " ${(sum * .12).toFixed(2)}">Tip 12%: <br>$ ${(sum * .12).toFixed(2)}</button>
<button class ="btn-tip" data-tip= "${(sum * .18).toFixed(2)}">Tip 18%: <br>$ ${(sum * .18).toFixed(2)}</button>
</div>
`
let sumTip = sum + tipp;
feedhtml += `
<div class="total">
<p>Total: $ ${sumTip}</p>
</div>
<div class="btnCheckout">
<button class="btn-check" data-btn = "${9999}">Checkout</button>
</div>
`
renderTotal(sumTip);
return feedhtml
}
function renderFeed() {
checkout.innerHTML = getFeed();
}
function renderTotal(sum) {
payTotal.innerHTML = '';
let pTotal = document.createElement('p');
pTotal.textContent = `The Total is: $ ${sum} `;
payTotal.appendChild(pTotal);
}
function renderPay() {
payPay.textContent = `Thanks ${name.value}!! your order is on its way!!`;
console.log(payPay);
return payPay;
}
function renderPayF() {
screenPayMain.innerHTML = renderPay();
}