worker-time-control/js/clockIn.js

143 lines
5.1 KiB
JavaScript
Raw Permalink Normal View History

2023-11-30 08:25:32 +00:00
let userData = "";
const footer = document.querySelector("footer");
const txtName = document.querySelector("#txtNombre");
const inBtn = document.querySelector(".in");
const inMiddleBtn = document.querySelector(".inMiddle");
const outMiddleBtn = document.querySelector(".outMiddle");
const outBtn = document.querySelector(".out");
const timetableList = document.querySelector(".listHorario");
const weekDays = ["Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado"];
document.addEventListener("DOMContentLoaded", () => {
userData = JSON.parse(localStorage.getItem("userData"));
2021-10-19 08:52:45 +00:00
setView();
setEvents();
});
function setEvents() {
document.querySelector(".btnSalir").addEventListener("click", close);
2021-10-19 08:52:45 +00:00
inBtn.addEventListener("click", () => clockIn("in"));
inMiddleBtn.addEventListener("click", () => clockIn("middle"));
outMiddleBtn.addEventListener("click", () => clockIn("middle"));
outBtn.addEventListener("click", () => clockIn("out"));
2021-10-19 08:52:45 +00:00
2024-06-19 09:40:30 +00:00
setTimeout(close, 5000);
}
2021-10-19 08:52:45 +00:00
async function clockIn(direction) {
let timeout = 1000;
const data = await call("WorkerTimeControls/clockIn", {
method: "POST",
body: {
workerFk: userData["userFk"],
direction,
device: localStorage.getItem("device"),
},
2021-10-19 08:52:45 +00:00
});
if (data.error) {
let msg = "";
for (const val of data) if (val.error) msg += `${val.error}\n`;
printError(msg);
} else {
document.querySelector(".confirm").classList.add("fade-in");
txtConfirm.textContent = "Fichada registrada correctamente";
timeout = 2000;
}
setTimeout(close, timeout);
2021-10-19 08:52:45 +00:00
}
function setView() {
footer.style.hidden = true;
inBtn.style.display = "none";
inMiddleBtn.style.display = "none";
outMiddleBtn.style.display = "none";
outBtn.style.display = "none";
2021-10-19 08:52:45 +00:00
txtName.textContent = `${userData.name} ${userData.surname}`;
getInfo();
if (userData.button1 === null && userData.button2 === null) return printError("Contacta con tu responsable");
2021-10-19 08:52:45 +00:00
document.querySelector(`.${userData.button1}`).style.display = "inline-block";
if (userData.button2) document.querySelector(`.${userData.button2}`).style.display = "inline-block";
2021-10-19 08:52:45 +00:00
}
async function getInfo() {
const data = await call("WorkerTimeControls/getClockIn", { params: { workerFk: userData["userFk"] } });
footer.style.hidden = false;
printTimetable(data);
2021-10-19 08:52:45 +00:00
}
function printTimetable(timetable) {
const dated = new Date();
dated.setDate(dated.getDate() - 6);
const table = document.createDocumentFragment();
const totalEl = document.querySelector(".total");
let totalHr = 0;
for (let day = 0; day < weekDays.length - 1; day++) {
const label = createElement("label", { text: weekDays[dated.getDay()] });
table.append(label);
dated.setDate(dated.getDate() + 1);
}
2021-10-19 08:52:45 +00:00
table.append(createElement("label", { text: "HOY", classes: ["hoy"] }), createElement("li", { classes: ["hrTop"] }));
const liContent = createElement("li", {});
const liTotals = createElement("li", {});
timetable.forEach((row, index) => {
for (let day = weekDays.length - 1; day >= 0; day--) {
2024-06-19 11:18:43 +00:00
const img = createElement("img", { attrs: { src: isEmpty(row[`${day}daysAgoDirection`], "image") } });
const p = createElement("p", { text: isEmpty(row[`${day}daysAgo`], "text") });
2021-10-19 08:52:45 +00:00
const innerDiv = createElement("div", { childs: [img, p] });
2024-06-19 11:18:43 +00:00
const outerDiv = createElement("div", { classes: ["time", isEmpty(row[`${day}daysAgo`])], childs: [innerDiv] });
liContent.append(outerDiv);
if (index === 0) {
const div = createElement("div", { classes: ["time"], text: secondsToHm(timetable[0][`${day}daysAgoTotal`]) });
liTotals.append(div);
totalHr += timetable[0][`${day}daysAgoTotal`] || 0;
}
2021-10-19 08:52:45 +00:00
}
table.append(liContent);
if (index === timetable.length - 1) table.append(createElement("hr", {}), liTotals);
});
2021-10-19 08:52:45 +00:00
totalEl.innerText = `Total: ${totalHr ? secondsToHm(totalHr) : 0} h`;
timetableList.append(table);
2021-10-19 08:52:45 +00:00
}
2024-06-19 11:18:43 +00:00
const isEmpty = (value, type) => {
const val = value?.toString()?.trim();
if (!type) return val ? "show" : "hide";
if (type === "image") return val ? `img/${val}.svg` : "img/in.svg";
if (type === "text") return val ? val : "00:00";
};
2024-06-19 10:43:02 +00:00
function close() {
2021-10-19 08:52:45 +00:00
localStorage.removeItem("userData");
setTimeout(() => (window.location = "index.html"), 200);
}
function secondsToHm(seconds) {
seconds = +seconds;
let hours = Math.floor(seconds / 3600);
let minutes = Math.floor((seconds % 3600) / 60);
return hours.toString().padStart(2, "0") + ":" + minutes.toString().padStart(2, "0");
}
function createElement(tag, { classes = [], text, attrs = {}, childs = [] }) {
const el = document.createElement(tag);
if (classes) el.classList.add(...classes);
if (text) el.textContent = text;
for (const [key, value] of Object.entries(attrs)) el.setAttribute(key, value);
if (childs) el.append(...childs);
return el;
}