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

214 lines
5.7 KiB
TypeScript
Raw Normal View History

2025-11-11 17:51:13 -06:00
"use client";
2025-11-19 18:24:38 -06:00
2025-11-21 12:11:37 -06:00
import { useState, useEffect } from "react";
2025-11-19 18:24:38 -06:00
import Cookies from "js-cookie";
import axios from "axios";
2025-11-19 18:55:39 -06:00
import Pregunta10 from "./pregunta10";
2025-11-28 11:13:57 -06:00
import ToggleButton from "../Toggle/ToggleButton";
2025-11-11 17:51:13 -06:00
2025-12-05 16:52:31 -06:00
import "./pregunta9.css";
2025-11-19 18:24:38 -06:00
type AntiguedadItem = {
antiguedad: string | null;
total: string;
2025-11-28 16:19:36 -06:00
porcentaje: string;
2025-11-19 18:24:38 -06:00
};
type ApiResponse = {
escritorios: AntiguedadItem[];
portatiles: AntiguedadItem[];
altoRendimiento: AntiguedadItem[];
};
2025-11-11 17:51:13 -06:00
export default function Pregunta9() {
const [datos, setDatos] = useState([
2025-11-28 16:19:36 -06:00
{
nombre: "Computadoras de Escritorio",
2025-12-05 16:52:31 -06:00
valores: ["0.00", "0.00", "0.00", "0.00"],
2025-11-28 16:19:36 -06:00
},
{
nombre: "Computadoras Portátiles",
2025-12-05 16:52:31 -06:00
valores: ["0.00", "0.00", "0.00", "0.00"],
2025-11-28 16:19:36 -06:00
},
{
nombre: "Alto Rendimiento",
2025-12-05 16:52:31 -06:00
valores: ["0.00", "0.00", "0.00", "0.00"],
2025-11-28 16:19:36 -06:00
},
2025-11-11 17:51:13 -06:00
]);
2025-11-19 18:24:38 -06:00
const [loading, setLoading] = useState(true);
// Transforma los datos del backend al formato de 4 columnas con % calculados
const transformarDatos = (items: AntiguedadItem[]): string[] => {
let menores2 = 0;
let entre2_3 = 0;
let entre4_6 = 0;
let mayores6 = 0;
for (const item of items) {
const total = Number(item.total) || 0;
switch (item.antiguedad) {
2026-02-04 15:53:31 -06:00
case "Menor a 2 años":
2025-11-19 18:24:38 -06:00
menores2 += total;
break;
2026-02-04 15:53:31 -06:00
case "Entre 2 y 3 años":
2025-11-19 18:24:38 -06:00
entre2_3 += total;
break;
2026-02-04 15:53:31 -06:00
case "Entre 4 y 5 años":
2025-11-19 18:24:38 -06:00
entre4_6 += total;
break;
2026-02-04 15:53:31 -06:00
case "Mayor a 6 años":
2025-11-19 18:24:38 -06:00
mayores6 += total;
break;
2025-11-28 16:19:36 -06:00
// Cualquier otro valor (aunque no debería haber) lo meteríamos en mayores6
default:
2025-12-05 16:52:31 -06:00
mayores6 += total;
2025-11-28 16:19:36 -06:00
break;
2025-11-19 18:24:38 -06:00
}
}
2025-12-05 16:52:31 -06:00
const totalEquipo = menores2 + mayores6 + entre2_3 + entre4_6 ;
2025-11-19 18:24:38 -06:00
if (totalEquipo === 0) {
2025-12-05 16:52:31 -06:00
return ["0.00", "0.00", "0.00", "0.00"];
2025-11-19 18:24:38 -06:00
}
const pct = (valor: number) => ((valor / totalEquipo) * 100).toFixed(2);
2025-11-28 16:19:36 -06:00
return [
pct(menores2),
pct(entre2_3),
pct(entre4_6),
pct(mayores6),
];
2025-11-11 17:51:13 -06:00
};
2025-11-28 19:28:07 -06:00
const cargarDatos = async (baja: boolean) => {
2025-11-19 18:24:38 -06:00
const token = Cookies.get("token");
if (!token) {
console.error("Token no encontrado");
setLoading(false);
return;
}
2025-11-28 19:28:07 -06:00
const body = baja ? ["BAJA"] : ["EN DESUSO", "EN USO"];
2025-11-19 18:24:38 -06:00
axios
2025-11-28 16:19:36 -06:00
.post<ApiResponse>(
`${process.env.NEXT_PUBLIC_API_URL}/equipos/reporte/tipoEquipos_antiguedad`,
2025-11-28 19:28:07 -06:00
body,
2025-11-28 16:19:36 -06:00
{
headers: { Authorization: `Bearer ${token}` },
}
)
2025-11-19 18:24:38 -06:00
.then((res) => {
const { escritorios, portatiles, altoRendimiento } = res.data;
setDatos([
{
nombre: "Computadoras de Escritorio",
valores: transformarDatos(escritorios),
},
{
nombre: "Computadoras Portátiles",
valores: transformarDatos(portatiles),
},
{
nombre: "Alto Rendimiento",
valores: transformarDatos(altoRendimiento),
},
]);
})
.catch((err) => {
console.error("Error al cargar datos de antigüedad", err);
})
.finally(() => {
setLoading(false);
});
2025-11-28 19:28:07 -06:00
};
useEffect(() => {
cargarDatos(false);
2025-11-19 18:24:38 -06:00
}, []);
const calcularTotalFila = (valores: string[]) =>
valores.reduce((sum, val) => sum + parseFloat(val), 0);
if (loading) {
return <div className="contenedor-pregunta">Cargando datos...</div>;
}
2025-11-11 17:51:13 -06:00
return (
<div className="contenedor-pregunta">
2025-11-28 16:19:36 -06:00
<div className="contenedor-censo">
Censo de equipos de cómputo - Generales
</div>
2025-11-19 18:24:38 -06:00
{/* Pregunta intro (servidores) */}
2025-11-20 12:53:22 -06:00
{/* <div className="pregunta-cuadro" style={{ marginTop: "30px" }}>
2025-11-19 14:57:55 -06:00
Indique cuantos servidores son utilizados en ambientes productivos y si en ellos se almacenan datos personales.
</div>
2025-11-19 18:00:12 -06:00
2025-11-20 12:53:22 -06:00
<Pregunta4/> */}
2025-11-19 14:57:55 -06:00
2025-11-19 18:24:38 -06:00
{/* Pregunta 9: Antigüedad */}
2025-11-27 19:28:48 -06:00
<div className="pregunta-cuadro">
2025-11-28 16:19:36 -06:00
Antigüedad que tienen los equipos de cómputo del área universitaria.
2025-11-28 19:28:07 -06:00
<ToggleButton
onChange={(estado) => {
cargarDatos(estado);
}}
/>
2025-11-19 14:57:55 -06:00
</div>
2025-11-11 17:51:13 -06:00
<div className="tabla-contenedor">
<table className="tabla">
<thead>
<tr>
2025-11-19 18:24:38 -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>
<th className="azul-marino">Total</th>
</tr>
</thead>
<tbody>
2025-11-19 18:24:38 -06:00
{datos.map((fila, idx) => {
const total = calcularTotalFila(fila.valores);
2025-11-11 17:51:13 -06:00
return (
2025-11-19 18:24:38 -06:00
<tr key={idx}>
2025-11-11 17:51:13 -06:00
<td>{fila.nombre}</td>
2025-11-19 18:24:38 -06:00
{fila.valores.map((valor, colIdx) => (
<td key={colIdx}>
2025-11-11 17:51:13 -06:00
<div className="input-contenedor">
<input
type="number"
2025-11-19 18:24:38 -06:00
step="0.01"
2025-11-11 17:51:13 -06:00
value={valor}
2025-11-19 18:24:38 -06:00
readOnly
2025-11-11 17:51:13 -06:00
/>
<span className="porcentaje">%</span>
</div>
</td>
))}
2025-11-28 16:19:36 -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>
2025-11-28 16:19:36 -06:00
<Pregunta10 />
2025-11-11 17:51:13 -06:00
</div>
);
2025-11-28 16:19:36 -06:00
}