-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
280 lines (246 loc) · 8.77 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
const body = document.querySelector("body");
const movies = document.querySelector(".movies");
const proximo = document.querySelector(".btn-next");
const anterior = document.querySelector(".btn-prev");
const input = document.querySelector(".input");
const destaque = document.querySelector(".highlight__video");
destaque.style.backgroundPosition = "center";
destaque.style.backgroundSize = "cover";
const destaqueTitulo = document.querySelector(".highlight__title");
const destaqueRating = document.querySelector(".highlight__rating");
const destaqueGenero = document.querySelector(".highlight__genres");
let destaqueGeneroArray = [];
const destaqueData = document.querySelector(".highlight__launch");
const destaqueDescricao = document.querySelector(".highlight__description");
const destaqueLink = document.querySelector(".highlight__video-link");
const meses = [
"Janeiro",
"Fevereiro",
"Março",
"Abril",
"Maio",
"Junho",
"Julho",
"Agosto",
"Outubro",
"Novembro",
"Dezembro",
];
const modal = document.querySelector(".modal");
const modalTitulo = document.querySelector(".modal__title");
const modalImagem = document.querySelector(".modal__img");
const modalDescricao = document.querySelector(".modal__description");
const modalRating = document.querySelector(".modal__average");
const modalGeneros = document.querySelector(".modal__genres");
const modalClose = document.querySelector(".modal__close");
const botaoTema = document.querySelector(".btn-theme");
const tema = localStorage.getItem("tema");
const info = document.querySelector(".info");
const modalInt = document.querySelector(".modal-interrogation");
const mensagemBusca = document.querySelector(".busca-zero");
const containerFilmes = document.querySelector(".movies-container");
let movieNode;
let filmeId = "";
let mesTratado = "";
const paginasIniciais = [];
let paginasDeBusca = [];
let paginasDeBuscaTratada;
let indice = 0;
info.addEventListener("click", function () {
modalInt.classList.toggle("hidden");
});
function temaClaro() {
localStorage.setItem("tema", "claro");
body.style.setProperty("--background-color", "#fff");
body.style.setProperty("--color", "#242424");
body.style.setProperty("--highlight-background", "#fff");
body.style.setProperty("--highlight-description", "#242424");
body.style.setProperty("--shadow-color", "0px 4px 8px rgba(0, 0, 0, 0.15)");
body.style.setProperty("--input-border-color", "#979797");
botaoTema.src = "./assets/light-mode.svg";
anterior.src = "./assets/seta-esquerda-preta.svg";
proximo.src = "./assets/seta-direita-preta.svg";
}
function temaEscuro() {
localStorage.setItem("tema", "escuro");
body.style.setProperty("--background-color", "#242424");
body.style.setProperty("--color", "#fff");
body.style.setProperty("--highlight-background", "#454545");
body.style.setProperty("--highlight-description", "#fff");
body.style.setProperty(
"--shadow-color",
"0px 4px 8px rgba(256, 256, 256, 0.15)"
);
body.style.setProperty("--input-border-color", "#fff");
botaoTema.src = "./assets/dark-mode.svg";
anterior.src = "./assets/seta-esquerda-branca.svg";
proximo.src = "./assets/seta-direita-branca.svg";
}
tema === "claro" ? temaClaro() : temaEscuro();
botaoTema.addEventListener("click", function (event) {
localStorage.getItem("tema") === "claro" ? temaEscuro() : temaClaro();
});
function geradorMovies(item, index) {
const movie = document.createElement("div");
movie.classList.add("movie");
const movieInfo = document.createElement("div");
movieInfo.classList.add("movie__info");
const title = document.createElement("span");
title.classList.add("movie__title");
const voteAverage = document.createElement("span");
voteAverage.classList.add("movie__rating");
const estrelaClassificacao = document.createElement("img");
movie.style.backgroundImage = `url(${item.poster_path})`;
movie.dataset.id = item.id;
title.textContent = item.title;
voteAverage.textContent = item.vote_average;
estrelaClassificacao.src = "./assets/estrela.svg";
voteAverage.append(estrelaClassificacao);
movieInfo.append(title, voteAverage);
movie.append(movieInfo);
movies.append(movie);
}
function geradorPaginas(array, arrayCorpo) {
const arrayFiltrado = arrayCorpo.results.filter(
(dados) =>
dados.adult === false &&
dados.title &&
dados.poster_path &&
dados.vote_average > 0
);
array.push(arrayFiltrado.slice(0, 5));
array.push(arrayFiltrado.slice(5, 10));
array.push(arrayFiltrado.slice(10, 15));
array.push(arrayFiltrado.slice(15, 20));
paginasDeBuscaTratada = array.filter((pagina) => pagina.length > 0);
if (array[0].length <= 0) {
containerFilmes.classList.add("hidden");
mensagemBusca.classList.remove("hidden");
}
if (array[0].length > 0) {
containerFilmes.classList.remove("hidden");
mensagemBusca.classList.add("hidden");
}
}
function esconderFilmes() {
const filmes = document.querySelectorAll(".movie");
filmes.forEach((filme) => {
filme.classList.add("hidden");
});
}
function exibirModal() {
movieNode = document.querySelectorAll(".movie");
movieNode.forEach((movie) => {
movie.addEventListener("click", async function () {
modalGeneros.innerHTML = "";
filmeId = movie.dataset.id;
const resTratada5 = await (
await fetch(
"https://tmdb-proxy.cubos-academy.workers.dev/3/movie/" + filmeId
)
).json();
modal.classList.remove("hidden");
modalTitulo.textContent = resTratada5.title;
modalImagem.src = resTratada5.backdrop_path
? resTratada5.backdrop_path
: "";
modalDescricao.textContent = resTratada5.overview
? resTratada5.overview
: "";
modalRating.textContent = resTratada5.vote_average;
resTratada5.genres.forEach((genero) => {
const generos = document.createElement("span");
generos.classList.add("modal__genre");
generos.textContent = genero.name;
modalGeneros.append(generos);
});
modalClose.addEventListener("click", function () {
modal.classList.add("hidden");
});
});
});
}
function primeirosFilmes(array) {
esconderFilmes();
array.forEach((filme) => {
geradorMovies(filme);
});
exibirModal();
}
function paginacao(index, array) {
esconderFilmes();
array[index].forEach((filme, index2) => {
geradorMovies(filme);
});
exibirModal();
}
function sistemaDePaginacao(index, array) {
proximo.addEventListener("click", function () {
index = index < array.length - 1 ? ++index : (index = 0);
paginacao(index, array);
});
anterior.addEventListener("click", function () {
index > 0 ? --index : (index = array.length - 1);
paginacao(index, array);
});
}
async function requisicao() {
const resTratada = await (
await fetch(
"https://tmdb-proxy.cubos-academy.workers.dev/3/discover/movie?language=pt-BR"
)
).json();
geradorPaginas(paginasIniciais, resTratada);
primeirosFilmes(paginasIniciais[0]);
sistemaDePaginacao(indice, paginasIniciais);
input.addEventListener("keydown", async function (event) {
if (event.key !== "Enter") return;
if (event.target.value === "") {
containerFilmes.classList.remove("hidden");
mensagemBusca.classList.add("hidden");
indice = 0;
primeirosFilmes(paginasIniciais[0]);
sistemaDePaginacao(indice, paginasIniciais);
return;
}
const resTratada2 = await (
await fetch(
"https://tmdb-proxy.cubos-academy.workers.dev/3/search/movie?language=pt-BR&include_adult=false&query=" +
event.target.value
)
).json();
event.target.value = "";
paginasDeBusca = [];
geradorPaginas(paginasDeBusca, resTratada2);
primeirosFilmes(paginasDeBusca[0]);
sistemaDePaginacao(indice, paginasDeBuscaTratada);
});
const resTratada3 = await (
await fetch(
"https://tmdb-proxy.cubos-academy.workers.dev/3/movie/436969?language=pt-BR"
)
).json();
resTratada3.genres.forEach((genero) => {
destaqueGeneroArray.push(genero.name);
});
let generos = destaqueGeneroArray.join(", ");
const date = new Date(resTratada3.release_date);
const dia = date.getDate();
const mes = date.getMonth();
const ano = date.getFullYear();
mesTratado = meses[mes];
destaque.style.backgroundImage = `url(${resTratada3.backdrop_path})`;
destaqueTitulo.textContent = resTratada3.title;
destaqueRating.textContent = resTratada3.vote_average;
destaqueGenero.textContent = generos;
destaqueData.textContent = dia + " de " + mesTratado + " de " + ano;
destaqueDescricao.textContent = resTratada3.overview;
const resTratada4 = await (
await fetch(
"https://tmdb-proxy.cubos-academy.workers.dev/3/movie/436969/videos?language=pt-BR"
)
).json();
destaqueLink.href =
"https://www.youtube.com/watch?v=" + resTratada4.results[0].key;
}
requisicao();