Skip to content

Commit

Permalink
Refactor API to handle empty response in inhabilitarAlumno function
Browse files Browse the repository at this point in the history
  • Loading branch information
yoelmarain committed Oct 16, 2024
1 parent ccef44a commit 3757211
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
9 changes: 7 additions & 2 deletions FrontAdmin/src/API/Inhabilitaciones.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@ export const inhabilitarAlumno = async (legajo: number) => {
},
});
if (response.ok) {
const data = await response.json();
return data;
if (response.status === 204) {
// No hay contenido en la respuesta
return {};
} else {
const data = await response.json();
return data;
}
} else {
throw new Error('Error en la respuesta del servidor');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ export default function AInhabilitar() {
const headers = ['Apellido y Nombre', 'Legajo' , 'DNI'];
const [request, setRequest] = useState<boolean>(false);

useEffect (() => {
const fetchData = async () => {
const response2 = await getAlumnosaInhabilitar();
setAlumnosAInhabilitar(response2.results);
setLoading(false);
};
const fetchData = async () => {
setLoading(true);
const response2 = await getAlumnosaInhabilitar();
setAlumnosAInhabilitar(response2.results);
};

useEffect(() => {
fetchData();
}, []);

Expand All @@ -25,20 +26,17 @@ export default function AInhabilitar() {
borderRadius={8}
w={"100%"}
>
{loading ? (
<Spinner />
) : (

<Box w={"100%"}>
<Flex justifyContent={"center"} w={"100%"}>
{alumnosAInhabilitar.length > 0 ? ( <Flex direction={"column"} w={"100%"}>
<Tabla headers={headers} data={alumnosAInhabilitar} request={request} />
<Tabla headers={headers} data={alumnosAInhabilitar} request={request} onInhabilitar={fetchData} />
<Flex justifyContent={"flex-end"}>
<Button
onClick={() => setRequest(true)}
color={"white"}
size={"md"}
m={2}

>
Inhabilitar
</Button>
Expand All @@ -47,7 +45,7 @@ export default function AInhabilitar() {
) : <p>No hay datos para mostrar</p>}
</Flex>
</Box>
)}

</Box>

);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import React, { useState } from 'react';
import React, { useEffect, useState } from 'react';
import { Table, Tr, Th,Thead, Tbody, Td, Checkbox } from '@chakra-ui/react';
import { useNavigate } from 'react-router-dom';
import { inhabilitarAlumno } from '../../../../../API/Inhabilitaciones';
import { set } from 'date-fns';

interface TablaProps {
headers: string[];
data: Array<Record<string, any>>;
request: boolean;
onInhabilitar: () => void;
}

const Tabla: React.FC<TablaProps> = ({ headers, data, request }) => {
const Tabla: React.FC<TablaProps> = ({ headers, data, request, onInhabilitar }) => {
const [selectedRows, setSelectedRows] = useState<{ [key: string]: boolean }>({});
const navigate = useNavigate();
const [renderKey, setRenderKey] = useState(0);

const keyMap: { [key: string]: string } = {
'Apellido y Nombre': 'full_name',
Expand All @@ -34,10 +37,25 @@ const Tabla: React.FC<TablaProps> = ({ headers, data, request }) => {
setSelectedRows(newSelectedRows);
};

useEffect(() => {
const handleInhabilitar = async () => {
const alumnosInhabilitar = data.filter((row) => selectedRows[row.user]);
for (const alumno of alumnosInhabilitar) {
await inhabilitarAlumno(alumno.legajo);
}
setSelectedRows({});
setRenderKey((prevKey) => prevKey + 1);
onInhabilitar();
};
if (request) {
handleInhabilitar();
}
}, [request]);

const allRowsSelected = data.length > 0 && Object.keys(selectedRows).length === data.length && Object.values(selectedRows).every(Boolean);

return (
<Table variant="simple">
<Table key={renderKey} variant="simple">
<Thead>
<Tr>
<Th>
Expand Down

0 comments on commit 3757211

Please sign in to comment.