Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
168 lines (143 loc) · 5 KB
/
Copy pathindex.js
File metadata and controls
168 lines (143 loc) · 5 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
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
import { menuArray, mealDeal } from "./data.js";
import addBtnUrl from "./images/add-btn.svg";
const completeOrderMessage = document.querySelector(".complete-order__message");
const completeOrderSection = document.querySelector(".complete-order");
const modal = document.querySelector(".modal");
const modalForm = document.querySelector(".modal__form");
const pizzaId = mealDeal.itemIds[0];
const beerId = mealDeal.itemIds[1];
const discountRate = mealDeal.discountPercent / 100;
const ratingFieldset = document.querySelector(".rating__fieldset");
const ratingForm = document.querySelector(".rating");
const ratingMessage = document.querySelector(".complete-order__rating-message");
let orderArray = [];
document.addEventListener("click", function (e) {
const addBtn = e.target.closest("[data-add]");
if (addBtn) {
handleAddClick(addBtn.dataset.add);
}
const removeBtn = e.target.closest("[data-remove]");
if (removeBtn) {
handleRemoveClick(removeBtn.dataset.remove);
}
const completeBtn = e.target.closest("[data-complete]");
if (completeBtn) {
handleCompleteOrder();
}
const themeBtn = e.target.closest("[data-theme-toggle]");
if (themeBtn) {
handleThemeToggle(themeBtn);
}
});
modalForm.addEventListener("submit", function (e) {
e.preventDefault();
const customerName = new FormData(modalForm).get("name");
handlePayment(customerName);
});
ratingFieldset.addEventListener("change", function (e) {
const rating = e.target.value;
handleRating(rating);
});
function handleAddClick(id) {
const menuItemId = Number(id);
const menuItem = menuArray.find((item) => item.id === menuItemId);
orderArray.push(menuItem);
completeOrderSection.classList.add("hidden");
renderOrder();
}
function handleRemoveClick(index) {
const orderItemIndex = Number(index);
orderArray.splice(orderItemIndex, 1);
renderOrder();
}
function handleCompleteOrder() {
modal.classList.remove("hidden");
document.querySelector("#name").focus();
}
function handlePayment(customerName) {
orderArray = [];
modal.classList.add("hidden");
modalForm.reset();
completeOrderSection.classList.remove("hidden");
completeOrderMessage.textContent = `Thanks, ${customerName}! Your order is on its way!`;
ratingMessage.classList.add("hidden");
ratingForm.reset();
renderOrder();
}
function handleThemeToggle(themeBtn) {
const html = document.documentElement;
const isDark = html.getAttribute("data-theme") === "dark";
html.setAttribute("data-theme", isDark ? "light" : "dark");
themeBtn.setAttribute("aria-pressed", String(!isDark));
}
function handleRating(rating) {
ratingMessage.classList.remove("hidden");
ratingMessage.textContent = `Thanks for rating us ${rating}/5!`;
}
function getMenuHtml() {
const menuArrayItems = menuArray.map((item) => {
return `
<article class="menu-item">
<span class="menu-item__emoji" aria-hidden="true">${item.emoji}</span>
<div class="menu-item__info">
<h2 class="menu-item__title">${item.name}</h2>
<p class="menu-item__description">
${item.ingredients.join(", ")}
</p>
<p class="menu-item__price">${formatPrice(item.price)}</p>
</div>
<button class="menu-item__button" aria-label="Add ${item.name} to order" data-add="${item.id}">
<img src="${addBtnUrl}" alt="" width="50" height="50" />
</button>
</article>
`;
});
return menuArrayItems.join("");
}
function getOrderHtml() {
const orderList = orderArray.map((item, index) => {
return `
<li class="order__item">
<p class="order__name">${item.name}</p>
<button class="order__remove" aria-label="Remove ${item.name} from order" data-remove="${index}">remove</button>
<p class="order__price">${formatPrice(item.price)}</p>
</li>
`;
});
return orderList.join("");
}
function getTotalPrice() {
return orderArray.reduce((acc, curr) => acc + curr.price, 0);
}
function getDiscount() {
const hasPizza = orderArray.some((item) => item.id === pizzaId);
const hasBeer = orderArray.some((item) => item.id === beerId);
if (!hasPizza || !hasBeer) {
return 0;
}
const pizzaPrice = menuArray.find((item) => item.id === pizzaId).price;
const beerPrice = menuArray.find((item) => item.id === beerId).price;
return (pizzaPrice + beerPrice) * discountRate;
}
function formatPrice(amount) {
return `$${amount.toFixed(2)}`;
}
function renderOrder() {
const discount = getDiscount();
document
.querySelector(".order__discount")
.classList.toggle("hidden", discount === 0);
document.querySelector(".order__discount-price").textContent =
`-${formatPrice(discount)}`;
document
.querySelector(".order")
.classList.toggle("hidden", orderArray.length === 0);
document.querySelector(".order__list").innerHTML = getOrderHtml();
document.querySelector(".order__total-price").textContent = formatPrice(
getTotalPrice() - discount,
);
}
function render() {
document.querySelector(".menu").innerHTML = getMenuHtml();
}
render();