-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
527 lines (488 loc) · 15.9 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
let form = document.getElementById("form");
let inputValue = document.getElementById("country-input");
let submitBtn = document.getElementById("submit-search");
let randomBtn = document.getElementById("random-btn");
let resultsList = document.getElementById("results-list");
let output = document.getElementById("output");
let body = document.querySelector("body");
let movieName = document.getElementById("movie-title");
let voteAverage = document.getElementById("vote-average");
let plotSummary = document.getElementById("plot-summary");
let moviePoster = document.getElementById("movie-poster");
const edamamAppID = "605b1768";
const edamamAppKey = "d2159e6469acba495c81cdce12ad0bcd";
const dishIntro = document.getElementById('dish-intro');
const dishImgElem = document.getElementById('dish-img');
const ingredientsElem = document.getElementById('ingredients');
const recipeURLelem = document.getElementById('recipe-url');
const ingredientsList = document.getElementById('ingredients-list');
const recipeSection = document.getElementById('display-dish');
const movieSection = document.getElementById('movie-rec');
// ****************************
// * Display Dish Information *
// ****************************
function displayDish() {
const cuisineType = countryMapping[resultCountry] //access the mapped value of the countrieMapping object
if (cuisineType) {
fetch(`https://api.edamam.com/api/recipes/v2/?type=public&q=&cuisineType=${cuisineType}&app_id=${edamamAppID}&app_key=${edamamAppKey}`)
.then((response) => {
if (!response.ok) {
const error = new Error(response.status);
throw error;
}
else {
return response.json();
}
})
.then((response) => {
const randomRecipe = Math.floor(Math.random() * response.hits.length);
const recipe = response.hits[randomRecipe].recipe;
const mealType = recipe.mealType[0];
dishIntro.innerText = `Not sure what to have for ${mealType}? Here's some inspiration from ${resultCountry}: ${recipe.label}!`
const dishImg = recipe.image;
dishImgElem.src = dishImg;
const ingredients = recipe.ingredientLines;
ingredientsElem.innerText = `Want to have the best ${recipe.label}? Here's what you need:`;
//ingredients appear as a list
for (let item of ingredients) {
// console.log(item);
const li = document.createElement("li");
li.append(item);
ingredientsList.append(li);
}
//create link
const a = document.createElement('a');
a.innerText = "Try out this wonderful recipe! (opens in new tab)";
a.href = recipe.url;
a.target = "_blank"
recipeURLelem.appendChild(a);
})
.catch(() => dishIntro.innerText = "Oops, something went wrong.")
} else { //handle output in case cuisineType is undefined
dishIntro.innerText = `Sorry, couldn't find a recipe from ${resultCountry}. Try another country!`
}
}
//function to hide previous results
function clearResults() {
dishImgElem.src = "";
ingredientsList.innerHTML = "";
recipeURLelem.innerHTML = "";
ingredientsElem.innerHTML = "";
}
//function to generate random film in language of chosen country
function movieRecommender(data, index = 0) {
let langResults = data.flatMap(country => country.languages);
let languageThing = langResults[index];
let language= Object.values(languageThing);
//chooses the first language listed if there are several for a country
if (language.length > 1){
language = language[0];
}
let languageCode;
for (const [key, value] of Object.entries(langObject)) {
if (value.includes(language)){
languageCode = key;}
}
fetch(`https://api.themoviedb.org/3/discover/movie?api_key=11d60f7fcb15ec34d310ee95b2269f47&with_original_language=${languageCode}&include_adult=false`)
.then((response) => {
if(!response.ok) {throw new Error ('problem calling API');}
return response.json();
})
.then((response) => {
if (response.total_pages == 0) {
moviePoster.src="";
movieName.innerText = `No film found for this language`;
voteAverage.innerText = ("");
plotSummary.innerText = "";
throw new Error ('no films found')
};
let filmResults = response.results;
let randomIndex = Math.floor(Math.random() * filmResults.length);
let resultFilm = filmResults[randomIndex];
//display information for user
if (resultFilm.backdrop_path){
moviePoster.src=`https://image.tmdb.org/t/p/w500/${resultFilm.backdrop_path}`;
}
else{
moviePoster.src="";
}
if (resultFilm.original_title !== resultFilm.title){
movieName.innerText=(`${resultFilm.title} (${resultFilm.original_title})`);
}
else{
movieName.innerText = (`${resultFilm.original_title}`);
}
voteAverage.innerText = (`Voter rating: ${resultFilm.vote_average}/10`);
plotSummary.innerText = (`${resultFilm.overview}`);
return resultFilm;
})
.catch((error) => {
console.log(error.message)});
}
// Show search results before submitting
let timeout = 0;
window.addEventListener("load", searchResults);
function searchResults() {
inputValue.onkeyup = () => {
fetch("https://restcountries.com/v3.1/all")
.then((response) => response.json())
.then((data) => {
resultsList.innerHTML = "";
clearTimeout(timeout);
if (inputValue.value.trim().length === 0) {
return;
}
let nameResults = data.flatMap(country => country.name.common);
timeout = setTimeout(() => {
nameResults.forEach(result => {
if (result.toLowerCase().includes(inputValue.value.toLowerCase())) {
resultsList.style.display = "block";
let listItem = document.createElement("li");
listItem.id = "country-list-item";
listItem.innerText = result;
listItem.addEventListener("click", function () {
inputValue.value = result;
resultsList.innerHTML = "";
resultsList.style.background = "";
resultsList.style.display = "none";
})
resultsList.style.background = "var(--background-color)";
resultsList.appendChild(listItem);
}
});
}, 200);
})
.catch(() => {
resultsList.innerHTML = "<li>No result found</li>";
});
}
}
// Submit input and show final result
form.addEventListener("submit", getCountry);
let resultCountry;
function getCountry(event) {
event.preventDefault();
let formData = new FormData(form);
let value = formData.get("country");
fetch(`https://restcountries.com/v3.1/name/${value}`)
.then((response) => {
if (!response.ok) {
const error = new Error(response.status);
throw error;
}
else {
return response.json();
}
})
.then((data) => {
clearResults();
resultCountry = data[0].name.common;
output.style.visibility = "visible";
output.innerHTML = `<p id="main-country">${resultCountry} </p>
<img src=${data[0].flags.png} alt="flag of ${resultCountry}" id="flag">`;
resultsList.innerHTML = "";
recipeSection.classList.remove('hide');
movieSection.classList.remove('hide');
displayDish();
movieRecommender(data);
window.scroll({
top: 750,
behavior: 'smooth'
});
})
.catch((error) => {
resultsList.innerHTML = "<li>No result found</li>";
output.innerText = "";
console.log(error)
});
inputValue.value = "";
}
// Generate a random country
randomBtn.addEventListener("click", randomCountry);
function randomCountry() {
fetch("https://restcountries.com/v3.1/all")
.then((response) => {
if (!response.ok) {
const error = new Error(response.status);
throw error;
}
else {
return response.json();
}
})
.then((data) => {
let nameResults = data.flatMap(country => country.name.common);
let flagResults = data.flatMap(country => country.flags.png);
let randomIndex = Math.floor(Math.random() * nameResults.length);
resultCountry = nameResults[randomIndex];
output.style.visibility = "visible";
output.innerHTML = `<p id="main-country">${resultCountry} </p>
<img src=${flagResults[randomIndex]} alt="flag of ${resultCountry}" id="flag">`;
recipeSection.classList.remove('hide');
movieSection.classList.remove('hide');
clearResults();
displayDish();
movieRecommender(data, randomIndex);
window.scroll({
top: 750,
behavior: 'smooth'
});
return resultCountry;
})
.catch((error) => {
console.log(error)
});
}
//map countries to cuisine types
const countryMapping = {
"Sweden": "Nordic",
"Albania": undefined,
"Vanuatu": undefined,
"Mauritius": undefined,
"United Kingdom": "British",
"Portugal": "Mediterranean",
"Ivory Coast": undefined,
"Georgia": "Eastern Europe",
"Bhutan": undefined,
"Namibia": undefined,
"Netherlands": "Central Europe",
"Uruguay": "South American",
"Belgium": "Central Europe",
"Guernsey": undefined,
"United States Virgin Islands": "Caribbean",
"Uganda": undefined,
"Liechtenstein": "Central Europe",
"Guinea-Bissau": undefined,
"South Korea": "Asian",
"Cuba": "Caribbean",
"Montenegro": undefined,
"Norway": "Nordic",
"Saint Vincent and the Grenadines": "Caribbean",
"Mayotte": undefined,
"Latvia": undefined,
"Paraguay": "South American",
"Jersey": undefined,
"Laos": "South East Asian",
"Qatar": "Middle Eastern",
"Dominican Republic": "Caribbean",
"Bouvet Island": undefined,
"Slovakia": "Eastern Europe",
"Bosnia and Herzegovina": undefined,
"Panama": "Caribbean",
"Myanmar": "South East Asian",
"Barbados": "Caribbean",
"Cambodia": "South East Asian",
"Antigua and Barbuda": "Caribbean",
"Kenya": undefined,
"India": "Indian",
"São Tomé and Príncipe": undefined,
"Faroe Islands": "Nordic",
"Czechia": "Central Europe",
"Montserrat": "Caribbean",
"South Georgia": undefined,
"Ukraine": "Eastern Europe",
"Djibouti": undefined,
"Greenland": "Nordic",
"Tonga": undefined,
"United States": "American",
"Norfolk Island": undefined,
"Republic of the Congo": undefined,
"Lebanon": "Middle Eastern",
"Uzbekistan": undefined,
"Guam": undefined,
"Estonia": undefined,
"Tuvalu": undefined,
"Bermuda": "Caribbean",
"South Sudan": undefined,
"Costa Rica": undefined,
"Finland": "Nordic",
"Gambia": undefined,
"French Polynesia": undefined,
"Belize": "Caribbean",
"Kiribati": undefined,
"French Guiana": "South American",
"Andorra": undefined,
"Kosovo": undefined,
"Mozambique": undefined,
"Puerto Rico": undefined,
"Australia": undefined,
"Azerbaijan": undefined,
"Sudan": undefined,
"Chad": undefined,
"Eritrea": undefined,
"Malaysia": "South East Asian",
"Kuwait": "Middle Eastern",
"Northern Mariana Islands": undefined,
"New Zealand": undefined,
"Morocco": "Mediterranean",
"Somalia": undefined,
"Singapore": undefined,
"Timor-Leste": undefined,
"Greece": "Mediterranean",
"Botswana": undefined,
"Indonesia": "South East Asian",
"North Macedonia": "Eastern Europe",
"Tanzania": undefined,
"Eswatini": undefined,
"Central African Republic": undefined,
"Bolivia": "South American",
"Solomon Islands": undefined,
"Thailand": "South East Asian",
"Réunion": undefined,
"Croatia": "Central Europe",
"Burundi": undefined,
"United Arab Emirates": "Middle Eastern",
"Palau": undefined,
"Falkland Islands": undefined,
"Malawi": undefined,
"Bangladesh": undefined,
"Canada": undefined,
"Jamaica": "Caribbean",
"Luxembourg": "Central Europe",
"Monaco": undefined,
"Tunisia": "Mediterranean",
"Equatorial Guinea": undefined,
"Martinique": "Caribbean",
"Cape Verde": "Cape Verde",
"Aruba": "Caribbean",
"American Samoa": undefined,
"Nigeria": undefined,
"Cocos (Keeling) Islands": undefined,
"Spain": "Mediterranean",
"Iraq": "Middle Eastern",
"Tokelau": undefined,
"Zimbabwe": undefined,
"Guinea": undefined,
"Ghana": undefined,
"Wallis and Futuna": undefined,
"Kyrgyzstan": undefined,
"Macau": undefined,
"Bahrain": "Middle Eastern",
"Ethiopia": undefined,
"Gibraltar": undefined,
"Isle of Man": undefined,
"Pakistan": undefined,
"Åland Islands": "Nordic",
"Honduras": "Caribbean",
"Haiti": "Caribbean",
"Caribbean Netherlands": "Caribbean",
"Nauru": undefined,
"Trinidad and Tobago": "Caribbean",
"Saint Barthélemy": "Caribbean",
"DR Congo": undefined,
"Denmark": "Nordic",
"Venezuela": "South American",
"Ireland": undefined,
"Dominica": "Caribbean",
"Benin": undefined,
"British Indian Ocean Territory": undefined,
"Mauritania": undefined,
"South Africa": undefined,
"Saint Pierre and Miquelon": undefined,
"San Marino": undefined,
"Egypt": "Middle Eastern",
"Tajikistan": undefined,
"Marshall Islands": undefined,
"Japan": "Japanese",
"Pitcairn Islands": undefined,
"Senegal": undefined,
"Palestine": "Middle Eastern",
"Gabon": undefined,
"Anguilla": "Caribbean",
"Mexico": "Mexican",
"Papua New Guinea": "Caribbean",
"Serbia": "Central Europe",
"Argentina": "South American",
"Mali": undefined,
"Oman": "Middle Eastern",
"Malta": "Mediterranean",
"Maldives": undefined,
"Austria": "Central Europe",
"Western Sahara": undefined,
"North Korea": undefined,
"British Virgin Islands": "Caribbean",
"Burkina Faso": undefined,
"Micronesia": undefined,
"Lithuania": undefined,
"Colombia": "South American",
"Rwanda": undefined,
"Madagascar": undefined,
"France": "French",
"Moldova": "Eastern Europe",
"Nepal": undefined,
"Afghanistan": undefined,
"Saudi Arabia": "Middle Eastern",
"Ecuador": "South American",
"Vatican City": undefined,
"Zambia": undefined,
"Germany": "Central Europe",
"Turkey": "Middle Eastern",
"Yemen": "Middle Eastern",
"Comoros": undefined,
"Curaçao": "Caribbean",
"Italy": "Italian",
"Bulgaria": "Eastern Europe",
"Brazil": "South American",
"Turks and Caicos Islands": "Caribbean",
"Lesotho": undefined,
"Hungary": "Central Europe",
"Guatemala": undefined,
"Chile": "South American",
"Cyprus": "Mediterranean",
"Heard Island and McDonald Islands": undefined,
"Saint Kitts and Nevis": "Caribbean",
"Sierra Leone": undefined,
"Brunei": undefined,
"Seychelles": undefined,
"Saint Helena, Ascension and Tristan da Cunha": undefined,
"Israel": "Middle Eastern",
"Peru": "South American",
"Romania": "Eastern Europe",
"Svalbard and Jan Mayen": undefined,
"French Southern and Antarctic Lands": undefined,
"China": "Chinese",
"Mongolia": undefined,
"Libya": undefined,
"Hong Kong": undefined,
"Algeria": "Mediterranean",
"Guyana": "South American",
"Philippines": "South East Asian",
"Poland": "Eastern Europe",
"Guadeloupe": "Caribbean",
"Armenia": undefined,
"Jordan": "Middle Eastern",
"Niger": undefined,
"Slovenia": "Central Europe",
"Angola": undefined,
"Cook Islands": undefined,
"Russia": "Eastern Europe",
"Cayman Islands": "Caribbean",
"Turkmenistan": undefined,
"Antarctica": undefined,
"Grenada": "Caribbean",
"El Salvador": undefined,
"Saint Lucia": "Caribbean",
"Syria": "Middle Eastern",
"Kazakhstan": undefined,
"Belarus": "Eastern Europe",
"Sri Lanka": "Asian",
"Saint Martin": "Caribbean",
"Liberia": undefined,
"Iceland": "Nordic",
"New Caledonia": undefined,
"Bahamas": "Caribbean",
"United States Minor Outlying Islands": undefined,
"Niue": undefined,
"Samoa": undefined,
"Vietnam": "South East Asian",
"Cameroon": undefined,
"Fiji": undefined,
"Sint Maarten": "Caribbean",
"Nicaragua": undefined,
"Taiwan": undefined,
"Iran": "Middle Eastern",
"Suriname": "South American",
"Togo": undefined,
"Switzerland": "Central Europe",
"Christmas Island": undefined
};