Files
front-AT/app/Components/CheckBoxEquipo.tsx
T

231 lines
5.9 KiB
TypeScript
Raw Normal View History

2025-10-02 01:39:50 -06:00
"use client";
2026-01-27 12:27:03 -06:00
import { useEffect, useState } from "react";
2026-01-28 11:05:24 -06:00
import { envConfig } from "../lib/config";
import { getEquipoByCount } from "../lib/getEquipoByCount";
2025-10-02 01:39:50 -06:00
import SearchUser from "./Global/SearchUser/searchUser";
2026-01-27 12:27:03 -06:00
import SearchEquipo from "./SearchEquipo";
import axios from "axios";
import Information from "./Global/Information/information";
2026-02-11 16:28:58 -06:00
import Swal from "sweetalert2";
import Cookies from "js-cookie";
2026-01-27 12:27:03 -06:00
async function getEquipoId(idEquipo: number) {
try {
const token = Cookies.get("token");
const headers = { Authorization: `Bearer ${token}` };
2026-01-27 12:27:03 -06:00
const res = await axios.get(
`${envConfig.apiUrl}/bitacora/equipo/${idEquipo}`,
2026-03-02 14:17:43 -06:00
{ headers }
2026-01-27 12:27:03 -06:00
);
2026-03-02 14:17:43 -06:00
2026-01-27 12:27:03 -06:00
return res.data;
} catch (error: any) {
if (axios.isAxiosError(error)) {
return {
error: error.response?.data?.message || "Error al consultar equipo",
};
}
return { error: "Error desconocido" };
}
}
2025-10-02 01:39:50 -06:00
2026-01-27 12:27:03 -06:00
interface Props {
2026-01-21 17:52:11 -06:00
numAcount?: string | null;
2026-01-27 12:27:03 -06:00
machine?: string | null;
}
export default function CheckBoxEquipo({ numAcount, machine }: Props) {
2026-03-02 14:17:43 -06:00
const [modo, setModo] = useState<"Equipo" | "Cuenta">("Cuenta");
2026-01-27 12:27:03 -06:00
2026-03-02 14:17:43 -06:00
const [bitacoraCuenta, setBitacoraCuenta] = useState<any>(null);
const [bitacoraEquipo, setBitacoraEquipo] = useState<any>(null);
2026-03-02 10:34:42 -06:00
2026-03-02 14:17:43 -06:00
const [tiempoRestante, setTiempoRestante] = useState("");
const [minutos, setMinutos] = useState<number>(0);
const [errorCuenta, setErrorCuenta] = useState<string | null>(null);
const [errorEquipo, setErrorEquipo] = useState<string | null>(null);
const [loading, setLoading] = useState(false);
const bitacora =
modo === "Cuenta" ? bitacoraCuenta : bitacoraEquipo;
2026-03-02 10:34:42 -06:00
2026-01-27 13:47:38 -06:00
const fetchByCuenta = async (idCuenta: number) => {
const result = await getEquipoByCount(idCuenta);
2026-01-27 12:27:03 -06:00
2026-01-27 13:47:38 -06:00
if (result?.error) {
2026-03-02 14:17:43 -06:00
setErrorCuenta(result.error);
setBitacoraCuenta(null);
2026-01-27 13:47:38 -06:00
} else {
2026-03-02 14:17:43 -06:00
setErrorCuenta(null);
setBitacoraCuenta(result);
2026-01-27 13:47:38 -06:00
}
};
2026-01-27 12:27:03 -06:00
2026-01-27 13:47:38 -06:00
const fetchByEquipo = async (idEquipo: number) => {
const result = await getEquipoId(idEquipo);
2026-01-27 12:27:03 -06:00
2026-01-27 13:47:38 -06:00
if (result?.error) {
2026-03-02 14:17:43 -06:00
setErrorEquipo(result.error);
setBitacoraEquipo(null);
2026-01-27 13:47:38 -06:00
} else {
2026-03-02 14:17:43 -06:00
setErrorEquipo(null);
setBitacoraEquipo(result);
2026-01-27 13:47:38 -06:00
}
};
2026-01-27 12:27:03 -06:00
useEffect(() => {
2026-03-02 14:17:43 -06:00
if (modo !== "Cuenta" || !numAcount) return;
2026-01-27 12:27:03 -06:00
2026-03-02 14:17:43 -06:00
const idCuenta = parseInt(numAcount);
if (!isNaN(idCuenta)) fetchByCuenta(idCuenta);
}, [numAcount, modo]);
2026-01-27 12:27:03 -06:00
2026-01-27 13:47:38 -06:00
useEffect(() => {
2026-03-02 14:17:43 -06:00
if (modo !== "Equipo" || !machine) return;
2026-01-27 12:27:03 -06:00
2026-03-02 14:17:43 -06:00
const idEquipo = parseInt(machine);
if (!isNaN(idEquipo)) fetchByEquipo(idEquipo);
}, [machine, modo]);
2026-01-27 12:27:03 -06:00
useEffect(() => {
if (!bitacora) return;
const entrada = new Date(bitacora.tiempo_entrada).getTime();
const asignadoMs = bitacora.tiempo_asignado * 60 * 1000;
2026-03-02 14:17:43 -06:00
const updateTimer = () => {
2026-01-27 12:27:03 -06:00
const ahora = Date.now();
const restante = entrada + asignadoMs - ahora;
if (restante <= 0) {
setTiempoRestante("agotado");
2026-03-02 14:17:43 -06:00
setMinutos(0);
return false;
2026-01-27 12:27:03 -06:00
}
2026-03-02 14:17:43 -06:00
const minutosRestantes = Math.floor(restante / 60000);
const segundos = Math.floor((restante % 60000) / 1000);
setMinutos(minutosRestantes + 1);
2026-03-02 14:17:43 -06:00
setTiempoRestante(
`${minutosRestantes} minuto(s) con ${segundos} segundo(s)`
);
return true;
};
updateTimer();
const interval = setInterval(() => {
const sigue = updateTimer();
if (!sigue) clearInterval(interval);
2026-01-27 12:27:03 -06:00
}, 1000);
return () => clearInterval(interval);
}, [bitacora]);
2026-01-21 17:10:17 -06:00
2026-01-27 13:47:38 -06:00
const handleButton = async (e: React.MouseEvent<HTMLButtonElement>) => {
e.preventDefault();
2026-03-02 14:17:43 -06:00
if (!bitacora) return;
2026-01-27 13:47:38 -06:00
2026-03-02 14:17:43 -06:00
setLoading(true);
2026-02-19 10:48:45 -06:00
2026-03-02 14:17:43 -06:00
try {
const token = Cookies.get("token");
const headers = { Authorization: `Bearer ${token}` };
2026-03-02 14:17:43 -06:00
await axios.patch(
`${envConfig.apiUrl}/bitacora/cancelar/${bitacora.id_bitacora}`,
{ tiempo_asignado: minutos },
{ headers }
);
2026-01-27 13:47:38 -06:00
2026-03-02 14:17:43 -06:00
Swal.fire({
title: "Tiempo cancelado correctamente!",
icon: "success",
});
if (modo === "Cuenta" && numAcount) {
fetchByCuenta(parseInt(numAcount));
setBitacoraEquipo(null);
2026-03-02 14:17:43 -06:00
}
2026-01-27 13:47:38 -06:00
2026-03-02 14:17:43 -06:00
if (modo === "Equipo" && machine) {
fetchByEquipo(parseInt(machine));
setBitacoraCuenta(null);
2026-03-02 14:17:43 -06:00
}
} catch (error) {
Swal.fire({
title: "Error al cancelar tiempo",
icon: "error",
});
} finally {
setLoading(false);
2026-01-27 13:47:38 -06:00
}
};
2025-10-02 01:39:50 -06:00
return (
<>
2026-01-21 17:10:17 -06:00
<div className="radio-grid">
<label className="radio-card">
2025-10-02 01:39:50 -06:00
<input
2026-01-21 17:10:17 -06:00
type="radio"
checked={modo === "Equipo"}
onChange={() => setModo("Equipo")}
/>
<span className="radio-ui"></span>
<span className="radio-text">Equipo</span>
2025-10-02 01:39:50 -06:00
</label>
2026-01-21 17:10:17 -06:00
<label className="radio-card">
2025-10-02 01:39:50 -06:00
<input
2026-01-21 17:10:17 -06:00
type="radio"
checked={modo === "Cuenta"}
onChange={() => setModo("Cuenta")}
/>
<span className="radio-ui"></span>
<span className="radio-text">Cuenta</span>
2025-10-02 01:39:50 -06:00
</label>
</div>
2026-01-21 17:10:17 -06:00
2026-03-02 14:17:43 -06:00
{modo === "Cuenta" && (
<>
<SearchUser value={numAcount ?? null} />
<h2>{errorCuenta}</h2>
</>
)}
2026-01-27 12:27:03 -06:00
2026-03-02 14:17:43 -06:00
{modo === "Equipo" && (
<>
<SearchEquipo value={machine ?? null} />
<h2>{errorEquipo}</h2>
</>
)}
2026-02-19 10:48:45 -06:00
2026-02-24 11:53:29 -06:00
{bitacora && tiempoRestante !== "agotado" && (
2026-01-27 13:47:38 -06:00
<>
<Information
NoCuenta={bitacora.alumno_inscrito.alumno.id_cuenta}
Nombre={bitacora.alumno_inscrito.alumno.nombre}
Tiempo={tiempoRestante}
Equipo={bitacora.equipo.ubicacion}
/>
2026-02-24 11:53:29 -06:00
2026-01-27 13:47:38 -06:00
<button
className="button buttonSearch"
style={{ marginTop: "1rem" }}
onClick={handleButton}
2026-03-02 14:17:43 -06:00
disabled={loading || minutos === 0}
2026-01-27 13:47:38 -06:00
>
Cancelar tiempo
</button>
</>
2026-01-27 12:27:03 -06:00
)}
2025-10-02 01:39:50 -06:00
</>
);
2026-03-02 14:17:43 -06:00
}