-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
97 lines (89 loc) · 3.33 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
// Titles: https://omdbapi.com/?s=thor&page=1&apikey=fc1fef96
// details: http://www.omdbapi.com/?i=tt3896198&apikey=fc1fef96
const movieSearchBox = document.getElementById("movie-search-box");
const searchList = document.getElementById("search-list");
const resultGrid = document.getElementById("result-grid");
// load movies from API
async function loadMovies(searchTerm) {
const URL = `https://omdbapi.com/?s=${searchTerm}&page=1&apikey=eabe1591`;
const res = await fetch(`${URL}`);
const data = await res.json();
// console.log(data.Search);
if (data.Response == "True") displayMovieList(data.Search);
}
function findMovies() {
let searchTerm = movieSearchBox.value.trim();
if (searchTerm.length > 0) {
searchList.classList.remove("hide-search-list");
loadMovies(searchTerm);
} else {
searchList.classList.add("hide-search-list");
}
}
function displayMovieList(movies) {
searchList.innerHTML = "";
for (let idx = 0; idx < movies.length; idx++) {
let movieListItem = document.createElement("div");
movieListItem.dataset.id = movies[idx].imdbID; // setting movie id in data-id
movieListItem.classList.add("search-list-item");
if (movies[idx].Poster != "N/A") moviePoster = movies[idx].Poster;
else moviePoster = "image_not_found.png";
movieListItem.innerHTML = `
<div class = "search-item-thumbnail">
<img src = "${moviePoster}">
</div>
<div class = "search-item-info">
<h3>${movies[idx].Title}</h3>
<p>${movies[idx].Year}</p>
</div>
`;
searchList.appendChild(movieListItem);
}
loadMovieDetails();
}
function loadMovieDetails() {
const searchListMovies = searchList.querySelectorAll(".search-list-item");
searchListMovies.forEach((movie) => {
movie.addEventListener("click", async () => {
// console.log(movie.dataset.id);
searchList.classList.add("hide-search-list");
movieSearchBox.value = "";
const result = await fetch(
`http://www.omdbapi.com/?i=${movie.dataset.id}&apikey=fc1fef96`
);
const movieDetails = await result.json();
// console.log(movieDetails);
displayMovieDetails(movieDetails);
});
});
}
function displayMovieDetails(details) {
resultGrid.innerHTML = `
<div class = "movie-poster">
<img src = "${
details.Poster != "N/A" ? details.Poster : "image_not_found.png"
}" alt = "movie poster">
</div>
<div class = "movie-info">
<h3 class = "movie-title">${details.Title}</h3>
<ul class = "movie-misc-info">
<li class = "year">Year: ${details.Year}</li>
<li class = "rated">Ratings: ${details.Rated}</li>
<li class = "released">Released: ${details.Released}</li>
</ul>
<p class = "genre"><b>Genre:</b> ${details.Genre}</p>
<p class = "writer"><b>Writer:</b> ${details.Writer}</p>
<p class = "actors"><b>Actors: </b>${details.Actors}</p>
<p class = "plot"><b>Plot:</b> ${details.Plot}</p>
<p class = "language"><b>Language:</b> ${details.Language}</p>
<p class = "awards"><b><i class = "fas fa-award"></i></b> ${
details.Awards
}</p>
</div>
`;
}
window.addEventListener("click", (event) => {
if (event.target.className != "form-control") {
searchList.classList.add("hide-search-list");
}
});