-
Notifications
You must be signed in to change notification settings - Fork 1
/
favorite.js
69 lines (60 loc) · 2.37 KB
/
favorite.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
// Getting the local Storage Information and assigning to list array
var storageInfo = localStorage.getItem('MovieArray');
var listArray = JSON.parse(storageInfo);
// console.log(listArray);
//Calling the each item of the movie by id
listArray.forEach(async id=>{
loadMovies(id);
});
// Retrieving the each item of the list by id
async function loadMovies(id){
const URL = `https://www.omdbapi.com/?i=${id}&apikey=6741a269`;
console.log(URL);
const res = await fetch(`${URL}`);
const movieDetails = await res.json();
console.log(movieDetails.Title);
if(movieDetails.Response =="True"){
displayMovieDetails(movieDetails);
}
}
// Displaying the each movie element of the list
function displayMovieDetails(details) {
var favList = document.createElement('div');
favList.classList.add('list-item');
favList.innerHTML =`
<div class="movie-details">
<div class="thumbnail">
<img src="${details.Poster}" alt="thumbnail">
</div>
<div class="title">
<a href="movie.html?id=${details.imdbID}"> ${details.Title} </a>
<span>Duration: ${details.Runtime} | Released on: ${details.Released}</span>
</div>
<div class="delete-movie-from-list" id="${details.imdbID}" onclick="deleteItem(${details.imdbID})">
<i class="fas fa-trash-alt"></i>
</div>
`;
// console.log("Aslu",details.imdbID);
document.getElementById('movie-container').appendChild(favList);
}
//Function to delete each movie item from the list
async function deleteItem(buttonID){
var butonIDInfo = (buttonID.id).toString();
// console.log("insidedelte");
console.log(butonIDInfo);
if(window.confirm('Do you want to delete this Movie from list?')){
console.log(butonIDInfo);
var temp = await JSON.parse(localStorage.getItem('MovieArray'));
var index = await temp.indexOf(butonIDInfo.toString());
await temp.splice(index,1);
await localStorage.setItem('MovieArray', JSON.stringify(temp));
window.location.reload();
}
}
//Function to delete all movies of the list
function clearAll () {
if(window.confirm("Do you want to clear your favorite list?")){
localStorage.clear();
window.location.reload();
}
}