-
Notifications
You must be signed in to change notification settings - Fork 0
/
favorite.js
66 lines (58 loc) · 2.31 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
(function () {
const BASE_URL = 'https://movie-list.alphacamp.io'
const INDEX_URL = BASE_URL + '/api/v1/movies/'
const POSTER_URL = BASE_URL + '/posters/'
const dataPanel = document.getElementById('data-panel')
const data = JSON.parse(localStorage.getItem('favoriteMovies')) || []
displayDataList(data)
dataPanel.addEventListener('click', event => {
if(event.target.matches('.btn-show-movie')) {
showMovie(event.target.dataset.id)
} else if (event.target.matches('.btn-remove-favorite')) {
removeFavoriteItem(event.target.dataset.id)
}
})
function displayDataList (data) {
let htmlContent = ''
data.forEach(item => {
htmlContent += `
<div class="col-sm-3">
<div class="card mb-2">
<img class="card-img-top " src="${POSTER_URL}${item.image}" alt="Card image cap">
<div class="card-body movie-item-body">
<h5 class="card-title">${item.title}</h5>
</div>
<div class="card-footer">
<button class="btn btn-primary btn-show-movie" data-toggle="modal" data-target="#show-movie-modal" data-id="${item.id}">More</button>
<button class="btn btn-danger btn-remove-favorite" data-id="${item.id}">X</button>
</div>
</div>
</div>
`
})
dataPanel.innerHTML = htmlContent
}
function showMovie(id) {
const modalTitle = document.getElementById('show-movie-title')
const modalImage = document.getElementById('show-movie-image')
const modalDate = document.getElementById('show-movie-date')
const modalDescription = document.getElementById('show-movie-description')
const url = INDEX_URL + id
axios.get(url)
.then(response => {
const data = response.data.results
modalTitle.textContent = data.title
modalImage.innerHTML = `<img src="${POSTER_URL}${data.image}" class="img-fluid" alt="Responsive image">`
modalDate.textContent = `release at : ${data.release_date}`
modalDescription.textContent = `${data.description}`
})
.catch()
}
function removeFavoriteItem(id) {
const index = data.findIndex(item => item.id === Number(id))
if (index === -1) return
data.splice(index,1)
localStorage.setItem('favoriteMovies', JSON.stringify(data))
displayDataList(data)
}
})()