-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfavourites.html
114 lines (94 loc) · 4.94 KB
/
favourites.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Favourite dishes</title>
<link rel="stylesheet" href="style.css">
<link rel="icon" type="image/x-icon" href="2391.png_860.ico">
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css'>
</head>
<body>
<header>
<div id="mealLogo"><img src="logo-small.png">
</div>
<div id="links">
<span id="l">
<a class="links-a" href="index.html">Home</a>
<a class="links-a" href="contact.html">Contact</a>
<a class="links-a" href="favourites.html">Favourites</a>
</span>
<!-- <span id="s">
<input type="text" id="search" placeholder="search here">
<button id="search-button">search</button>
</span> -->
</div>
</header>
<!-- <div class="container">
<div id="meal"></div>
</div> -->
<h1 style="text-align:center">Your favourites</h1>
<div id="favourite">
</div>
<script>
let favouriteList = JSON.parse(localStorage.getItem('favourite')) //getting the item in localstorage with key value favourite
let favour = document.getElementById("favourite"); //setting the variable as favour for id favourite
let removefav = document.querySelectorAll(".rfav") //setting the variable as removefav for class .rfav
let f = Array.from(favouriteList) //converting favouriteList set to f array
window.onload = (like(favouriteList)) //loadidng the list of favourite items on loading the window
document.addEventListener("click",(e)=>
{
if(e.target.classList == "r-fav" || e.target.classList == "trash") //if we press the trash can icon
{
let r = e.target.parentElement.parentElement.id; //tehn we can remove the item from favourite list
let i= f.indexOf(r);
if(f.includes(f[i]))
{
f.splice(i,1)
}
window.onload = (like(f))
}
})
favouriteList = Array.from(f)
function like(array)
{
let area="";
for(let i = 0;i<array.length;i++)
{
fetch(`https://www.themealdb.com/api/json/v1/1/lookup.php?i=${array[i]}`) //fetching the url and appending the searchtext and loading the results
.then(res=>res.json()) //converting it to json()
.then(data=>{ //mapping the data with each of the element present in the meals array
area +=`
<div class="meal_id" id=${data.meals[0].idMeal}>
<img class="meal_image" src="${data.meals[0].strMealThumb}" >
<div class="mealname">
<h2>${data.meals[0].strMeal}</h2>
</div>
<div class="mealDetails" id=${data.meals[0].idMeal}>
<button class="r-fav" ><img class="trash" src="trash-can-solid.svg"></button>
<a class="rec" target="_blank" href="meal.html" id=${meal.idMeal} >Cook</a>
</div>
</div>
`
favour.innerHTML = area;
})
}
}
</script>
</body>
<!-- </html>
let favourite = []
const favouriteList = new Set();
localStorage.setItem('favourite', JSON.stringify(favourite)); //saving the favourite list to localstorage and getting the id in the meals page
document.addEventListener("click",(e)=>
{
if(e.target.classList == "fav" ) //if the target element's classlist is fav
{
favouriteList.add(e.target.id); //adding the item to favouriteList set and
favourite= Array.from(favouriteList) //and filling this values to favourite array
localStorage.setItem('favourite', JSON.stringify(favourite)); //sending the data to localstorage
alert(`${e.target.name} added to your favourites`)
}
// console.log(favourite)
}) -->