added conection to api in inscritos

This commit is contained in:
2026-02-17 15:04:16 -06:00
parent 62afe827dc
commit 90bdb482cd
+111 -42
View File
@@ -1,79 +1,148 @@
"use client" "use client";
import { envConfig } from "@/app/lib/config"; import { envConfig } from "@/app/lib/config";
import axios from "axios"; import axios from "axios";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
interface Periodo{ interface Periodo {
semestre:string, id_periodo: number;
semestre: string;
} }
export default function Inscripciones() { interface ApiResponse {
const [periodo,setPeriodo]= useState<Periodo[]>([]) carrera: string;
genero: string | null;
profesor: string;
total: string;
}
useEffect(()=>{ interface TablaRow {
const getPeriodo = async()=>{ carrera: string;
const response = await axios.get(`${envConfig.apiUrl}/periodo`) genero: string | null;
setPeriodo(response.data) WINDOWS: number;
} MACINTOSH: number;
getPeriodo() LINUX: number;
},[]) PROFESORES: number;
TOTAL: number;
}
type TipoConteo = "WINDOWS" | "MACINTOSH" | "LINUX" | "PROFESORES";
export default function Inscripciones() {
const [periodo, setPeriodo] = useState<Periodo[]>([]);
const [selectedPeriodo, setSelectedPeriodo] = useState<number | null>(null);
const [tablaData, setTablaData] = useState<TablaRow[]>([]);
useEffect(() => {
const getPeriodo = async () => {
const response = await axios.get(`${envConfig.apiUrl}/periodo`);
setPeriodo(response.data);
};
getPeriodo();
}, []);
const handleBuscar = async (e: React.FormEvent) => {
e.preventDefault();
if (!selectedPeriodo) return;
const response = await axios.get(
`${envConfig.apiUrl}/alumno-inscrito/inscritos/${selectedPeriodo}`
);
const data: ApiResponse[] = response.data;
const agrupado: Record<string, TablaRow> = {};
data.forEach((item) => {
const generoNormalizado =
item.genero === "F" || item.genero === "Femenino"
? "Femenino"
: item.genero === "M" || item.genero === "Masculino"
? "Masculino"
: "-";
const key = `${item.carrera}_${generoNormalizado}`;
if (!agrupado[key]) {
agrupado[key] = {
carrera: item.carrera,
genero: generoNormalizado,
WINDOWS: 0,
MACINTOSH: 0,
LINUX: 0,
PROFESORES: 0,
TOTAL: 0,
};
}
const tipo = item.profesor as TipoConteo;
const cantidad = Number(item.total);
agrupado[key][tipo] += cantidad;
agrupado[key].TOTAL += cantidad;
});
setTablaData(Object.values(agrupado));
};
return ( return (
<section className="containerSection"> <section className="containerSection">
<h2 className="title"> INSCRITOS </h2> <h2 className="title">INSCRITOS</h2>
<form className="containerForm"> <form className="containerForm" onSubmit={handleBuscar}>
<label>Seleccione el periodo</label> <label>Seleccione el periodo</label>
<div className="groupInput"> <div className="groupInput">
<select> <select
{periodo.map((p,index)=>( onChange={(e) => setSelectedPeriodo(Number(e.target.value))}
<option key={index}> defaultValue=""
{p.semestre} >
</option>))} <option value="" disabled>
Seleccione
</option>
{periodo.map((p) => (
<option key={p.id_periodo} value={p.id_periodo}>
{p.semestre}
</option>
))}
</select> </select>
<button className="button buttonSearch" type="submit"> <button className="button buttonSearch" type="submit">
Buscar Buscar
</button> </button>
</div> </div>
</form> </form>
<div> <div style={{background:"#f9f9f9", position:"sticky", top:"-1000px"}}>
<table> <table>
<thead> <thead>
<tr> <tr>
<th>Carrera</th> <th>Carrera</th>
<th>Genero</th> <th>Genero</th>
<th>WINDOWS</th>
<th>MACINTOSH</th>
<th>LINUX</th>
<th>Profesores</th> <th>Profesores</th>
<th>Total</th> <th>Total</th>
</tr> </tr>
</thead> </thead>
<tbody></tbody>
</table>
</div>
<div>
<table>
<thead>
<tr>
<th>Impresione B/N</th>
<th>Impresiones Color</th>
<th>Plotteo</th>
<th>Escaner</th>
<th>Total</th>
</tr>
</thead>
<tbody> <tbody>
<tr> {tablaData.map((row, index) => (
<td>5,000</td> <tr key={index}>
<td>10,000</td> <td>{row.carrera}</td>
<td>10,000</td> <td>{row.genero ?? "-"}</td>
<td>20,000</td> <td>{row.WINDOWS}</td>
<td>45,000</td> <td>{row.MACINTOSH}</td>
</tr> <td>{row.LINUX}</td>
<td>{row.PROFESORES}</td>
<td>{row.TOTAL}</td>
</tr>
))}
</tbody> </tbody>
</table> </table>
</div> </div>
</section> </section>
); );
} }
//IO