-
-
Notifications
You must be signed in to change notification settings - Fork 524
/
script.js
57 lines (51 loc) · 1.67 KB
/
script.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
const KEY = "3fd2be6f0c70a2a598f084ddfb75487c";
// For educational purposes only - DO NOT USE in production
// Request your own key for free: https://developers.themoviedb.org/3
const API_URL = `https://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=${KEY}&page=1`;
const IMG_PATH = "https://image.tmdb.org/t/p/w1280";
const SEARCH_API = `https://api.themoviedb.org/3/search/movie?api_key=${KEY}&query=`;
const main = document.getElementById("main");
const form = document.getElementById("form");
const search = document.getElementById("search");
const getClassByRate = (vote) => {
if (vote >= 7.5) return "green";
else if (vote >= 7) return "orange";
else return "red";
};
const showMovies = (movies) => {
main.innerHTML = "";
movies.forEach((movie) => {
const { title, poster_path, vote_average, overview } = movie;
const movieElement = document.createElement("div");
movieElement.classList.add("movie");
movieElement.innerHTML = `
<img
src="${IMG_PATH + poster_path}"
alt="${title}"
/>
<div class="movie-info">
<h3>${title}</h3>
<span class="${getClassByRate(vote_average)}">${vote_average}</span>
</div>
<div class="overview">
<h3>Overview</h3>
${overview}
</div>
`;
main.appendChild(movieElement);
});
};
const getMovies = async (url) => {
const res = await fetch(url);
const data = await res.json();
showMovies(data.results);
};
getMovies(API_URL);
form.addEventListener("submit", (e) => {
e.preventDefault();
const searchTerm = search.value;
if (searchTerm && searchTerm !== "") {
getMovies(SEARCH_API + searchTerm);
search.value = "";
} else history.go(0);
});