delete all fetch

This commit is contained in:
2026-02-23 12:47:08 -06:00
parent d0b8cc3062
commit d53561e339
11 changed files with 169 additions and 142 deletions
@@ -216,4 +216,5 @@ export default function PlaticaGate({ inscripcion, numAcount }: Props) {
</div>
</div>
);
}
}
//IO
@@ -5,7 +5,6 @@
flex: 1;
width: 100%;
font-size: 14px;
margin-bottom: 1rem;
max-width: 500px;
}
+22 -13
View File
@@ -2,6 +2,7 @@
import { useEffect, useState } from "react";
import styles from "./Page.module.css";
import axios from "axios";
type AreaUbicacion = {
id_area_ubicacion: number;
@@ -24,20 +25,29 @@ export default function MachineTable() {
const [loading, setLoading] = useState(true);
const fetchMachines = async () => {
const res = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/equipo/disable`,
{ cache: "no-store" },
);
setMachines(await res.json());
setLoading(false);
try {
const res = await axios.get(
`${process.env.NEXT_PUBLIC_API_URL}/equipo/disable`
);
setMachines(res.data);
} catch (error) {
console.error("Error cargando equipos:", error);
} finally {
setLoading(false);
}
};
const fetchAreas = async () => {
const res = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/area-ubicacion`,
{ cache: "no-store" },
);
setAreas(await res.json());
try {
const res = await axios.get(
`${process.env.NEXT_PUBLIC_API_URL}/area-ubicacion`
);
setAreas(res.data);
} catch (error) {
console.error("Error cargando áreas:", error);
}
};
useEffect(() => {
@@ -56,8 +66,6 @@ export default function MachineTable() {
(machine) => machine.areaUbicacion.area === selectedArea,
);
if (loading) return <p>Cargando equipos...</p>;
return (
<div className={styles.tableContainer}>
<table className={styles.machineTable}>
@@ -108,3 +116,4 @@ export default function MachineTable() {
</div>
);
}
//IO
+27 -17
View File
@@ -2,6 +2,7 @@
import { useEffect, useState } from "react";
import styles from "./Page.module.css";
import axios from "axios";
type AreaUbicacion = {
id_area_ubicacion: number;
@@ -14,10 +15,9 @@ type Equipo = {
ubicacion: string;
plataforma: string;
area: string;
ocupado:boolean;
ocupado: boolean;
};
export default function MachineTableActive() {
const [machines, setMachines] = useState<Equipo[]>([]);
const [areas, setAreas] = useState<AreaUbicacion[]>([]);
@@ -25,20 +25,29 @@ export default function MachineTableActive() {
const [loading, setLoading] = useState(true);
const fetchMachines = async () => {
const res = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/equipo/active`,
{ cache: "no-store" },
);
setMachines(await res.json());
setLoading(false);
try {
const res = await axios.get(
`${process.env.NEXT_PUBLIC_API_URL}/equipo/active`
);
setMachines(res.data);
} catch (error) {
console.error("Error cargando equipos:", error);
} finally {
setLoading(false);
}
};
const fetchAreas = async () => {
const res = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/area-ubicacion`,
{ cache: "no-store" },
);
setAreas(await res.json());
try {
const res = await axios.get(
`${process.env.NEXT_PUBLIC_API_URL}/area-ubicacion`
);
setAreas(res.data);
} catch (error) {
console.error("Error cargando áreas:", error);
}
};
useEffect(() => {
@@ -50,8 +59,8 @@ export default function MachineTableActive() {
selectedArea === "Todo"
? machines
: machines.filter(
(machine) => machine.area === selectedArea,
);
(machine) => machine.area === selectedArea,
);
if (loading) return <p>Cargando equipos...</p>;
@@ -66,7 +75,7 @@ export default function MachineTableActive() {
<th>Área</th>
<th>Disponible</th>
</tr>
<tr style={{position:"relative", zIndex:"0"}}>
<tr style={{ position: "relative", zIndex: "0" }}>
<th />
<th />
<th />
@@ -90,7 +99,7 @@ export default function MachineTableActive() {
<tbody>
{filteredMachines.map((machine) => (
<tr key={machine.id_equipo} className={!machine.ocupado ? '':styles.ocupado}>
<tr key={machine.id_equipo} className={!machine.ocupado ? '' : styles.ocupado}>
<td>{machine.ubicacion}</td>
<td>{machine.nombre_equipo}</td>
<td className={styles[machine.plataforma]}>
@@ -105,3 +114,4 @@ export default function MachineTableActive() {
</div>
);
}
//IO
@@ -1,6 +1,7 @@
"use client";
import { envConfig } from "@/app/lib/config";
import axios from "axios";
import { useEffect, useState } from "react";
function Equipos() {
@@ -10,10 +11,18 @@ function Equipos() {
const [mensaje, setMensaje] = useState("");
useEffect(() => {
fetch(`${envConfig.apiUrl}/equipo`)
.then((res) => res.json())
.then((data) => setEquipos(data))
.catch(() => setMensaje("Error al cargar equipos"));
const fetchEquipos = async () => {
try {
const response = await axios.get(`${envConfig.apiUrl}/equipo`);
setEquipos(response.data);
} catch (error: any) {
setMensaje(
error.response?.data?.message || "Error al cargar equipos"
);
}
};
fetchEquipos();
}, []);
const handleBuscar = (e: React.FormEvent<HTMLFormElement>) => {
@@ -3,6 +3,7 @@
import { useEffect, useState } from "react";
import axios from "axios";
import { envConfig } from "@/app/lib/config";
import Swal from "sweetalert2";
interface Mesa {
id_mesa: number;
@@ -30,9 +31,9 @@ export default function MesasDisponibles() {
const toggleEquipo = async (id_mesa: number) => {
try {
await fetch(`${envConfig.apiUrl}/mesa/${id_mesa}/activo`, {
method: "PATCH",
});
await axios.patch(
`${envConfig.apiUrl}/mesa/${id_mesa}/activo`
);
setMesas((prev) =>
prev
@@ -42,12 +43,16 @@ export default function MesasDisponibles() {
...e,
activo: e.activo === 1 ? 0 : 1,
}
: e,
: e
)
: prev,
: prev
);
} catch (error) {
console.error("Error al cambiar estado del equipo", error);
} catch (error: any) {
Swal.fire({
title: "Error al cambiar estado",
text: error.response?.data?.message || error.message,
icon: "error",
});
}
};
+51 -62
View File
@@ -5,6 +5,7 @@ import { envConfig } from "@/app/lib/config";
import { getEquipoByCount } from "../lib/getEquipoByCount";
import { getMesaByCount } from "../lib/getMesaByCount";
import Swal from "sweetalert2";
import axios from "axios";
type Props = {
inscripcion: any[];
@@ -30,44 +31,43 @@ export default function AsignacionMesas({ idCuenta, inscripcion }: Props) {
setBitacora(null);
} else {
setBitacora([]);
if (!result?.error)
Swal.fire({
title: "Ya cuentas con un equipo asignado!",
icon: "error",
draggable: true,
});
if (!resultMesa?.error)
Swal.fire({
title: "Ya cuentas con mesa asignada!",
icon: "error",
draggable: true,
});
if (!result?.error) {
setError("Equio")
Swal.fire({
title: "Ya cuentas con un equipo asignado!",
icon: "error",
});
}
if (!resultMesa?.error) {
setError("Mesa")
Swal.fire({
title: "Ya cuentas con mesa asignada!",
icon: "error",
});
}
}
};
useEffect(() => {
if (!idCuenta) return;
const Cuenta = idCuenta;
if (isNaN(Cuenta)) return;
fetchByCuenta(Cuenta);
if (!idCuenta || isNaN(idCuenta)) return;
fetchByCuenta(idCuenta);
}, [idCuenta]);
useEffect(() => {
if (!Array.isArray(inscripcion) || inscripcion.length === 0) return;
const todasAsistieron = inscripcion.every(
(ins) => ins.platica?.data?.[0] === 1,
(ins) => ins.platica?.data?.[0] === 1
);
if (!todasAsistieron) {
Swal.fire({
title: "El alumno no asistió a todas las pláticas!",
icon: "error",
draggable: true,
});
setPuedeContinuar(false);
return;
}
@@ -81,23 +81,18 @@ export default function AsignacionMesas({ idCuenta, inscripcion }: Props) {
const fetchMesas = async () => {
try {
setLoadingMesas(true);
const res = await fetch(`${envConfig.apiUrl}/mesa/activo`, {
cache: "no-store",
});
if (!res.ok) {
throw new Error("No se pudieron cargar las mesas");
}
const response = await axios.get(
`${envConfig.apiUrl}/mesa/activo`
);
const data = await res.json();
setMesas(data);
} catch (error) {
setMesas(response.data);
} catch (error: any) {
Swal.fire({
title: "Error al cargar mesas disponibles!",
icon: "error",
draggable: true,
});
title: "Error al cargar mesas disponibles!",
text: error.response?.data?.message || error.message,
icon: "error",
});
} finally {
setLoadingMesas(false);
}
@@ -109,12 +104,10 @@ export default function AsignacionMesas({ idCuenta, inscripcion }: Props) {
const submit = async (e: React.MouseEvent<HTMLButtonElement>) => {
e.preventDefault();
if (!mesaSeleccionada) {
Swal.fire({
if (!mesaSeleccionada || Number(mesaSeleccionada) === 0) {
Swal.fire({
title: "Seleccione una mesa!",
icon: "error",
draggable: true,
});
return;
}
@@ -126,31 +119,22 @@ export default function AsignacionMesas({ idCuenta, inscripcion }: Props) {
id_alumno_inscrito: inscripcion[0].id_alumno_inscrito,
};
const res = await fetch(`${envConfig.apiUrl}/bitacora-mesa`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
if (!res.ok) {
throw new Error("Error al asignar mesa");
}
await axios.post(
`${envConfig.apiUrl}/bitacora-mesa`,
body
);
setBitacora([]);
Swal.fire({
title: "Mesa asignada correctamente!",
icon: "success",
draggable: true,
});
} catch (error) {
Swal.fire({
} catch (error: any) {
Swal.fire({
title: "No se pudo asignar la mesa!",
text: error.response?.data?.message || error.message,
icon: "error",
draggable: true,
});
}
};
@@ -158,11 +142,15 @@ export default function AsignacionMesas({ idCuenta, inscripcion }: Props) {
if (!puedeContinuar) return null;
if (bitacora) {
return error === "Mesa"
? <p style={{ margin: "1rem", color: "red" }}>Ya cuentas con una mesa asignada</p>
: error === "Equipo"
? <p style={{ margin: "1rem", color: "red" }}>Ya cuentas con un equipo asignado</p>
: null;
return error === "Mesa" ? (
<p style={{ margin: "1rem", color: "red" }}>
Ya cuentas con una mesa asignada
</p>
) : error === "Equipo" ? (
<p style={{ margin: "1rem", color: "red" }}>
Ya cuentas con un equipo asignado
</p>
) : null;
}
return (
@@ -193,6 +181,7 @@ export default function AsignacionMesas({ idCuenta, inscripcion }: Props) {
)}
<option value={0}>Selecciona una mesa</option>
{!loadingMesas &&
mesas.map((mesa) => (
<option key={mesa.id_mesa} value={mesa.id_mesa}>
@@ -212,4 +201,4 @@ export default function AsignacionMesas({ idCuenta, inscripcion }: Props) {
</div>
);
}
//
//IO
+1
View File
@@ -176,3 +176,4 @@ export default function CheckBoxEquipo({ numAcount, machine }: Props) {
</>
);
}
//IO
+1 -1
View File
@@ -162,4 +162,4 @@ export default function CheckBoxMesa({ numAcount, table }: props) {
</>
);
}
//I
//IO
+28 -21
View File
@@ -4,43 +4,50 @@ import { envConfig } from "@/app/lib/config";
import { useEffect, useState } from "react";
import "./tableEquipos.css";
import axios from "axios";
export default function TableEquipos() {
const [equipos, setEquipos] = useState<any[]>([]);
const [loading, setLoading] = useState(true);
useEffect(() => {
fetch(`${envConfig.apiUrl}/equipo`)
.then((res) => res.json())
.then((data) => {
setEquipos(data);
const fetchEquipos = async () => {
try {
const response = await axios.get(`${envConfig.apiUrl}/equipo`);
setEquipos(response.data);
} catch (error: any) {
console.error("ERROR API:", error.response?.data || error.message);
} finally {
setLoading(false);
})
.catch((err) => {
console.error("ERROR API:", err);
setLoading(false);
});
}
};
fetchEquipos();
}, []);
const toggleEquipo = async (id_equipo: number) => {
try {
await fetch(`${envConfig.apiUrl}/equipo/${id_equipo}/activo`, {
method: "PATCH",
});
await axios.patch(
`${envConfig.apiUrl}/equipo/${id_equipo}/activo`
);
setEquipos((prev) =>
prev.map((e) =>
e.id_equipo === id_equipo
? {
...e,
activo: {
data: [e.activo?.data?.[0] === 1 ? 0 : 1],
},
}
: e,
),
...e,
activo: {
data: [e.activo?.data?.[0] === 1 ? 0 : 1],
},
}
: e
)
);
} catch (error: any) {
console.error(
"Error al cambiar estado del equipo",
error.response?.data || error.message
);
} catch (error) {
console.error("Error al cambiar estado del equipo", error);
}
};
+12 -15
View File
@@ -166,33 +166,30 @@ export default function Inscripcion({
const id_plataforma = PLATAFORMA_MAP[plataformaSeleccionada];
try {
const res = await fetch(`${envConfig.apiUrl}/alumno-inscrito`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
const res = await axios.post(
`${envConfig.apiUrl}/alumno-inscrito`,
{
id_cuenta: numAcount,
id_plataforma,
realizo_pago: false,
}),
});
const data = await res.json();
if (!res.ok) {
Swal.fire("Error");
throw new Error(data.message || "Error al inscribir");
}
}
);
Swal.fire({
title: "Alumno inscrito sin pago!",
icon: "success",
draggable: true,
});
router.refresh();
} catch (err: any) {
const message =
err.response?.data?.message || "Error al inscribir";
Swal.fire({
title: err.message,
title: message,
icon: "error",
draggable: true,
});