-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
170 lines (164 loc) · 7.39 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
// it makes a favourites meal array if its not exist in local storage
if (localStorage.getItem("favouritesList") == null) {
localStorage.setItem("favouritesList", JSON.stringify([]));
}
// it fetches meals from api and return it
async function fetchMealsFromApi(url, value) {
const response = await fetch(`${url + value}`);
const meals = await response.json();
return meals;
}
// it show's all meals card in main acording to search input value
function showMealList() {
let inputValue = document.getElementById("my-search").value;
let arr = JSON.parse(localStorage.getItem("favouritesList"));
let url = "https://www.themealdb.com/api/json/v1/1/search.php?s=";
let html = "";
let meals = fetchMealsFromApi(url, inputValue);
meals.then((data) => {
if (data.meals) {
data.meals.forEach((element) => {
let isFav = false;
for (let index = 0; index < arr.length; index++) {
if (arr[index] == element.idMeal) {
isFav = true;
}
}
if (isFav) {
html += `
<div id="card" class="card mb-3" style="width: 20rem;">
<img src="${element.strMealThumb}" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">${element.strMeal}</h5>
<div class="d-flex justify-content-between mt-5">
<button type="button" class="btn btn-outline-light" onclick="showMealDetails(${element.idMeal})">More Details</button>
<button id="main${element.idMeal}" class="btn btn-outline-light active" onclick="addRemoveToFavList(${element.idMeal})" style="border-radius:50%"><i class="fa-solid fa-heart" style="color: #ff0000;"></i></button>
</div>
</div>
</div>
`;
} else {
html += `
<div id="card" class="card mb-3" style="width: 20rem;">
<img src="${element.strMealThumb}" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">${element.strMeal}</h5>
<div class="d-flex justify-content-between mt-5">
<button type="button" class="btn btn-outline-light" onclick="showMealDetails(${element.idMeal})">More Details</button>
<button id="main${element.idMeal}" class="btn btn-outline-light" onclick="addRemoveToFavList(${element.idMeal})" style="border-radius:50%"><i class="fa-solid fa-heart"></i></button>
</div>
</div>
</div>
`;
}
});
} else {
html += `
<div class="page-wrap d-flex flex-row align-items-center">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12 text-center">
<span class="display-1 d-block">404</span>
<div class="mb-4 lead">
The meal you are looking for was not found.
</div>
</div>
</div>
</div>
</div>
`;
}
document.getElementById("main").innerHTML = html;
});
}
//it shows full meal details in main
async function showMealDetails(id) {
let url = "https://www.themealdb.com/api/json/v1/1/lookup.php?i=";
let html = "";
await fetchMealsFromApi(url, id).then((data) => {
html += `
<div id="meal-details" class="mb-5">
<div id="meal-header" class="d-flex justify-content-around flex-wrap">
<div id="meal-thumbail">
<img class="mb-2" src="${data.meals[0].strMealThumb}" alt="" srcset="">
</div>
<div id="details">
<h3>${data.meals[0].strMeal}</h3>
<h6>Category : ${data.meals[0].strCategory}</h6>
<h6>Area : ${data.meals[0].strArea}</h6>
</div>
</div>
<div id="meal-instruction" class="mt-3">
<h5 class="text-center">Instruction :</h5>
<p>${data.meals[0].strInstructions}</p>
</div>
<div class="text-center">
<a href="${data.meals[0].strYoutube}" target="_blank" class="btn btn-outline-light mt-3">Watch Video</a>
<a href="${data.meals[0].strSource}" target="_blank" class="btn btn-outline-light mt-3">Read Article</a>
</div>
</div>
`;
});
document.getElementById("main").innerHTML = html;
}
// it shows all favourites meals in favourites body
async function showFavMealList() {
let arr = JSON.parse(localStorage.getItem("favouritesList"));
let url = "https://www.themealdb.com/api/json/v1/1/lookup.php?i=";
let html = "";
if (arr.length == 0) {
html += `
<div class="page-wrap d-flex flex-row align-items-center">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12 text-center">
<span class="display-1 d-block">404</span>
<div class="mb-4 lead">
No meal added in your favourites list.
</div>
</div>
</div>
</div>
</div>
`;
} else {
for (let index = 0; index < arr.length; index++) {
await fetchMealsFromApi(url, arr[index]).then((data) => {
html += `
<div id="card" class="card mb-3" style="width: 20rem;">
<img src="${data.meals[0].strMealThumb}" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">${data.meals[0].strMeal}</h5>
<div class="d-flex justify-content-between mt-5">
<button type="button" id="details-btn" class="btn btn-outline-light" onclick="showMealDetails(${data.meals[0].idMeal})">More Details</button>
<button id="main${data.meals[0].idMeal}" class="btn btn-outline-light active" onclick="addRemoveToFavList(${data.meals[0].idMeal})" style="border-radius:50%"><i class="fa-solid fa-heart" style="color: #ff0000;"></i></button>
</div>
</div>
</div>
`;
});
}
}
document.getElementById("favourites-body").innerHTML = html;
}
//it adds and remove meals to favourites list
function addRemoveToFavList(id) {
let arr = JSON.parse(localStorage.getItem("favouritesList"));
let contain = false;
for (let index = 0; index < arr.length; index++) {
if (id == arr[index]) {
contain = true;
}
}
if (contain) {
let number = arr.indexOf(id);
arr.splice(number, 1);
alert("Meal removed from favourites list");
} else {
arr.push(id);
alert("Meal added to favourites list");
}
localStorage.setItem("favouritesList", JSON.stringify(arr));
showMealList();
showFavMealList();
}