-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
180 lines (157 loc) · 5.07 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
//Variables Storage
const welcome = "bienvenida";
let saludoStorage = localStorage.getItem(welcome);
let nameStorage = localStorage.getItem("nombreUsuario");
let lastNameStorage = localStorage.getItem("apellidoUsuario");
let emailStorage = localStorage.getItem("emailUsuario");
let passwordStorage = localStorage.getItem("passwordUsuario");
let anioNacimientoStorage = localStorage.getItem("anioNacimientoUsuario");
const contFormulario = document.getElementById("contFormulario");
const anioNacimiento = document.getElementById("anioNacimiento");
const YEAR = 2022;
let edad = YEAR - anioNacimiento;
let esMayorDeEdad = edad >= 18;
//Variables DOM
const greeting = document.getElementById("greeting");
const reset = document.getElementById("reset");
const borrar = document.getElementById("borrar");
const form = document.getElementById("form");
const contForm = document.getElementById("contForm");
const modal_container = document.getElementById("modal_container");
const close = document.getElementById("close");
const send = document.getElementById("send");
const open = document.getElementById("open");
const searchform = document.getElementById("search-form");
const searchbox = document.getElementById("searchbox");
const otra = document.getElementById("otra");
const nombre = document.getElementById("nombre");
const apellido = document.getElementById("apellido");
const email = document.getElementById("email");
const password = document.getElementById("password");
form.addEventListener("submit", (e) => {
e.preventDefault();
if (
nombre.value !== "" &&
apellido.value !== "" &&
email.value !== "" &&
password.value !== "" &&
anioNacimiento.value !== ""
) {
localStorage.setItem("nombreUsuario", form.children[0].value);
localStorage.setItem("apellidoUsuario", form.children[1].value);
localStorage.setItem("emailUsuario", form.children[2].value);
localStorage.setItem("passwordUsuario", form.children[3].value);
localStorage.setItem("anioNacimientoUsuario", form.children[4].value);
nameStorage = form.children[0].value;
lastNameStorage = form.children[1].value;
const Toast = Swal.mixin({
toast: true,
position: "top-end",
showConfirmButton: false,
timer: 1500,
timerProgressBar: true,
didOpen: (toast) => {
toast.addEventListener("mouseenter", Swal.stopTimer);
toast.addEventListener("mouseleave", Swal.resumeTimer);
},
});
Toast.fire({
icon: "success",
title: "Registrado con Éxito",
});
} else {
Swal.fire({
icon: "error",
title: "Oops...",
text: "Algun campo se encuentra vacio.",
});
}
checkForm();
});
const checkForm = () => {
if (nameStorage && nameStorage !== "null") {
const element = document.createElement("div");
contFormulario.remove();
element.innerHTML = `<p class="Color">Hola ${nameStorage} ${lastNameStorage} te estabamos esperando</p>`;
greeting.prepend(element);
eliminarDatos();
borrar.remove();
} else {
searchbox.addEventListener("click", () => {
Swal.fire({
icon: "error",
title: "Oops...",
text: "Debes loguearte primero",
});
});
}
};
function eliminarDatos() {
const otherElement = document.createElement("div");
otherElement.innerHTML = `
<div class="logginn">
<button class="login" id="resetear">Resetear Datos</button>
</div>`;
reset.prepend(otherElement);
otherElement.onclick = () => {
localStorage.removeItem("nombreUsuario");
localStorage.removeItem("apellidoUsuario");
localStorage.removeItem("anioNacimientoUsuario");
localStorage.removeItem("emailUsuario");
localStorage.removeItem("passwordUsuario");
location.reload();
};
}
checkForm();
open.addEventListener("click", () => {
modal_container.classList.add("show");
});
close.addEventListener("click", () => {
modal_container.classList.remove("show");
});
const card = document.getElementById("card");
const city = document.getElementById("city");
const date = document.getElementById("date");
const tempImg = document.getElementById("temp-img");
const temp = document.getElementById("temp");
const weather = document.getElementById("weather");
const range = document.getElementById("range");
const rangee = document.getElementById("rangee");
function Images(data) {
const temp = toCelsius(data.main.temp);
let src = "images/termometro.png";
if (temp > 28) {
src = "images/caliente.png";
} else if (temp < 20) {
src = "images/frio.png";
}
tempImg.src = src;
}
async function search(query) {
try {
const response = await fetch(
`${api.url}?q=${query}&appid=${api.key}&lang=es`
);
const data = await response.json();
card.style.display = "block";
city.innerHTML = `${data.name}, ${data.sys.country}`;
data.innerHTML = new Date().toLocaleDateString();
temp.innerHTML = `${toCelsius(data.main.temp)}°C`;
weather.innerHTML = data.weather[0].description;
Images(data);
} catch (err) {
Swal.fire({
icon: "error",
title: "Oops...",
text: "Ingresa otro lugar",
});
}
}
function toCelsius(kelvin) {
return Math.round(kelvin - 273.15);
}
function onSubmit(event) {
event.preventDefault();
search(searchbox.value);
}
searchform.addEventListener("submit", onSubmit, true);