worker-time-control/js/index.js

71 lines
2.6 KiB
JavaScript

let pin = "";
if ("addEventListener" in document) {
document.addEventListener("DOMContentLoaded", () => {
FastClick.attach(document.body);
const heartEl = document.querySelector("body > h1 > span");
const txtPin = document.querySelector("#txtPin");
refreshDeviceLabel();
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);
});
});
document.querySelectorAll(".btnnum").forEach((btn) => {
btn.addEventListener("click", (e) => {
pin += +e.currentTarget.children[0].innerHTML;
txtPin.textContent = pin;
});
});
document.querySelector(".btnCancel").addEventListener("click", () => {
pin = "";
txtPin.textContent = "ID USUARIO";
});
document.querySelector(".btnOk").addEventListener("click", () => pin && login());
});
}
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 = "";
}
}
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();
}
const refreshDeviceLabel = () => (document.querySelector("#deviceLabel").textContent = localStorage.getItem("device") ?? "");