"use client"; import { useState, useEffect } from "react"; import Cookies from "js-cookie"; import axios from "axios"; import "./pregunta7.css"; import ToggleButton from "../Toggle/ToggleButton"; import dynamic from "next/dynamic"; const MapaCalor = dynamic( () => import("./MapaCalor"), { ssr: false } ); type AntiguedadItem = { antiguedad: string | null; total: number | string; }; type RespuestaAntiguedad = { impresion: AntiguedadItem[]; digitalizacion: AntiguedadItem[]; }; export default function Pregunta7() { const [datos, setDatos] = useState([ { nombre: "Impresión", valores: ["0.00", "0.00", "0.00", "0.00"] }, { nombre: "Digitalización", valores: ["0.00", "0.00", "0.00", "0.00"], }, ]); const [loading, setLoading] = useState(true); const api_url = process.env.NEXT_PUBLIC_API_URL; const transformarDatos = (items: AntiguedadItem[]): string[] => { let menor2 = 0; let entre2_3 = 0; let entre4_5 = 0; let mayor6 = 0; for (const item of items) { const total = Number(item.total) || 0; const antig = item.antiguedad?.toLowerCase().trim(); switch (antig) { case "menor a 2 años": menor2 += total; break; case "entre 2 y 3 años": entre2_3 += total; break; case "entre 4 y 5 años": entre4_5 += total; break; case "mayor a 6 años": mayor6 += total; break; } } const suma = menor2 + entre2_3 + entre4_5 + mayor6; if (suma === 0) return ["0.00", "0.00", "0.00", "0.00"]; const pct = (v: number) => ((v / suma) * 100).toFixed(2); 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 ]; }; const cargarDatos = async (baja: boolean) => { const token = Cookies.get("token"); const body = baja ? ["BAJA"] : ["EN DESUSO", "EN USO"]; axios .post( `${api_url}/equipos/reporte/contar_perifericos_antiguedad`, body, { headers: { Authorization: `Bearer ${token}` }, }, ) .then((res) => { const { impresion, digitalizacion } = res.data; setDatos([ { nombre: "Impresión", valores: transformarDatos(impresion) }, { nombre: "Digitalización", valores: transformarDatos(digitalizacion), }, ]); }) .catch((err) => console.error("Error cargando datos:", err)) .finally(() => setLoading(false)); }; useEffect(() => { cargarDatos(false); }, []); const calcularTotalFila = (valores: string[]) => valores.reduce((t, v) => t + Number(v), 0); return (
Censo de equipos periféricos - Equipo de digitalización
Antigüedad que tienen los equipos periféricos del área universitaria. { cargarDatos(estado); }} />
{datos.map((fila, idx) => { const total = calcularTotalFila(fila.valores); return ( {fila.valores.map((v, c) => ( ))} ); })}
% Antigüedad de los equipos Menor a 2 años Entre 2 y 3 años Entre 4 y 5 años Mayor a 6 años Total
{fila.nombre}
%
100.1 ? "total-error" : "" }`} > {total.toFixed(2)}%
); }