-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
98 lines (65 loc) · 2.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
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
document.addEventListener("DOMContentLoaded", () => {
//Vars
const form = document.querySelector("form"),
input = document.querySelector("input"),
title = document.querySelector(".title"),
score = document.querySelector(".score"),
rated = document.querySelector(".rated"),
img = document.querySelector(".content-general img"),
release = document.querySelector(".release"),
runtime = document.querySelector(".runtime"),
plot = document.querySelector(".plot-content"),
cast = document.querySelector(".cast-content"),
loadingSvg = document.querySelector(".search-wrapper .icon");
key = "36b9f0e7";
// Get movie by default
getData("World war z", key)
.then((movieInfo) => {
setMovieOnPage(movieInfo)
});
// Get movie form user input
form.addEventListener("submit", e => {
e.preventDefault();
const movieName = input.value;
loadingSvg.classList.remove("hide");
getData(movieName, key)
.then((movieInfo) => {
setMovieOnPage(movieInfo)
document.querySelector(".content-wrapper").classList.remove("hide");
document.querySelector(".error").classList.add("hide");
})
.catch(() =>{
document.querySelector(".content-wrapper").classList.add("hide");
document.querySelector(".error").classList.remove("hide");
})
form.reset();
})
//Functions
async function getData (movie, key) {
return await fetch(`http://www.omdbapi.com/?t=${movie}&apikey=${key}`)
.then(data => data.json());
}
function setMovieOnPage ({Title, Year, Rated, Actors, Genre, Plot, Poster, Runtime, imdbRating}) {
loadingSvg.classList.add("hide");
title.textContent = Title;
score.textContent = imdbRating;
rated.textContent = Rated;
release.textContent = Year;
runtime.textContent = Runtime;
plot.textContent = Plot;
cast.textContent = Actors;
let arr = Genre.split(", ");
createGenreWrappers(arr);
img.src = `${Poster}`;
img.style.width = "200px";
}
function createGenreWrappers (arr) {
document.querySelector(".genres").innerHTML = "";
arr.forEach((item, i) => {
const genre = document.createElement("div");
genre.classList.add("genres-item");
genre.textContent = item;
document.querySelector(".genres").append(genre)
})
}
})