worker-time-control/js/index.js

71 lines
2.6 KiB
JavaScript
Raw Permalink Normal View History

2023-11-30 08:25:32 +00:00
let pin = "";
if ("addEventListener" in document) {
document.addEventListener("DOMContentLoaded", () => {
FastClick.attach(document.body);
2021-10-19 08:52:45 +00:00
const heartEl = document.querySelector("body > h1 > span");
const txtPin = document.querySelector("#txtPin");
refreshDeviceLabel();
2021-10-19 08:52:45 +00:00
heartEl.addEventListener("click", () => {
Swal.fire({
title: "Iniciar sesión",
html: `
<input id="device" class="swal2-input" type="text" placeholder="Dispositivo" value="${localStorage.getItem("device") ?? ""}">
<input id="user" class="swal2-input" type="text" placeholder="Usuario" value="${localStorage.getItem("user") ?? ""}">
<input id="pass" class="swal2-input" type="password" placeholder="Contraseña">`,
confirmButtonText: "Login",
showCloseButton: true,
showCancelButton: false,
}).then(async (result) => {
if (result.isConfirmed)
signIn(document.querySelector("#user").value, document.querySelector("#pass").value, document.querySelector("#device").value);
});
});
2022-12-12 09:21:02 +00:00
document.querySelectorAll(".btnnum").forEach((btn) => {
btn.addEventListener("click", (e) => {
pin += +e.currentTarget.children[0].innerHTML;
txtPin.textContent = pin;
});
});
2021-10-19 08:52:45 +00:00
document.querySelector(".btnCancel").addEventListener("click", () => {
pin = "";
txtPin.textContent = "ID USUARIO";
});
2021-10-19 08:52:45 +00:00
document.querySelector(".btnOk").addEventListener("click", () => pin && login());
2024-01-30 13:44:44 +00:00
});
2021-10-19 08:52:45 +00:00
}
async function login() {
try {
const data = await call("WorkerTimeControls/login", {
method: "POST",
body: { pin },
});
localStorage.setItem("userData", JSON.stringify(data));
window.location = "clockIn.html";
} catch (e) {
document.querySelector("#txtPin").textContent = "ID USUARIO";
pin = "";
}
2023-12-05 14:56:51 +00:00
}
async function signIn(user, password, device) {
const data = await call("vnUsers/sign-in", {
method: "POST",
body: { user, password },
});
localStorage.setItem("token", data.token);
localStorage.setItem("ttl", data.ttl);
localStorage.setItem("user", user);
localStorage.setItem("device", device);
localStorage.setItem("created", Date.now());
getTokenConfig();
refreshDeviceLabel();
2024-01-30 13:44:44 +00:00
}
const refreshDeviceLabel = () => (document.querySelector("#deviceLabel").textContent = localStorage.getItem("device") ?? "");