-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatchlist.js
50 lines (43 loc) · 1.78 KB
/
watchlist.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
const watchlistEl = document.getElementById("watchlist");
const watchlistEmpty = document.getElementById("watchlist-empty")
const watchlistBtn = document.getElementsByClassName("watchlist-btn")
function renderWatchlist () {
const watchlist = JSON.parse(localStorage.watchlist)
for (let movie of watchlist) {
watchlistEl.innerHTML += `
<div id="movie">
<img id="poster" src="${movie.Poster}" alt="Poster for ${movie.Title}">
<div id="movie-details">
<div id="line1">
<h4 id="movie-title">${movie.Title}</h4>
<i class="star"></i> <span id="movie-rating">${movie.imdbRating} / 10</span>
</div>
<div id="line2">
<span>${movie.Runtime}</span> <span>${movie.Genre}</span> <button class="watchlist-btn"><i class="watchlist-minus"></i> Remove</button>
</div>
<p id="plot">${movie.Plot}</p>
</div> <!-- movie-details -->
</div> <!-- movie -->
<hr>`
}
}
function removeWatchlistItem(i) {
const newWatchlist = JSON.parse(localStorage.watchlist)
newWatchlist.splice(i, 1)
localStorage.setItem('watchlist', JSON.stringify(newWatchlist))
}
document.onload = checkForWatchlist()
function checkForWatchlist() {
if (!localStorage.watchlist||localStorage.watchlist == "[]") {
watchlistEmpty.style.display = "block"
} else if (localStorage.watchlist) {
watchlistEmpty.style.display = "none"
renderWatchlist();
}
}
for(let i = 0; i < watchlistBtn.length; ++i) {
watchlistBtn[i].addEventListener("click", () => {
removeWatchlistItem(i);
location.reload();
});
}