worker-time-control/js/main.js

125 lines
3.9 KiB
JavaScript
Raw Permalink Normal View History

const renewPeriod = localStorage.getItem("renewPeriod");
2023-12-05 14:56:51 +00:00
let intervalId, isCheckingToken;
const txtConfirm = document.querySelector(".txtConfirm");
const confirmBtn = document.querySelector(".confirm");
2023-11-29 08:26:04 +00:00
2021-10-20 13:13:30 +00:00
function confirmReset() {
confirmBtn.classList.remove("confirmKO", "fade-in", "fade-out");
confirmBtn.style.hidden = true;
txtConfirm.textContent = "";
2021-10-20 13:13:30 +00:00
}
2021-10-19 08:52:45 +00:00
function printError(msg) {
2021-10-20 13:13:30 +00:00
confirmReset();
const txtConfirm = document.querySelector(".txtConfirm");
txtConfirm.textContent = msg;
confirmBtn.classList.add("confirmKO");
confirmBtn.style.hidden = false;
confirmBtn.classList.add("fade-in");
2024-06-19 07:52:07 +00:00
setTimeout(() => {
confirmBtn.classList.add("fade-out");
setTimeout(confirmReset, 200);
}, 2300);
2021-10-19 08:52:45 +00:00
}
async function renewToken() {
2024-06-19 07:52:07 +00:00
const data = await call("vnUsers/renewToken", { method: "POST" });
2023-11-29 08:26:04 +00:00
localStorage.setItem("ttl", data.ttl);
localStorage.setItem("created", Date.now());
2023-12-05 14:56:51 +00:00
}
async function getTokenConfig() {
const data = await call("AccessTokenConfigs/findOne", {
params: { filter: { fields: ["renewInterval", "renewPeriod"] } },
});
if (!data) return;
localStorage.setItem("renewPeriod", data.renewPeriod);
clearInterval(intervalId);
intervalId = setInterval(() => {
const created = +localStorage.getItem("created");
const ttl = localStorage.getItem("ttl");
if (isCheckingToken || !created) return;
isCheckingToken = true;
const renewPeriodInSeconds = Math.min(ttl, renewPeriod) * 1000;
const maxDate = created + renewPeriodInSeconds;
2023-12-05 14:56:51 +00:00
if (new Date().getTime() <= maxDate) return (isCheckingToken = false);
2022-12-12 09:21:02 +00:00
renewToken();
isCheckingToken = false;
}, data.renewInterval * 1000);
}
async function call(url, { method = "GET", body = {}, params = {} }) {
const controller = new AbortController();
const { signal } = controller;
const timeoutId = setTimeout(() => controller.abort(), 2000);
const opts = {
method,
2021-10-19 08:52:45 +00:00
headers: {
Authorization: localStorage.getItem("token"),
"Content-Type": "application/json; charset=utf-8",
2021-10-19 08:52:45 +00:00
},
signal,
};
if (method === "GET") {
const searchParams = new URLSearchParams();
for (let [key, value] of Object.entries(params)) {
searchParams.append(key, typeof value === "object" ? JSON.stringify(value) : value);
}
url += "?" + searchParams.toString();
} else if (method === "POST") opts.body = JSON.stringify(body);
try {
const res = await fetch(`/api/${url}`, opts);
const data = await res.json();
if (res.ok) return data;
clearTimeout(timeoutId);
throw data.error;
} catch (e) {
let mensaje = "Ha ocurrido un error, consulta con informática";
switch (e.name) {
case "SyntaxError":
mensaje = "Requested JSON parse failed";
break;
case "TimeoutError":
mensaje = "Time out error";
break;
case "AbortError":
mensaje = "Ajax request aborted";
break;
case "UserError":
mensaje = e.message;
break;
default:
switch (e.statusCode) {
case 0:
mensaje = "Not connect: Verify Network";
break;
case 504:
mensaje = "No se ha podido conectar con Salix, consulta con informática";
2024-01-31 08:04:49 +00:00
break;
case 555:
mensaje = "Error 555";
break;
default:
if (e.statusCode >= 400 && e.statusCode < 500) mensaje = e.message;
}
2021-10-19 08:52:45 +00:00
}
printError(mensaje);
throw e;
}
}
2023-11-29 12:38:53 +00:00
if (renewPeriod) getTokenConfig();