-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
65 lines (58 loc) · 2.15 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
const nomeBusca = document.querySelector(".input");//Acessando a classe(retorna o obj)
const mensagemErro = document.querySelector("#mensagemErro");
const botaoBuscar = document.querySelector("#botao_buscar");
const titulo = document.querySelector("#titulo");
const ano = document.querySelector("#ano");
const duracao = document.querySelector("#duracao");
const genero = document.querySelector("#genero");
const diretor = document.querySelector("#diretor");
const atores = document.querySelector("#atores");
const poster = document.querySelector(".poster");
const sinopse = document.querySelector("#sinopse");
const apiKey = "cc9360b9";
const imgDefault = "./default_image.png";
//async: Indica que a função possui código assíncrono.
//await: Indica um ponto a ser aguardado pelo código.
async function buscaFilme(nomeBusca){
const resposta = await fetch(`http://www.omdbapi.com/?t=${nomeBusca}&apikey=${apiKey}`);
return resposta.json();
}
botaoBuscar.addEventListener("click", () => {
limparCampos();
core();
});
function limparCampos() {
titulo.textContent = "";
sinopse.textContent = "";
ano.textContent = "";
duracao.textContent = "";
genero.textContent = "";
atores.textContent = "";
diretor.textContent = "";
poster.setAttribute("src", imgDefault);
}
async function core() {
try {
const filme = await buscaFilme(nomeBusca.value);
validaDados(filme);
defineValores(filme);
} catch (erro) {
console.log(erro);
mensagemErro.textContent = `${erro}`;
}
}
function defineValores(filme) {
titulo.textContent = filme.Title;
sinopse.textContent = filme.Plot;
ano.textContent = `Year: ${filme.Year}`;
duracao.textContent = `Run time: ${filme.Runtime}`;
genero.textContent = `Genre: ${filme.Genre}`;
atores.textContent = `Actors: ${filme.Actors}`;
diretor.textContent = `Director: ${filme.Director}`;
poster.setAttribute("src", filme.Poster);
}
function validaDados(filme) {
if(filme.Plot === undefined || filme.Year === undefined || filme.Actors === "N/A"){
throw new Error("Filme não foi encontrado!");
}
}