-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
244 lines (217 loc) · 9.03 KB
/
app.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
const API_KEY = `04ec85a684eae5ff819855d99f118b3b`
const image_path = `https://image.tmdb.org/t/p/w1280`
// - API key will only be available for this video since I'll delete it right after that but you can go to https://www.themoviedb.org/ , create an account and get a key for free.
// - Image path is only the base url from where we'll get the images.
const input = document.querySelector('.search input')
const btn = document.querySelector('.search button')
const main_grid_title = document.querySelector('.favorites h1')
const main_grid = document.querySelector('.favorites .movies-grid')
const trending_el = document.querySelector('.trending .movies-grid')
const popup_container = document.querySelector('.popup-container')
function add_click_effect_to_card (cards) {
cards.forEach(card => {
card.addEventListener('click', () => show_popup(card))
})
}
// SEARCH MOVIES
async function get_movie_by_search (search_term) {
const resp = await fetch(`https://api.themoviedb.org/3/search/movie?api_key=${API_KEY}&query=${search_term}`)
const respData = await resp.json()
return respData.results
}
btn.addEventListener('click', add_searched_movies_to_dom)
async function add_searched_movies_to_dom () {
const data = await get_movie_by_search(input.value)
main_grid_title.innerText = `Search Results...`
main_grid.innerHTML = data.map(e => {
return `
<div class="card" data-id="${e.id}">
<div class="img">
<img src="${image_path + e.poster_path}">
</div>
<div class="info">
<h2>${e.title}</h2>
<div class="single-info">
<span>Rate: </span>
<span>${e.vote_average} / 10</span>
</div>
<div class="single-info">
<span>Release Date: </span>
<span>${e.release_date}</span>
</div>
</div>
</div>
`
}).join('')
const cards = document.querySelectorAll('.card')
add_click_effect_to_card(cards)
}
// POPUP
async function get_movie_by_id (id) {
const resp = await fetch(`https://api.themoviedb.org/3/movie/${id}?api_key=${API_KEY}`)
const respData = await resp.json()
return respData
}
async function get_movie_trailer (id) {
const resp = await fetch(`https://api.themoviedb.org/3/movie/${id}/videos?api_key=${API_KEY}`)
const respData = await resp.json()
return respData.results[0].key
}
async function show_popup (card) {
popup_container.classList.add('show-popup')
const movie_id = card.getAttribute('data-id')
const movie = await get_movie_by_id(movie_id)
const movie_trailer = await get_movie_trailer(movie_id)
popup_container.style.background = `linear-gradient(rgba(0, 0, 0, .8), rgba(0, 0, 0, 1)), url(${image_path + movie.poster_path})`
popup_container.innerHTML = `
<span class="x-icon">✖</span>
<div class="content">
<div class="left">
<div class="poster-img">
<img src="${image_path + movie.poster_path}" alt="">
</div>
<div class="single-info">
<span>Add to favorites:</span>
<span class="heart-icon">♥</span>
</div>
</div>
<div class="right">
<h1>${movie.title}</h1>
<h3>${movie.tagline}</h3>
<div class="single-info-container">
<div class="single-info">
<span>Language:</span>
<span>${movie.spoken_languages[0].name}</span>
</div>
<div class="single-info">
<span>Length:</span>
<span>${movie.runtime} minutes</span>
</div>
<div class="single-info">
<span>Rate:</span>
<span>${movie.vote_average} / 10</span>
</div>
<div class="single-info">
<span>Budget:</span>
<span>$ ${movie.budget}</span>
</div>
<div class="single-info">
<span>Release Date:</span>
<span>${movie.release_date}</span>
</div>
</div>
<div class="genres">
<h2>Genres</h2>
<ul>
${movie.genres.map(e => `<li>${e.name}</li>`).join('')}
</ul>
</div>
<div class="overview">
<h2>Overview</h2>
<p>${movie.overview}</p>
</div>
<div class="trailer">
<h2>Trailer</h2>
<iframe width="560" height="315" src="https://www.youtube.com/embed/${movie_trailer}" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
</div>
</div>
`
const x_icon = document.querySelector('.x-icon')
x_icon.addEventListener('click', () => popup_container.classList.remove('show-popup'))
const heart_icon = popup_container.querySelector('.heart-icon')
const movie_ids = get_LS()
for(let i = 0; i <= movie_ids.length; i++) {
if (movie_ids[i] == movie_id) heart_icon.classList.add('change-color')
}
heart_icon.addEventListener('click', () => {
if(heart_icon.classList.contains('change-color')) {
remove_LS(movie_id)
heart_icon.classList.remove('change-color')
} else {
add_to_LS(movie_id)
heart_icon.classList.add('change-color')
}
fetch_favorite_movies()
})
}
// Local Storage
function get_LS () {
const movie_ids = JSON.parse(localStorage.getItem('movie-id'))
return movie_ids === null ? [] : movie_ids
}
function add_to_LS (id) {
const movie_ids = get_LS()
localStorage.setItem('movie-id', JSON.stringify([...movie_ids, id]))
}
function remove_LS (id) {
const movie_ids = get_LS()
localStorage.setItem('movie-id', JSON.stringify(movie_ids.filter(e => e !== id)))
}
// Favorite Movies
fetch_favorite_movies()
async function fetch_favorite_movies () {
main_grid.innerHTML = ''
const movies_LS = await get_LS()
const movies = []
for(let i = 0; i <= movies_LS.length - 1; i++) {
const movie_id = movies_LS[i]
let movie = await get_movie_by_id(movie_id)
add_favorites_to_dom_from_LS(movie)
movies.push(movie)
}
}
function add_favorites_to_dom_from_LS (movie_data) {
main_grid.innerHTML += `
<div class="card" data-id="${movie_data.id}">
<div class="img">
<img src="${image_path + movie_data.poster_path}">
</div>
<div class="info">
<h2>${movie_data.title}</h2>
<div class="single-info">
<span>Rate: </span>
<span>${movie_data.vote_average} / 10</span>
</div>
<div class="single-info">
<span>Release Date: </span>
<span>${movie_data.release_date}</span>
</div>
</div>
</div>
`
const cards = document.querySelectorAll('.card')
add_click_effect_to_card(cards)
}
// Trending Movies
get_trending_movies()
async function get_trending_movies () {
const resp = await fetch(`https://api.themoviedb.org/3/trending/all/day?api_key=${API_KEY}`)
const respData = await resp.json()
return respData.results
}
add_to_dom_trending()
async function add_to_dom_trending () {
const data = await get_trending_movies()
console.log(data);
trending_el.innerHTML = data.slice(0, 5).map(e => {
return `
<div class="card" data-id="${e.id}">
<div class="img">
<img src="${image_path + e.poster_path}">
</div>
<div class="info">
<h2>${e.title}</h2>
<div class="single-info">
<span>Rate: </span>
<span>${e.vote_average} / 10</span>
</div>
<div class="single-info">
<span>Release Date: </span>
<span>${e.release_date}</span>
</div>
</div>
</div>
`
}).join('')
}