-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
100 lines (73 loc) · 2.47 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
const apiKey = "04c35731a5ee918f014970082a0088b1";
const defaultApiUrl = "https://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc";
const searchApiUrl = "https://api.themoviedb.org/3/search/movie";
const main = document.getElementById("main");
const search = document.getElementById("search");
const form = document.querySelector("form");
const imgPath = "https://image.tmdb.org/t/p/w1280";
// Function to fetch movies from API
async function getMovies(url) {
const resp = await fetch(url);
const respData = await resp.json();
// Clear previous search results
main.innerHTML = "";
// Display search results or popular movies
if (url.includes("search")) {
respData.results.forEach((movie) => {
const { poster_path, title, vote_average,overview } = movie;
const movieEl = document.createElement("div");
movieEl.classList.add("movie");
movieEl.innerHTML = `
<img src="${imgPath + poster_path}" alt="${title}">
<div class="movie-info">
<h3>${title}</h3>
<span class="${getClassByRate(vote_average)}">${vote_average}</span>
<div class="overview"><h3>OverView:</h3>
${overview}</div>
</div>
`;
main.appendChild(movieEl);
});
} else {
respData.results.forEach((movie) => {
const { poster_path, title, vote_average,overview } = movie;
const movieEl = document.createElement("div");
movieEl.classList.add("movie");
movieEl.innerHTML = `
<img src="${imgPath + poster_path}" alt="${title}">
<div class="movie-info">
<h3>${title}</h3>
<span class="${getClassByRate(vote_average)}">${vote_average}</span>
<div class="overview"><h2>OverView : </h2>
${overview}</div>
</div>
</div>
`;
main.appendChild(movieEl);
});
}
}
// Function to determine CSS class based on movie rating
function getClassByRate(vote) {
if (vote > 8) {
return "green";
} else if (vote >= 6) {
return "orange";
} else {
return "red";
}
}
// Event listener for form submission
form.addEventListener("submit", (e) => {
e.preventDefault();
const searchTerm = search.value.trim();
if (searchTerm) {
const url = `${searchApiUrl}?api_key=${apiKey}&query=${searchTerm}`;
getMovies(url);
search.value = "";
} else {
getMovies(defaultApiUrl + `&api_key=${apiKey}`);
}
});
// Load popular movies on page load
getMovies(defaultApiUrl + `&api_key=${apiKey}`);