forked from alura-cursos/3519-nodejs-vidflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
79 lines (63 loc) · 2.57 KB
/
script.js
File metadata and controls
79 lines (63 loc) · 2.57 KB
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 containerVideos = document.querySelector(".videos__container");
async function buscarEMostrarVideos(){
try{
const busca = await fetch("http://localhost:3000/videos");
const videos = await busca.json();
videos.forEach((video)=> {
if(video.categoria == ""){
throw new Error('Vídeo não tem categoria');
}
containerVideos.innerHTML += `
<li class="videos__item">
<iframe src="${video.url}" title="${video.titulo}" frameborder="0" allowfullscreen></iframe>
<div class="descricao-video">
<img class="img-canal" src="${video.imagem} alt="Logo do Canal">
<h3 class="titulo-video">${video.titulo}</h3>
<p class="titulo-canal">${video.descricao}</p>
<p class="categoria" hidden>${video.categoria}</p>
</div>
</li>
`;
})
} catch(error){
containerVideos.innerHTML = `<p> Houve um erro ao carregar os vídeos: ${error}</p>`
}
}
buscarEMostrarVideos();
const barraDePesquisa = document.querySelector(".pesquisar__input");
barraDePesquisa.addEventListener("input", filtrarPesquisa);
function filtrarPesquisa(){
const videos = document.querySelectorAll(".videos__item");
if(barraDePesquisa.value != ""){
for(let video of videos){
let titulo = video.querySelector(".titulo-video").textContent.toLowerCase();
let valorFiltro = barraDePesquisa.value.toLowerCase();
if(!titulo.includes(valorFiltro)){
video.style.display = "none";
} else {
video.style.display = "block";
}
}
} else {
for (let video of videos) {
video.style.display = "block";
}
}
}
const botaoCategoria = document.querySelectorAll(".superior__item");
botaoCategoria.forEach((botao) => {
let nomeCategoria = botao.getAttribute("name");
botao.addEventListener("click", () => filtrarPorCategoria(nomeCategoria));
})
function filtrarPorCategoria(filtro){
const videos = document.querySelectorAll(".videos__item");
for(let video of videos){
let categoria = video.querySelector(".categoria").textContent.toLowerCase();
let valorFiltro = filtro.toLowerCase();
if(!categoria.includes(valorFiltro) && valorFiltro != 'tudo'){
video.style.display = "none";
} else {
video.style.display = "block";
}
}
}