forked from ALPHACamp/movie-list
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
142 lines (120 loc) · 4.16 KB
/
Copy pathindex.js
File metadata and controls
142 lines (120 loc) · 4.16 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const BASE_URL = 'https://movie-list.alphacamp.io'
const INDEX_URL = BASE_URL + '/api/v1/movies/'
const POSTER_URL = BASE_URL + '/posters/'
const movies = [] //電影總清單
let filteredMovies = [] //搜尋清單
const MOVIES_PER_PAGE = 12
const dataPanel = document.querySelector('#data-panel')
const searchForm = document.querySelector('#search-form')
const searchInput = document.querySelector('#search-input')
const paginator = document.querySelector('#paginator')
function renderMovieList(data) {
let rawHTML = ''
data.forEach((item) => {
// title, image, id
rawHTML += `<div class="col-sm-3">
<div class="mb-2">
<div class="card">
<img src="${POSTER_URL + item.image
}" class="card-img-top" alt="Movie Poster">
<div class="card-body">
<h5 class="card-title">${item.title}</h5>
</div>
<div class="card-footer">
<button
class="btn btn-primary
btn-show-movie"
data-bs-toggle="modal"
data-bs-target="#movie-modal"
data-id="${item.id}"
>
More
</button>
<button
class="btn btn-info btn-add-favorite"
data-id="${item.id}"
>
+
</button>
</div>
</div>
</div>
</div>`
})
dataPanel.innerHTML = rawHTML
}
function renderPaginator(amount) {
const numberOfPages = Math.ceil(amount / MOVIES_PER_PAGE)
let rawHTML = ''
for (let page = 1; page <= numberOfPages; page++) {
rawHTML += `<li class="page-item"><a class="page-link" href="#" data-page="${page}">${page}</a></li>`
}
paginator.innerHTML = rawHTML
}
function getMoviesByPage(page) {
const data = filteredMovies.length ? filteredMovies : movies
const startIndex = (page - 1) * MOVIES_PER_PAGE
return data.slice(startIndex, startIndex + MOVIES_PER_PAGE)
}
function showMovieModal(id) {
// get elements
const modalTitle = document.querySelector('#movie-modal-title')
const modalImage = document.querySelector('#movie-modal-image')
const modalDate = document.querySelector('#movie-modal-date')
const modalDescription = document.querySelector('#movie-modal-description')
// send request to show api
axios.get(INDEX_URL + id).then((response) => {
const data = response.data.results
// insert data into modal ui
modalTitle.innerText = data.title
modalDate.innerText = 'Release date: ' + data.release_date
modalDescription.innerText = data.description
modalImage.innerHTML = `<img src="${POSTER_URL + data.image
}" alt="movie-poster" class="img-fluid">`
})
}
function addToFavorite(id) {
const list = JSON.parse(localStorage.getItem('favoriteMovies')) || []
const movie = movies.find((movie) => movie.id === id)
if (list.some((movie) => movie.id === id)) {
return alert('此電影已經在收藏清單中!')
}
list.push(movie)
localStorage.setItem('favoriteMovies', JSON.stringify(list))
}
// listen to data panel
dataPanel.addEventListener('click', function onPanelClicked(event) {
if (event.target.matches('.btn-show-movie')) {
showMovieModal(event.target.dataset.id)
} else if (event.target.matches('.btn-add-favorite')) {
addToFavorite(Number(event.target.dataset.id))
}
})
//listen to search form
searchForm.addEventListener('submit', function onSearchFormSubmitted(event) {
event.preventDefault()
const keyword = searchInput.value.trim().toLowerCase()
filteredMovies = movies.filter((movie) =>
movie.title.toLowerCase().includes(keyword)
)
if (filteredMovies.length === 0) {
return alert(`您輸入的關鍵字:${keyword} 沒有符合條件的電影`)
}
renderPaginator(filteredMovies.length)
renderMovieList(getMoviesByPage(1))
})
// listen to paginator
paginator.addEventListener('click', function onPaginatorClicked(event) {
if (event.target.tagName !== 'A') return
const page = Number(event.target.dataset.page)
renderMovieList(getMoviesByPage(page))
})
// send request to index api
axios
.get(INDEX_URL)
.then((response) => {
movies.push(...response.data.results)
renderPaginator(movies.length)
renderMovieList(getMoviesByPage(1))
})
.catch((err) => console.log(err))