-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
executable file
·288 lines (262 loc) · 8.25 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
$(document).ready(function () {
const loadReviews = (data) => {
clearReviews();
let reviews;
// For Reviews
if (data.length === 0) {
reviews =
"<p class='alert-message'>Be the first one who let us know how it tastes!</p>";
} else {
reviews = data
.map(
(review) => `<div class="review">
<div class="header">
<h5 class="user-name">${review.firstName}</h5>
<span class="date">${timeAgo(review.date)}</span>
</div>
<div class="comment">
${review.comment}
</div>
<div class="footer">
<span>
${[1, 2, 3, 4, 5]
.map((num) =>
review.rate >= num
? `<ion-icon name="star" class="star"></ion-icon>`
: `<ion-icon name="star-outline" class="star"></ion-icon>`
)
.join("")}
</span>
</div>
</div>`
)
.join("");
}
$("#modal .main .user-reviews").html(reviews);
};
const clearReviews = () => {
$("#modal .main .user-reviews").empty();
$("#modal #review").val("");
$("#modal .main .review-form .star-group > input:checked").prop(
"checked",
false
);
Array.from(
$("#modal .main .review-form .star-group label ion-icon")
).forEach((el) => {
el.setAttribute("name", "star-outline");
});
};
const splitToLi = (instruction) => {
return instruction.split(/\d+\./g).filter((el) => el.length > 0);
};
const timeAgo = (date) => {
const seconds = Math.floor((new Date() - new Date(date)) / 1000);
let interval = Math.floor(seconds / 31536000);
if (interval > 1) {
return interval + " years ago";
}
interval = Math.floor(seconds / 2592000);
if (interval > 1) {
return interval + " months ago";
}
interval = Math.floor(seconds / 86400);
if (interval > 1) {
return interval + " days ago";
}
interval = Math.floor(seconds / 3600);
if (interval > 1) {
return interval + " hours ago";
}
interval = Math.floor(seconds / 60);
if (interval > 1) {
return interval + " minutes ago";
}
if ((interval = 1)) {
return "a minute ago";
}
if (seconds < 10) return "just now";
return Math.floor(seconds) + " seconds ago";
};
function closeModal(e) {
$("#modal").fadeOut(200);
$("#overlay").fadeOut(300);
$("#modal .main .instruction-content").remove();
$("#modal > .header .title").remove();
clearReviews();
}
function openModal(e) {
$("#modal").fadeIn(300);
$("#overlay").fadeIn(200);
}
// MODAL
//close
$("#modal .close-modal").on("click", closeModal);
$("#overlay").on("click", closeModal);
//open
$("#modal .close-modal").on("click", closeModal);
$("#overlay").on("click", closeModal);
//open
$(".recipe-card .action").on("click", function (e) {
openModal();
const id = e.target.dataset.recipeId;
$.ajax({
url: "api/recipe.php?id=" + id,
method: "GET",
headers: {
"Content-Type": "application/json",
},
statusCode: {
200: function (data) {
// for Instruction
const instructions = splitToLi(data.instruction)
.map((instruction) => `<li>${instruction}</li>`)
.join("");
$("#modal > .header").prepend(
`<h3 class="title" data-recipe-id=${id}>${data.title}</h3>`
);
$("#modal .main").prepend(`<div class="instruction-content">
<h4>Cooking steps:</h4>
<ol class="instruction">
${instructions}
</ol>
</div>`);
},
},
});
$.ajax({
url: "api/review.php?id=" + id,
method: "GET",
headers: {
"Content-Type": "application/json",
},
statusCode: {
200: loadReviews,
},
});
});
$("#modal .main .review-form").on("submit", function (e) {
e.preventDefault();
const id = $("#modal > .header .title")[0].dataset.recipeId;
const formData = new FormData(this);
const reqData = Object.fromEntries(formData.entries());
console.log("HERE")
console.log("reqData", reqData)
$.post("api/review.php?id=" + id, reqData).done(function (data, err) {
if(err){console.log(err)}
loadReviews(data);
});
});
$("main aside ul.selectedList").on("click", function (e) {
const dataEl = document.getElementById("ingredients");
dataEl.value = dataEl.value.split(`+${e.target.textContent},`).join("");
$(e.target).remove();
});
$("main aside ul.unselectedList").on("click", function (e) {
const dataEl = document.getElementById("ingredients");
dataEl.value = dataEl.value.split(`-${e.target.textContent},`).join("");
$(e.target).remove();
});
$("#ing-to-have").on("focus", function (e) {
const dataEl = document.getElementById("ingredients");
const ingredientsList = Array.from(
$("main aside form .by-ing datalist")[0].options
).map((opt) => opt.value);
$(this).on("keypress", function (e) {
if (e.key === "Enter") {
if (
ingredientsList.includes(this.value) &&
!dataEl.value.includes(this.value)
) {
dataEl.value += `+${this.value},`;
$("main aside ul.selectedList").append(`<li>${this.value}</li>`);
}
this.value = "";
}
});
});
$("#ing-to-have").on("blur", function (e) {
$(this).off("keypress");
});
$("#ing-not-to-have").on("focus", function (e) {
const ingredientsList = Array.from(
$("main aside form .by-ing datalist")[0].options
).map((opt) => opt.value);
const dataEl = document.getElementById("ingredients");
$(this).on("keypress", function (e) {
if (e.key === "Enter") {
if (
ingredientsList.includes(this.value) &&
!dataEl.value.includes(this.value)
) {
dataEl.value += `-${this.value},`;
$("main aside ul.unselectedList").append(`<li>${this.value}</li>`);
}
this.value = "";
}
});
});
$("#ing-not-to-have").on("blur", function (e) {
$(this).off("keypress");
});
$("main aside form").on("keypress", function (e) {
if (e.key === "Enter") {
e.preventDefault();
return;
}
});
$("main aside form button[type='reset']").on("click", function (e) {
const curUrl = window.location.href;
console.log(curUrl);
const newUrl = curUrl.split("?")[0];
location.assign(newUrl);
});
$("main aside form .by-cal input").on("input", function (e) {
const inputs = this.closest("div").querySelectorAll("input");
let larger, smaller;
if (+inputs[0].value >= +inputs[1].value) {
larger = inputs[0];
smaller = inputs[1];
} else {
larger = inputs[1];
smaller = inputs[0];
}
larger.setAttribute("name", "maxCal");
smaller.setAttribute("name", "minCal");
$("main aside form .by-cal span.to").html(larger.value + " Cal");
$("main aside form .by-cal span.from").html(smaller.value + " Cal");
});
const starGroupChangeHandler = function (e) {
const curRateValue = e.target.value;
Array.from(
this.closest("div").querySelectorAll("label > ion-icon")
).forEach((el) => {
if (curRateValue >= el.dataset.starVal) {
el.setAttribute("name", "star");
} else {
el.setAttribute("name", "star-outline");
}
});
};
$("main > aside form .by-rating input").on("change", starGroupChangeHandler);
if ($("main > aside form .by-rating").data("previousRate")) {
const curRateValue = $("main > aside form .by-rating").data("previousRate");
delete $("main > aside form .by-rating")[0].dataset.previousRate;
Array.from($("main > aside form .by-rating label > ion-icon")).forEach(
(el) => {
if (curRateValue >= el.dataset.starVal) {
el.setAttribute("name", "star");
} else {
el.setAttribute("name", "star-outline");
}
}
);
}
$("#modal > .main form.review-form .star-group input").on(
"change",
starGroupChangeHandler
);
$("main > aside form .by-cook-time input").on("input", function (e) {
$(this).parent().find("label > span").html(`${e.target.value} Minutes`);
});
});