-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
89 lines (82 loc) · 3.01 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
const searchButton = document.querySelector(".search-button");
searchButton.addEventListener("click", async function () {
try {
const inputKeyword = document.querySelector(".input-keyword");
const movies = await getMovies(inputKeyword.value);
updateUI(movies);
} catch (error) {
// console.log(error);
alert(error);
}
});
function getMovies(keyword) {
return fetch("http://www.omdbapi.com/?apikey=c9d8da0a&s=" + keyword)
.then((response) => {
// console.log(response);
if (response.ok === "False") {
throw new Error(response.statusText);
}
return response.json();
})
.then((response) => {
// console.log(response);
if (response.Response === "False") {
throw new Error(response.Error);
}
return response.Search;
});
}
function updateUI(movies) {
let cards = "";
movies.forEach((m) => (cards += showCards(m)));
const movieContainer = document.querySelector(".movie-container");
movieContainer.innerHTML = cards;
}
// event binding
document.addEventListener("click", async function (e) {
if (e.target.classList.contains("modal-detail-button")) {
const imdbid = e.target.dataset.imdbid;
const movieDetail = await getMovieDetail(imdbid);
updateUIDetail(movieDetail);
}
});
function getMovieDetail(imdbid) {
return fetch("http://www.omdbapi.com/?apikey=c9d8da0a&i=" + imdbid)
.then((response) => response.json())
.then((response) => response);
}
function updateUIDetail(m) {
const movieDetail = showMovieDetail(m);
const modalBody = document.querySelector(".modal-body");
modalBody.innerHTML = movieDetail;
}
function showCards(m) {
return `<div class="col-md-3 my-5">
<div class="card">
<img src="${m.Poster}" class="card-img-top" alt="" />
<div class="card-body">
<h5 class="card-title">${m.Title}</h5>
<h6 class="card-subtitle mb-2 text-muted">${m.Year}</h6>
<a href="#" class="btn btn-primary modal-detail-button" data-bs-toggle="modal" data-bs-target="#movieDetailModal" data-imdbid="${m.imdbID}">More</a>
</div>
</div>
</div>`;
}
function showMovieDetail(m) {
return `<div class="container-fluid">
<div class="row">
<div class="col-md-3">
<img src="${m.Poster}" class="img-fluid" />
</div>
<div class="col-md">
<ul class="list-group">
<li class="list-group-item"><h4>${m.Title} ${m.Year}</h4></li>
<li class="list-group-item"><strong>Director : </strong>${m.Director}</li>
<li class="list-group-item"><strong>Actors : </strong>${m.Actors}</li>
<li class="list-group-item"><strong>Genre : </strong>${m.Genre}</li>
<li class="list-group-item"><strong>Plot : </strong><br />${m.Plot}</li>
</ul>
</div>
</div>
</div>`;
}