-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
182 lines (156 loc) · 5.36 KB
/
index.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
"use strict";
let data;
const botoncito = document.getElementById("boton");
const select = document.getElementById("seleccionar");
const random = document.getElementById("aleatorio");
const copy = document.getElementById("copiar");
const contenedor = document.getElementById("app");
botoncito.addEventListener("click", buscarPersonaje);
random.addEventListener("click", Randomizar);
function setContainerStyles() {
const app = document.getElementById("app");
app.className = "resultados";
}
var content;
function crearTarjeta(quote) {
const tarjeta = document.createElement("div");
tarjeta.className = "tarjeta";
// agrega la frase
const fraseHTML = document.createElement("p");
const frase = quote.quote;
fraseHTML.className = "frasesita";
fraseHTML.textContent = frase;
// agrega la imagen
const imagenHTML = document.createElement("img");
const imagenURL = quote.imagen;
imagenHTML.className = "imagenes";
imagenHTML.src = imagenURL;
//agrega un salto de linea
const br = document.createElement("br");
const button = document.createElement("button");
button.innerHTML = "compartir";
button.className = "copiar";
button.value = `${quote.id}`;
button.onclick = () =>
navigator.clipboard
.writeText(`https://frases-los-simpson-latino.vercel.app/?id=${quote.id}`)
.then(() => console.log("copiado al portapapeles"))
.catch((error) => console.log(error));
// agrega el video
const videoContainer = document.createElement("iframe");
videoContainer.src = `https://www.youtube.com/embed/${quote.video}`;
videoContainer.className = "videos";
videoContainer.title = "YouTube video player";
videoContainer.frameBorder = "0";
videoContainer.allow =
"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture";
videoContainer.allowfullscreen = true;
//agrega la linea
const linea = document.createElement("hr");
// agrega todo del quote al contenedor
tarjeta.append(fraseHTML);
tarjeta.append(imagenHTML);
tarjeta.append(br);
tarjeta.append(videoContainer);
tarjeta.append(button);
tarjeta.append(linea);
return tarjeta;
}
function getData(personaje, valor1, valor2) {
contenedor.innerHTML = "<div><p>CARGANDO...</p></div>";
return fetch(
`https://frases-simpsons-latino.herokuapp.com/${valor1}?${valor2}=${personaje}`
)
.then((respuesta) => respuesta.json())
.then((res) => {
const contenido = document.createElement("div");
contenido.className = "contenido";
if (res && res.length > 0) {
console.log({ res });
// agrega el nombre del personaje
const personajeHTML = document.createElement("p");
const personaje = res[0].character;
personajeHTML.className = "autor";
personajeHTML.textContent = personaje;
contenido.prepend(personajeHTML);
res.map((quote) => {
const tarjeta = crearTarjeta(quote);
contenido.append(tarjeta);
contenedor.innerHTML = "<div></div>";
contenedor.append(contenido);
content = `https://frases-simpsons.herokuapp.com/${valor1}?${valor2}=${personaje}`;
});
} else {
contenedor.innerHTML =
"<div class='mensaje-resultado'>No encontramos resultados para tu búsqueda, pero puedes probar una diferente!</div><br/><div class='mensaje-resultado'>¿Que tal si buscas Homero Simpson?</div>";
}
})
.then(() => {
setContainerStyles();
});
}
function between(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function getRandom() {
contenedor.innerHTML = "<div><p>CARGANDO...</p></div>";
return fetch(`https://frases-simpsons-latino.herokuapp.com/quote?quote=`)
.then((respuesta) => respuesta.json())
.then((res) => {
const randomNumber = between(0, res.length - 1);
const contenido = document.createElement("div");
contenido.className = "contenido";
data = [res[randomNumber]];
data.map((quote) => {
const tarjeta = crearTarjeta(quote);
contenido.append(tarjeta);
contenedor.innerHTML = "<div></div>";
const nombre = document.createElement("p");
nombre.className = "autor";
nombre.innerHTML = quote.character;
contenedor.append(nombre);
contenedor.append(contenido);
});
})
.then(() => {
setContainerStyles();
});
}
var valor1 = "character";
var valor2 = "name";
function buscarPersonaje(event) {
event.preventDefault();
console.log("BUSCANDO");
const buscar = document.getElementById("texto");
if (select.value === "Character") {
console.log("personaje");
var valor1 = "character";
var valor2 = "name";
} else if (select.value === "Quote") {
console.log("frase");
var valor1 = "quote";
var valor2 = "quote";
}
return getData(buscar.value, valor1, valor2);
}
function Randomizar() {
return getRandom();
}
document.addEventListener("DOMContentLoaded", function () {
const queryparams = window.location.search;
const params = new URLSearchParams(queryparams);
const personaje = params.get("character");
const frase = params.get("quote");
const id = params.get("id");
if (personaje) {
getData(personaje, "character", "name");
}
if (frase) {
getData(frase, "quote", "quote");
}
if (id) {
getData(id, "id", "id");
}
console.log("pagina cargo", { personaje });
});
console.log("carga");