-
Notifications
You must be signed in to change notification settings - Fork 0
/
popularMovie.js
306 lines (258 loc) · 8.67 KB
/
popularMovie.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
const url = `https://api.themoviedb.org/3/movie`;
const searchMultiUrl = `https://api.themoviedb.org/3/search/multi`;
const imgUrl = `https://image.tmdb.org/t/p/original`;
const emptyImg = `https://s3-us-west-1.amazonaws.com/files.delesign/assets/Not-Found-1.svg`;
const options = {
method: "GET",
headers: {
accept: "application/json",
Authorization:
"Bearer eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJmZmMyZGZhZDQ3YThlZDRmMWUwYWQxYjc1MGVhMzBhMSIsInN1YiI6IjY1MzA5NGQ3YWVkZTU5MDE0YzM4MDBjZiIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.F6qLqVKcX12Lxl8WUe5P3sDfhlIdJ44DaMgj0Dvuq1M",
},
};
window.addEventListener("load", () => {
document.querySelector(`#search`).focus();
});
let movieList = [];
//모달 보이게
const scoreModal = () => {
document.querySelector('#searchModal').style.display = 'block';
}
const infoModalOpen = (movieId) => {
document.querySelector('#infoModal').style.display = 'block';
movieInfo(movieId)
}
//모달 닫고 모달안에 있는 input 데이터 초기화
const scoreModalClose = () => {
document.querySelector('#searchModal').style.display = 'none';
document.querySelector('#score').value = ``;
}
const infoModalClose = () => {
document.querySelector('#infoModal').style.display = 'none';
}
const makeMovieCard = (movieId, postImg, movieTitle, voteAverage, overView) => {
const movieCard = document.createElement("div");
movieCard.className = 'movieCard';
movieCard.addEventListener('click', () => infoModalOpen(movieId))
movieCard.innerHTML = `<div class="moviePoster">
<img src=${postImg === null ? emptyImg : postImg} alt="">
</div>
<div class="movieTitle">${movieTitle}</div>
<div class="voteAverage" >🍅 : <span id="voteAverage">${voteAverage}</span></div>
<div class="overView">${overView}</div>`;
return document.querySelector("#movieList").appendChild(movieCard);
};
//영화 리스트
fetch(`${url}/popular?language=ko-KR&page=1`, options)
.then((response) => response.json())
.then((response) => {
movieList = response.results;
response.results.map((res) => {
let postImg =
res.poster_path === null ? emptyImg : ` ${imgUrl}${res.poster_path}`;
let movieTitle = res.title;
let voteAverage = res.vote_average;
let overView = res.overview;
makeMovieCard(res.id, postImg, movieTitle, voteAverage, overView);
sortByRating(voteAverage);
});
})
.catch((err) => console.error(err));
// 영화 검색
const enterKey = (event) => {
if (event.keyCode === 13) {
searchQuery();
}
};
const searchQuery = () => {
const searchParams = document.querySelector("#search").value;
document.querySelector("#score").value = ``;
searchParams ? clearCard() : alert("검색어를 입력해주세요");
searchResults(searchParams);
};
const scoreSearch = () => {
const inputScore = document.querySelector('#score').value
const scoreCheck = document.querySelector('#scoreCheck > option:checked').value
const scoreFilter = movieList;
clearCard()
scoreModalClose()
const scoreCheck = document.querySelector(
"#scoreCheck > option:checked"
).value;
const scoreFilter = movieList;
clearCard();
closeModal();
scoreFilter.filter((res) => {
let score = res.vote_average;
let movieId = res.id;
let postImg =
res.poster_path === null ? emptyImg : ` ${imgUrl}${item.poster_path}`;
let movieTitle = res.title;
let overView = res.overview;
switch (scoreCheck) {
case "up":
if (score >= inputScore) {
makeMovieCard(movieId, postImg, movieTitle, score, overView);
}
break;
case "down":
if (score <= inputScore) {
makeMovieCard(movieId, postImg, movieTitle, score, overView);
}
break;
}
});
};
const clearCard = () => {
const cardList = document.querySelectorAll(`.movieCard `);
cardList.forEach((element) => {
element.remove();
});
};
const searchResults = (searchParams) => {
fetch(
`${searchMultiUrl}?query=${searchParams}&include_adult=false&language=ko-KR&page=1`,
options
)
.then((response) => response.json())
.then((response) => {
let data = response.results;
console.log();
data.map((movie) => {
if (movie.media_type === "movie") {
let movieId = movie.id;
let movieTitle = movie.title;
let voteAverage = movie.vote_average.toFixed(2);
let postImg =
movie.poster_path === null
? emptyImg
: `${imgUrl}${movie.poster_path}`;
let overView = movie.overview;
makeMovieCard(movieId, postImg, movieTitle, voteAverage, overView);
} else if (movie.media_type === "person") {
let knownFor = movie.known_for;
knownFor.filter((item) => {
let movieId = item.id;
let movieTitle = item.title;
let voteAverage = item.vote_average.toFixed(2);
let postImg =
item.poster_path === null
? emptyImg
: `${imgUrl}${item.poster_path}`;
let overView = item.overview;
console.log(item);
if (item.media_type === "movie") {
makeMovieCard(
movieId,
postImg,
movieTitle,
voteAverage,
overView
);
}
});
}
});
})
.catch((err) => console.error(err));
};
// 관람연령 가져오는 함수
const getMovieAge = (movieId) => {
let age;
return fetch(`${url}/${movieId}/release_dates`, options)
.then(response => response.json())
.then(response => {
response.results.map((res) => {
switch (res.iso_3166_1) {
case "KR":
if (!age) {
age = res.release_dates[0].certification;
}
console.log("KR:",age)
return age;
break;
case "US":
if (!age) {
age = res.release_dates[0].certification;
}
console.log("US:",age)
return age;
break;
default:
break;
}
console.log(age)
;
})
return age;
})
.catch(err => console.log(err));
}
// 장르 가져오는 함수
const movieGenres = (genres) => {
let genresArray = [];
genres.map(data => {
genresArray = [...genresArray, data.name]
})
genresArray.join(`,`)
document.querySelector(`.tabContent .movieGenres span`).textContent = genresArray
}
// 배우 및 감독 정보 가져오는 함수
const getMovieCredits = (movieId) => {
return fetch(`${url}/${movieId}/credits?language=ko-KR`, options)
.then(response => response.json())
.then(response => {
response.cast.map((data) => {
// console.log(document.querySelector(`.tabContent .movieGenres span`))
// document.querySelector(`.tabContent .movieGenres span`).textContent = data.name
})
})
.catch(err => console.error(err));
}
const movieInfo = (movieId) => {
fetch(`${url}/${movieId}?language=ko-KR&page=1`, options)
.then(response => response.json())
.then(async response => {
console.log(response)
let postImg = `${imgUrl}${response.poster_path}`
let movieTitle = response.title;
let voteAverage = response.vote_average;
let overView = response.overview;
let runTime = `${Math.floor(response.runtime/60)}시간 ${response.runtime%60}분`;
let movieDate = `개봉일 : ${response.release_date}`;
let movieAge = await getMovieAge(movieId);
// let movieGenres = response.genres;
movieGenres(response.genres)
getMovieCredits(movieId);
document.querySelector(`.movieInfo > .moviePoster > img`).src = postImg
document.querySelector(`.movieInfo .movieContent .movieTitle`).textContent = movieTitle
document.querySelector(`.movieInfo .movieSubInfo .movieDate`).textContent = movieDate
document.querySelector(`.movieInfo .movieSubInfo .movieRuntime`).textContent = runTime
document.querySelector(`.movieInfo .movieSubInfo .movieAge`).textContent = movieAge
document.querySelector(`.movieInfo .movieContent .overView`).textContent = overView
document.querySelector(`.movieInfo .movieSubInfo .movieAge`).textContent = movieAge
console.log(movieAge)
// response.map((res) => {
//
// })
const tabList = document.querySelectorAll(`.tabList`)
const tabContent = document.querySelectorAll(`.tabContent`)
console.log(tabList)
tabList.forEach((tab,idx) => {
tab.addEventListener("click", () => {
tabContent.forEach((content) => {
content.classList.remove(`active`)
})
tabList.forEach((list) => {
list.classList.remove(`active`)
})
tabList[idx].classList.add(`active`)
tabContent[idx].classList.add(`active`)
})
})
})
.catch(err => console.error(err));
}
// const tabList = document.querySelectorAll(`.tabList`)
// const tabContent = document.querySelectorAll(`.tabContent`)
// console.log(tabList)