-
Notifications
You must be signed in to change notification settings - Fork 0
/
details.js
61 lines (46 loc) · 1.94 KB
/
details.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
const mealTitle = document.getElementById('meal-name');
const category = document.getElementById('cat');
const area = document.getElementById('area');
const vid = document.getElementById('ifream');
const ins = document.getElementById('ins');
const image = document.getElementById('thumb');
const ingredients = document.getElementById('ingredients');
//retrieve id from queryString
let id = window.location.search.split('?')[1];
//search for it in localStorage render the details
let meal = JSON.parse(localStorage.getItem(id));
document.title = meal.strMeal;
//function to render the details using jsonata from fetch
function renderDetailsPage(meal) {
const {strMeal, strCategory, strArea, strYoutube, strInstructions, strMealThumb} = meal;
mealTitle.innerText = strMeal;
category.innerHTML = strCategory;
area.innerHTML = strArea;
vid.setAttribute('src', strYoutube.replace("watch?v=", "embed/"));
ins.innerHTML = strInstructions;
image.src = strMealThumb;
//loop to map ingredients with their measure(max ingredients in meals is 20)
for(let i = 1; i<21; i++){
let index = i.toString();
let ingredient = 'strIngredient' + index;
let measure = 'strMeasure' + index;
if(meal[ingredient] === "" || meal[ingredient] === null){
break;
}
const li = document.createElement('li');
li.innerHTML = `<input type='checkbox'> ${meal[measure]} ${meal[ingredient]}`
ingredients.appendChild(li)
}
}
window.onload = () =>{
renderDetailsPage(meal);
}
// window.onload = () =>{
// //fetch the meal details with the given ID
// const url = `https://www.themealdb.com/api/json/v1/1/lookup.php?i=${mealId}`;
// fetch(url)
// .then(response => response.json())
// .then(jsonData => renderDetailsPage(jsonData.meals[0]))
// .catch((error) =>{
// throw(error)
// });