forked from Ayushparikh-code/Web-dev-mini-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
109 lines (101 loc) · 3.02 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
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
const API_KEY = "api_key=1cf50e6248dc270629e802686245c2c8";
const BASE_URL = "https://api.themoviedb.org/3";
const API_URL = BASE_URL + "/discover/movie?sort_by=popularity.desc&" + API_KEY;
const IMG_URL = "https://image.tmdb.org/t/p/w500";
const searchURL = BASE_URL + "/search/movie?" + API_KEY;
const recommendationsHeader = document.getElementById("recommendations-header");
let movieId = "";
let recommendationsURL = "";
const main = document.getElementById("main");
const recommendations = document.getElementById("recommendations");
const form = document.getElementById("form");
const search = document.getElementById("search");
getMovies(API_URL);
function getMovies(url) {
fetch(url)
.then((res) => res.json())
.then((data) => {
showMovies(data.results);
});
}
function getRecommendations(url) {
fetch(url)
.then((res) => res.json())
.then((data) => {
movieId = data.results[0].id;
recommendationsURL =
BASE_URL +
`/movie/${movieId}/recommendations?` +
API_KEY +
"&language=en-US&page=1";
fetch(recommendationsURL)
.then((res) => res.json())
.then((data) => {
showRecommendations(data.results);
});
});
}
function showMovies(data) {
main.innerHTML = "";
data.forEach((movie) => {
const { title, poster_path, vote_average, overview } = movie;
const movieEl = document.createElement("div");
movieEl.classList.add("movie");
movieEl.innerHTML = `
<img src="${IMG_URL + poster_path}" alt="${title}">
<div class="movie-info">
<h3>${title}</h3>
<span class="${getColor(vote_average)}">${
Math.round(vote_average * 10) / 10
}</span>
</div>
<div class="overview">
<h3>Overview</h3>
${overview}
</div>
`;
main.appendChild(movieEl);
});
}
function showRecommendations(data) {
recommendations.innerHTML = "";
data.forEach((movie) => {
const { title, poster_path, vote_average, overview } = movie;
const movieEl = document.createElement("div");
movieEl.classList.add("movie");
movieEl.innerHTML = `
<img src="${IMG_URL + poster_path}" alt="${title}">
<div class="movie-info">
<h3>${title}</h3>
<span class="${getColor(vote_average)}">${
Math.round(vote_average * 10) / 10
}</span>
</div>
<div class="overview">
<h3>Overview</h3>
${overview}
</div>
`;
recommendations.appendChild(movieEl);
});
}
function getColor(vote) {
if (vote >= 8) {
return "green";
} else if (vote >= 5) {
return "orange";
} else {
return "red";
}
}
form.addEventListener("submit", (e) => {
e.preventDefault();
const searchTerm = search.value;
if (searchTerm) {
getMovies(searchURL + "&query=" + searchTerm);
getRecommendations(searchURL + "&query=" + searchTerm);
recommendationsHeader.hidden = false;
} else {
getMovies(API_URL);
}
});