-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.js
56 lines (50 loc) · 1.75 KB
/
data.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
const watchList = document.getElementById('watch-list')
let storedMovies = JSON.parse(localStorage.getItem('myMovies'))
let watchListArray = []
document.addEventListener('click', (e) => {
if (e.target.dataset.remove) {
handleRemoveClick(e.target.dataset.remove)
}
})
if (storedMovies) {
watchListArray = storedMovies
}
function updateWatchlist() {
if (watchListArray) {
watchList.innerHTML = ``
for (let movie of watchListArray) {
watchList.innerHTML += `
<div class="movie-container>
<img class="poster" src="${movie.Poster}" />
<div class="info1>
<h4 class="movie-title">${movie.Title}</h4>
<p class="rating">${movie.imdbRating}</p>
</div>
<div class="info2>
<p class="run-time">${movie.Runtime}</p>
<p class="genre">${movie.Genre}</p>
<button id="remove-btn" data-remove="${movie.imdbID}">- Remove from Watchlist</button>
</div>
<p class="plot">${movie.Plot}</p>
</div>
`
}
} else {
watchList.innerHTML += `
<div class="empty-list">
<p class="unsuccessful">Your watchlist is looking a little empty...</p>
<button><a href="index.html>+ Let's add some movies!</a></button>
</div>
`
}
}
function handleRemoveClick(movieID) {
const filteredMovies = watchListArray.filter((movie) => {
return movie.imdbID !== movieID
})
watchListArray = filteredMovies
localStorage.setItem('myMovies', JSON.stringify(watchListArray))
console.log(watchListArray)
updateWatchlist()
}
updateWatchlist()