-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
103 lines (86 loc) · 4.07 KB
/
app.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
const randomMealBtn = document.querySelector('#random-meal');
const mealContainer = document.querySelector('.meal-container');
randomMealBtn.addEventListener('click', () => {
fetch('https://www.themealdb.com/api/json/v1/1/random.php')
.then(response => response.json())
.then(data => data.meals[0])
.then(meal => createMeal(meal))
.catch(error => console.error(error));
});
const getIngredients = (meal, num = 1) => {
return meal[`strIngredient${num}`]
? [{
'name': meal[`strIngredient${num}`],
'amount': meal[`strMeasure${num}`]
}].concat(getIngredients(meal, num + 1)): '';
}
const createMeal = (meal) => {
const ingredients = getIngredients(meal).slice(0, -1); // remove last item
// remove inconsistencies in the data as much as possible and format it accordingly
const instructions = meal.strInstructions
.trim()
.replace(/^\.?(?:(?:STEP\s)?[0-9]\.?(?:\s|\s\1)*)?|(\s?\r\n)+(?:(?:\s?STEP)?\s?\d+\.?\1*)?|(\.$)|(?<!\.\s?)(\r\n)+/gi, (m, g1, g2, g3) => {
if (g1) return `</li>\n<li class="steps">`;
else if (g2) return `.</li>`;
else if (g3) return '';
else return `<li class="steps">`;
});
mealContainer.innerHTML = `
<header class="flex-row">
<h2 id="meal-title">${meal.strMeal}</h2>
</header>
<section class="meal-content">
<section class="primary">
<section class="thumbnail-container">
<img src='${meal.strMealThumb}' alt='${meal.strMeal}' title='${meal.strMeal}'>
</section>
<section class="ingredients">
<header class="flex-row">
<h3 id="ingredient-title">Ingredients</h3>
</header>
<ol>
${ingredients.length
? ingredients.map(ingredient =>
`<li><p>${ingredient.name}<span class="amount">${ingredient.amount}</span></p></li>`).join('\n') : ''}
</ol>
</section>
<section class="tags">
${meal.strArea.toLowerCase() !== 'unknown'
? `<div class="country"><i class="fa-solid fa-location-dot"></i> ${meal.strArea}</div>` : ''}
${meal.strCategory
? `<div class="category"><i class="fa-solid fa-hashtag"></i> ${meal.strCategory}</div>` : ''}
</section>
</section>
<section class="secondary">
<section class="instructions">
<header class="flex-row">
<h3 id="instruction-title">Instructions</h3>
</header>
<ul>
${instructions}
</ul>
</section>
${meal.strYoutube ?
`<section class="button-container flex-row">
<button id="watch-video" onclick="displayVideo('${meal.strYoutube.match(/(?<=\?v=).+/g)}')">
<i class="fa-solid fa-play"></i>
Watch video
</button>
</section>` : ''}
</section>
</section>
`;
document.body.classList.remove('flex-column');
}
const displayVideo = (videoId) => {
const [videoContainer, iframe, btn] = ['section', 'iframe', 'button'].map(elem => document.createElement(elem));
videoContainer.classList.add('video-container', 'flex-row');
videoContainer.addEventListener('click', () => videoContainer.remove());
iframe.setAttribute('src', `https://www.youtube.com/embed/${videoId}`);
iframe.setAttribute('allowfullscreen', '');
btn.setAttribute('id', 'remove-video');
btn.innerHTML = `<i class="fa-solid fa-xmark"></i>`;
btn.addEventListener('click', () => videoContainer.remove());
[iframe, btn].forEach(elem => videoContainer.appendChild(elem));
document.body.appendChild(videoContainer);
}