forked from WhalenSITHS/Create-TaskQuestions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbbas.js
More file actions
82 lines (75 loc) · 2.45 KB
/
Abbas.js
File metadata and controls
82 lines (75 loc) · 2.45 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
import "./style.css";
let viewed = [];
async function mainCall() {
const response = await fetch(
`https://www.themealdb.com/api/json/v1/1/categories.php`
);
const data = await response.json();
data.categories.forEach((catergory) => {
document.getElementById("options").insertAdjacentHTML(
"afterbegin",
`
<input type="checkbox" class="box" value="${catergory.strCategory}">
<label for="${catergory.strCategory}">${catergory.strCategory}</label><br>`
);
});
document.getElementById("call").addEventListener("click", (event) => {
event.preventDefault();
document.getElementById("selection").innerHTML = "";
document.querySelectorAll(".box").forEach((box) => {
if (box.checked) {
APICall(box.value);
}
});
});
}
async function APICall(input) {
const response = await fetch(
`https://www.themealdb.com/api/json/v1/1/filter.php?c=${input}`
);
const data = await response.json();
document
.getElementById("selection")
.insertAdjacentHTML("beforeend", `<h2>${input}</h2>`);
data.meals.forEach((dish) => {
document
.getElementById("selection")
.insertAdjacentHTML(
"beforeend",
`<p>${dish.strMeal}</p><img class="${input}" id="${dish.idMeal}"src="${dish.strMealThumb}">`
);
});
document.querySelectorAll(`.${input}`).forEach((img) => {
img.addEventListener("click", (event) => {
document.getElementById("info").innerHTML = "";
displayMain(img.id);
if (viewed.includes(img.id) === false) {
console.log(viewed.length);
if (viewed.length > 4) {
viewed.shift();
}
viewed.push(img.id);
}
});
});
}
async function displayMain(id) {
const response = await fetch(
`https://www.themealdb.com/api/json/v1/1/lookup.php?i=${id}`
);
const data = await response.json();
document
.getElementById("info")
.insertAdjacentHTML(
"afterbegin",
`<div class="infobox"><h2>${data.meals[0].strMeal}</h2><div class="foodInfo"><p>Type: ${data.meals[0].strCategory}</p><p>Region: ${data.meals[0].strArea}</p></div><img id="${id}"src="${data.meals[0].strMealThumb}"><p>${data.meals[0].strInstructions}</p></div>`
);
}
mainCall();
document.getElementById("history").addEventListener("click", (event) => {
event.preventDefault();
console.log(viewed);
document.getElementById("info").innerHTML = "";
viewed.forEach((dish) => displayMain(dish));
});
//Credit To TheMealDB for API