Files
front-Censo/src/components/Equipo_Computo/Pregunta7.tsx
T

181 lines
4.6 KiB
TypeScript
Raw Normal View History

2025-11-11 17:51:13 -06:00
"use client";
2025-11-21 12:11:37 -06:00
2026-03-12 20:11:45 -04:00
2025-11-21 12:11:37 -06:00
import { useState, useEffect } from "react";
2025-11-20 18:53:55 -06:00
import Cookies from "js-cookie";
2025-11-21 12:11:37 -06:00
import axios from "axios";
2025-11-18 13:53:21 -06:00
import "./pregunta7.css";
2025-11-28 11:13:57 -06:00
import ToggleButton from "../Toggle/ToggleButton";
2026-03-19 19:01:47 -04:00
import dynamic from "next/dynamic";
const MapaCalor = dynamic(
() => import("./MapaCalor"),
{ ssr: false }
);
2025-11-11 17:51:13 -06:00
2025-11-21 12:11:37 -06:00
type AntiguedadItem = {
antiguedad: string | null;
total: number | string;
};
2025-11-20 18:53:55 -06:00
2025-11-21 12:11:37 -06:00
type RespuestaAntiguedad = {
2025-11-20 18:53:55 -06:00
impresion: AntiguedadItem[];
digitalizacion: AntiguedadItem[];
2025-11-21 12:11:37 -06:00
};
2025-11-20 18:53:55 -06:00
2025-11-18 13:53:21 -06:00
export default function Pregunta7() {
2025-11-11 17:51:13 -06:00
const [datos, setDatos] = useState([
2025-11-27 15:28:59 -06:00
{ nombre: "Impresión", valores: ["0.00", "0.00", "0.00", "0.00"] },
2025-11-21 12:11:37 -06:00
{
nombre: "Digitalización",
2025-11-28 16:19:36 -06:00
valores: ["0.00", "0.00", "0.00", "0.00"],
2025-11-21 12:11:37 -06:00
},
2025-11-11 17:51:13 -06:00
]);
2025-11-21 12:11:37 -06:00
const [loading, setLoading] = useState(true);
2025-11-20 18:53:55 -06:00
2025-11-21 12:11:37 -06:00
const api_url = process.env.NEXT_PUBLIC_API_URL;
2025-11-11 17:51:13 -06:00
2025-11-21 12:11:37 -06:00
const transformarDatos = (items: AntiguedadItem[]): string[] => {
2026-01-23 15:31:33 -06:00
let menor2 = 0;
2025-11-21 12:11:37 -06:00
let entre2_3 = 0;
let entre4_5 = 0;
2026-01-23 15:31:33 -06:00
let mayor6 = 0;
2025-11-21 12:11:37 -06:00
for (const item of items) {
const total = Number(item.total) || 0;
2026-01-23 15:31:33 -06:00
const antig = item.antiguedad?.toLowerCase().trim();
2025-11-21 12:11:37 -06:00
switch (antig) {
2026-03-12 18:45:04 -05:00
case "menor a 2 años":
2026-01-23 15:31:33 -06:00
menor2 += total;
2025-11-21 12:11:37 -06:00
break;
2026-01-23 15:31:33 -06:00
2026-03-12 18:45:04 -05:00
case "entre 2 y 3 años":
2025-11-21 12:11:37 -06:00
entre2_3 += total;
break;
2026-01-23 15:31:33 -06:00
2026-03-12 18:45:04 -05:00
case "entre 4 y 5 años":
2025-11-21 12:11:37 -06:00
entre4_5 += total;
break;
2026-01-23 15:31:33 -06:00
2026-03-12 18:45:04 -05:00
case "mayor a 6 años":
2026-01-23 15:31:33 -06:00
mayor6 += total;
2025-11-21 12:11:37 -06:00
break;
}
}
2026-01-23 15:31:33 -06:00
const suma = menor2 + entre2_3 + entre4_5 + mayor6;
2025-11-21 12:11:37 -06:00
2026-01-23 15:31:33 -06:00
if (suma === 0) return ["0.00", "0.00", "0.00", "0.00"];
2025-11-21 12:11:37 -06:00
2026-01-23 15:31:33 -06:00
const pct = (v: number) => ((v / suma) * 100).toFixed(2);
2025-11-21 12:11:37 -06:00
2026-01-23 15:31:33 -06:00
return [
pct(menor2), // Menor a 2
pct(entre2_3), // Entre 2 y 3
pct(entre4_5), // Entre 4 y 5
pct(mayor6), // Mayor a 6
];
2025-11-21 12:11:37 -06:00
};
2025-11-28 19:28:07 -06:00
const cargarDatos = async (baja: boolean) => {
2025-11-20 18:53:55 -06:00
const token = Cookies.get("token");
2025-11-28 19:28:07 -06:00
const body = baja ? ["BAJA"] : ["EN DESUSO", "EN USO"];
2025-11-20 18:53:55 -06:00
axios
2025-11-28 16:19:36 -06:00
.post<RespuestaAntiguedad>(
2025-11-21 12:11:37 -06:00
`${api_url}/equipos/reporte/contar_perifericos_antiguedad`,
2025-11-28 19:28:07 -06:00
body,
2025-11-21 12:11:37 -06:00
{
headers: { Authorization: `Bearer ${token}` },
2026-01-23 15:31:33 -06:00
},
2025-11-21 12:11:37 -06:00
)
.then((res) => {
const { impresion, digitalizacion } = res.data;
2025-11-20 18:53:55 -06:00
2025-11-21 12:11:37 -06:00
setDatos([
{ nombre: "Impresión", valores: transformarDatos(impresion) },
2025-11-27 15:28:59 -06:00
{
nombre: "Digitalización",
valores: transformarDatos(digitalizacion),
},
2025-11-21 12:11:37 -06:00
]);
2025-11-20 18:53:55 -06:00
})
2025-11-21 12:11:37 -06:00
.catch((err) => console.error("Error cargando datos:", err))
.finally(() => setLoading(false));
2025-11-28 19:28:07 -06:00
};
useEffect(() => {
cargarDatos(false);
2025-11-20 18:53:55 -06:00
}, []);
2025-11-21 12:11:37 -06:00
const calcularTotalFila = (valores: string[]) =>
valores.reduce((t, v) => t + Number(v), 0);
2025-11-11 17:51:13 -06:00
return (
<div className="contenedor-pregunta">
2025-11-27 15:28:59 -06:00
<div className="contenedor-censo">
Censo de equipos periféricos - Equipo de digitalización
</div>
<div className="pregunta-cuadro">
2025-11-28 16:19:36 -06:00
Antigüedad que tienen los equipos periféricos del área universitaria.
2025-11-28 19:28:07 -06:00
<ToggleButton
onChange={(estado) => {
cargarDatos(estado);
}}
/>
2025-11-18 13:53:21 -06:00
</div>
2025-11-11 17:51:13 -06:00
<div className="tabla-contenedor">
<table className="tabla">
<thead>
<tr>
2025-11-21 12:11:37 -06:00
<th className="azul-marino">% Antigüedad de los equipos</th>
2025-11-11 17:51:13 -06:00
<th className="rosa-fuerte">Menor a 2 años</th>
<th className="rosa-fuerte">Entre 2 y 3 años</th>
<th className="rosa-fuerte">Entre 4 y 5 años</th>
<th className="rosa-fuerte">Mayor a 6 años</th>
2025-11-27 15:28:59 -06:00
2025-11-11 17:51:13 -06:00
<th className="azul-marino">Total</th>
</tr>
</thead>
2025-11-21 12:11:37 -06:00
2025-11-11 17:51:13 -06:00
<tbody>
2025-11-21 12:11:37 -06:00
{datos.map((fila, idx) => {
const total = calcularTotalFila(fila.valores);
2025-11-20 18:53:55 -06:00
2025-11-11 17:51:13 -06:00
return (
2025-11-21 12:11:37 -06:00
<tr key={idx}>
2025-11-11 17:51:13 -06:00
<td>{fila.nombre}</td>
2025-11-20 18:53:55 -06:00
2025-11-21 12:11:37 -06:00
{fila.valores.map((v, c) => (
<td key={c}>
2025-11-11 17:51:13 -06:00
<div className="input-contenedor">
2025-11-21 12:11:37 -06:00
<input type="number" value={v} readOnly />
2025-11-11 17:51:13 -06:00
<span className="porcentaje">%</span>
</div>
</td>
))}
2025-11-20 18:53:55 -06:00
2025-11-27 15:28:59 -06:00
<td
className={`total-celda ${
total > 100.1 ? "total-error" : ""
}`}
>
2025-11-11 17:51:13 -06:00
{total.toFixed(2)}%
</td>
</tr>
);
})}
</tbody>
</table>
</div>
2026-03-13 21:32:26 -04:00
<MapaCalor />
2025-11-11 17:51:13 -06:00
</div>
2026-03-12 20:11:45 -04:00
2025-11-11 17:51:13 -06:00
);
2026-03-12 20:11:45 -04:00
}