diff --git a/src/components/CardProfesores/CardProfesor.js b/src/components/CardProfesores/CardProfesor.js
index b326d68..eca5adf 100644
--- a/src/components/CardProfesores/CardProfesor.js
+++ b/src/components/CardProfesores/CardProfesor.js
@@ -1,7 +1,7 @@
import React, { useState } from 'react';
import './CardProfesor.css';
import { PrivateRoutes, PublicRoutes } from '../../models/routes';
-import { useDeleteProfesor } from '../../services/auth.services';
+import { useDeleteProfesor } from '../../services/teacher.services';
const CardProfesor = ({ profesor, edificioNombre, permissions }) => {
const [showDialog, setShowDialog] = useState(false);
diff --git a/src/components/CardProfesores/Mapeo.js b/src/components/CardProfesores/Mapeo.js
index ffa0033..9916bd7 100644
--- a/src/components/CardProfesores/Mapeo.js
+++ b/src/components/CardProfesores/Mapeo.js
@@ -2,7 +2,8 @@ import React, { useState } from 'react';
import '../../App.css';
import CardProfesor from './CardProfesor';
import Guard from '../../guards/route.guard';
-import { useEdificio, useProfesores, useProfesoresActives } from '../../services/auth.services';
+import { useEdificio} from '../../services/auth.services';
+import { useProfesores, useProfesoresActives } from '../../services/teacher.services';
const Mapeo = ({ filters }) => {
const [currentPage, setCurrentPage] = useState(1);
diff --git a/src/components/CrearProfesor/FormProfesor.js b/src/components/CrearProfesor/FormProfesor.js
index 1706454..aee1b51 100644
--- a/src/components/CrearProfesor/FormProfesor.js
+++ b/src/components/CrearProfesor/FormProfesor.js
@@ -1,7 +1,8 @@
import React, { useState, useCallback } from 'react';
import { useDropzone } from 'react-dropzone';
import './FormProfesor.css';
-import { useAdscripcion, useCategoria, useEdificio, useRegisterTeacher } from '../../services/auth.services';
+import { useAdscripcion, useCategoria, useEdificio } from '../../services/auth.services';
+import { useRegisterTeacher } from '../../services/teacher.services';
const FormProfesor = () => {
const [page, setPage] = useState(1);
diff --git a/src/components/CrearUsuario/CrearUsuario.js b/src/components/CrearUsuario/CrearUsuario.js
index cdd2eb7..c759dd1 100644
--- a/src/components/CrearUsuario/CrearUsuario.js
+++ b/src/components/CrearUsuario/CrearUsuario.js
@@ -1,6 +1,6 @@
import React, { useState } from 'react';
import './CrearUsuario.css';
-import { useRegisterUser } from '../../services/auth.services';
+import { useRegisterUser } from '../../services/user.services';
const CrearUsuario = () => {
diff --git a/src/components/ModificarUsuario/MDUser.js b/src/components/ModificarUsuario/MDUser.js
index 5267589..6afd50a 100644
--- a/src/components/ModificarUsuario/MDUser.js
+++ b/src/components/ModificarUsuario/MDUser.js
@@ -2,73 +2,58 @@ import React, { useState, useEffect } from 'react';
import { useParams } from 'react-router-dom';
import './MDUser.css';
import { PrivateRoutes } from '../../models/routes';
+import { useGetUser, useUpdateUser } from '../../services/user.services';
const MDUser = () => {
const { id_usuario } = useParams();
+
+ const { user, loading, error } = useGetUser(id_usuario);
+ const { updateUser, loading: updateLoading, error: updateError } = useUpdateUser();
+
const [nombre, setNombre] = useState('');
- const [usuario, setUsuario] = useState(null);
- const [nusuario, setNusuario] = useState('');
+ const [usuario, setUsuario] = useState('');
const [correo, setCorreo] = useState('');
const [contraseña, setContraseña] = useState('');
const [id_tipo_usuario, setId_tipo_usuario] = useState('');
useEffect(() => {
- fetch(`http://localhost:3000/auth/${id_usuario}`)
- .then(response => response.json())
- .then(data => {
- setUsuario(data)
- setNombre(data.nombre)
- setNusuario(data.usuario)
- setCorreo(data.correo)
- setId_tipo_usuario(data.id_tipo_usuario)
+ if (user) {
+ setNombre(user.nombre);
+ setUsuario(user.usuario);
+ setCorreo(user.correo);
+ setId_tipo_usuario(user.id_tipo_usuario);
+ }
+ }, [user]);
- console.log(data)
- })
- }, [id_usuario]);
-
- const handleSubmit = (e) => {
+ const handleSubmit = async (e) => {
e.preventDefault();
const data = {
- ...usuario,
+ ...user,
nombre,
- usuario:nusuario,
+ usuario,
correo,
contraseña,
id_tipo_usuario,
};
- console.log(data);
- const token = localStorage.getItem('Token');
-
- if (!token) {
- console.error('No tienes el token');
- return;
- }
-
- fetch(`http://localhost:3000/auth/${id_usuario}`, {
- method: 'PUT',
- headers: {
- 'Content-Type': 'application/json',
- Authorization: `Bearer ${token}`,
- },
- body: JSON.stringify(data),
- })
- .then(response => response.json())
- .then(data => {
- console.log('Success:', data);
+ try {
+ const updatedUser = await updateUser(id_usuario, data);
+ console.log('Success:', updatedUser);
window.location.href = PrivateRoutes.FILTERUSUARIO;
- })
- .catch((error) => {
- console.error('Error:', error);
- });
+ } catch (error) {
+ console.error('Error updating user:', error);
+ }
};
-
- if (!usuario) {
+ if (loading) {
return
Loading...
;
}
+ if (error) {
+ return Error: {error.message}
;
+ }
+
return (
diff --git a/src/components/PerfilProfesor/PerfilProfesor.js b/src/components/PerfilProfesor/PerfilProfesor.js
index f1672c7..6d719ae 100644
--- a/src/components/PerfilProfesor/PerfilProfesor.js
+++ b/src/components/PerfilProfesor/PerfilProfesor.js
@@ -3,7 +3,8 @@ import './PerfilProfesor.css';
import { useParams } from 'react-router-dom';
import { PrivateRoutes } from '../../models/routes';
import Guard from '../../guards/route.guard';
-import { useAdscripcion, useCategoria, useDeleteProfesor, useEdificio } from '../../services/auth.services';
+import { useAdscripcion, useCategoria, useEdificio } from '../../services/auth.services';
+import { useDeleteProfesor } from '../../services/teacher.services';
const defaultImage = 'https://static.vecteezy.com/system/resources/thumbnails/020/765/399/small_2x/default-profile-account-unknown-icon-black-silhouette-free-vector.jpg';
diff --git a/src/services/auth.services.js b/src/services/auth.services.js
index 2491525..2c9f21d 100644
--- a/src/services/auth.services.js
+++ b/src/services/auth.services.js
@@ -1,5 +1,4 @@
import { useState, useEffect } from "react";
-import { PrivateRoutes, PublicRoutes } from "../models/routes";
export const useAdscripcion = () => {
const [adscripciones, setAdscripciones] = useState([]);
@@ -38,185 +37,4 @@ export const useCategoria = () => {
}, []);
return categorias;
-}
-
-export const useDeleteProfesor = () => {
- const [loading, setLoading] = useState(false);
- const [error, setError] = useState(null);
-
- const deleteProfesor = async (id_profesor) => {
- setLoading(true);
- setError(null);
-
- try {
- const token = localStorage.getItem('Token');
-
- if (!token) {
- throw new Error('No tienes el token');
- }
-
- const response = await fetch(`http://localhost:3000/profesor/${id_profesor}`, {
- method: 'DELETE',
- headers: {
- Authorization: `Bearer ${token}`,
- },
- });
-
- if (!response.ok) {
- throw new Error('Error al eliminar el profesor');
- }
-
- const data = await response.json();
- console.log('Success:', data);
- return data;
- } catch (error) {
- console.error('Error:', error);
- setError(error.message);
- } finally {
- setLoading(false);
- }
- };
-
- return { deleteProfesor, loading, error };
-};
-
-export const useProfesoresActives = (currentPage, filters) => {
- const [profesores, setProfesores] = useState([]);
- const [totalPages, setTotalPages] = useState(1);
-
- useEffect(() => {
- const fetchProfesores = () => {
- const query = new URLSearchParams({
- page: currentPage,
- limit: 10,
- activo:true,
- ...filters
- }).toString();
-
- fetch(`http://localhost:3000/profesor/page?${query}`)
- .then(response => {
- if (!response.ok) {
- throw new Error('Network response was not ok');
- }
- return response.json();
- })
- .then(data => {
- setProfesores(data.profesores);
- setTotalPages(data.totalPages);
- })
- .catch(error => {
- console.error('Error fetching the data: ', error);
- });
- };
-
- fetchProfesores();
- }, [currentPage, filters]);
-
- return { profesores, totalPages };
-};
-
-export const useProfesores = (currentPage, filters) => {
- const [profesores, setProfesores] = useState([]);
- const [totalPages, setTotalPages] = useState(1);
-
- useEffect(() => {
- const fetchProfesores = () => {
- const query = new URLSearchParams({
- page: currentPage,
- limit: 10,
- ...filters
- }).toString();
-
- fetch(`http://localhost:3000/profesor/page?${query}`)
- .then(response => {
- if (!response.ok) {
- throw new Error('Network response was not ok');
- }
- return response.json();
- })
- .then(data => {
- setProfesores(data.profesores);
- setTotalPages(data.totalPages);
- })
- .catch(error => {
- console.error('Error fetching the data: ', error);
- });
- };
-
- fetchProfesores();
- }, [currentPage, filters]);
-
- return { profesores, totalPages };
-};
-
-export const useRegisterUser = () => {
- const registerUser = (userData) => {
- const token = localStorage.getItem('Token');
-
- if (!token) {
- console.error('No tienes el token');
- return Promise.reject(new Error('No token available'));
- }
-
- return fetch('http://localhost:3000/auth/register', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- Authorization: `Bearer ${token}`,
- },
- body: JSON.stringify(userData),
- })
- .then(response => {
- if (!response.ok) {
- throw new Error('Network response was not ok');
- }
- return response.json();
- })
- .then(data => {
- console.log('Success:', data);
- window.location.href = PrivateRoutes.FILTERUSUARIO;
- })
- .catch((error) => {
- console.error('Error:', error);
- throw error;
- });
- };
-
- return { registerUser };
-};
-
-export const useRegisterTeacher = () => {
- const registerTeacher = (teacherData) => {
- const token = localStorage.getItem('Token');
-
- if (!token) {
- console.error('No tienes el token');
- return Promise.reject(new Error('No token available'));
- }
-
- return fetch('http://localhost:3000/profesor', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- Authorization: `Bearer ${token}`,
- },
- body: JSON.stringify(teacherData),
- })
- .then(response => {
- if (!response.ok) {
- throw new Error('Network response was not ok');
- }
- return response.json();
- })
- .then(data => {
- console.log('Success:', data);
- window.location.href = PublicRoutes.FILTER;
- })
- .catch((error) => {
- console.error('Error:', error);
- throw error;
- });
- };
-
- return { registerTeacher };
-};
\ No newline at end of file
+}
\ No newline at end of file
diff --git a/src/services/teacher.services.js b/src/services/teacher.services.js
new file mode 100644
index 0000000..3a7015e
--- /dev/null
+++ b/src/services/teacher.services.js
@@ -0,0 +1,149 @@
+import { useState, useEffect } from "react";
+import { PrivateRoutes, PublicRoutes } from "../models/routes";
+
+export const useDeleteProfesor = () => {
+ const [loading, setLoading] = useState(false);
+ const [error, setError] = useState(null);
+
+ const deleteProfesor = async (id_profesor) => {
+ setLoading(true);
+ setError(null);
+
+ try {
+ const token = localStorage.getItem('Token');
+
+ if (!token) {
+ throw new Error('No tienes el token');
+ }
+
+ const response = await fetch(`http://localhost:3000/profesor/${id_profesor}`, {
+ method: 'DELETE',
+ headers: {
+ Authorization: `Bearer ${token}`,
+ },
+ });
+
+ if (!response.ok) {
+ throw new Error('Error al eliminar el profesor');
+ }
+
+ const data = await response.json();
+ console.log('Success:', data);
+ return data;
+ } catch (error) {
+ console.error('Error:', error);
+ setError(error.message);
+ } finally {
+ setLoading(false);
+ }
+ };
+
+ return { deleteProfesor, loading, error };
+ };
+
+ export const useProfesoresActives = (currentPage, filters) => {
+ const [profesores, setProfesores] = useState([]);
+ const [totalPages, setTotalPages] = useState(1);
+
+ useEffect(() => {
+ const fetchProfesores = () => {
+ const query = new URLSearchParams({
+ page: currentPage,
+ limit: 10,
+ activo:true,
+ ...filters
+ }).toString();
+
+ fetch(`http://localhost:3000/profesor/page?${query}`)
+ .then(response => {
+ if (!response.ok) {
+ throw new Error('Network response was not ok');
+ }
+ return response.json();
+ })
+ .then(data => {
+ setProfesores(data.profesores);
+ setTotalPages(data.totalPages);
+ })
+ .catch(error => {
+ console.error('Error fetching the data: ', error);
+ });
+ };
+
+ fetchProfesores();
+ }, [currentPage, filters]);
+
+ return { profesores, totalPages };
+ };
+
+ export const useProfesores = (currentPage, filters) => {
+ const [profesores, setProfesores] = useState([]);
+ const [totalPages, setTotalPages] = useState(1);
+
+ useEffect(() => {
+ const fetchProfesores = () => {
+ const query = new URLSearchParams({
+ page: currentPage,
+ limit: 10,
+ ...filters
+ }).toString();
+
+ fetch(`http://localhost:3000/profesor/page?${query}`)
+ .then(response => {
+ if (!response.ok) {
+ throw new Error('Network response was not ok');
+ }
+ return response.json();
+ })
+ .then(data => {
+ setProfesores(data.profesores);
+ setTotalPages(data.totalPages);
+ })
+ .catch(error => {
+ console.error('Error fetching the data: ', error);
+ });
+ };
+
+ fetchProfesores();
+ }, [currentPage, filters]);
+
+ return { profesores, totalPages };
+ };
+
+ export const useRegisterTeacher = () => {
+ const registerTeacher = (teacherData) => {
+ const token = localStorage.getItem('Token');
+
+ if (!token) {
+ console.error('No tienes el token');
+ return Promise.reject(new Error('No token available'));
+ }
+
+ return fetch('http://localhost:3000/profesor', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ Authorization: `Bearer ${token}`,
+ },
+ body: JSON.stringify(teacherData),
+ })
+ .then(response => {
+ if (!response.ok) {
+ throw new Error('Network response was not ok');
+ }
+ return response.json();
+ })
+ .then(data => {
+ console.log('Success:', data);
+ window.location.href = PublicRoutes.FILTER;
+ })
+ .catch((error) => {
+ console.error('Error:', error);
+ throw error;
+ });
+ };
+
+ return { registerTeacher };
+ };
+
+
\ No newline at end of file
diff --git a/src/services/user.services.js b/src/services/user.services.js
new file mode 100644
index 0000000..b3e5b39
--- /dev/null
+++ b/src/services/user.services.js
@@ -0,0 +1,103 @@
+import { useState, useEffect } from "react";
+import { PrivateRoutes, PublicRoutes } from "../models/routes";
+
+export const useRegisterUser = () => {
+ const registerUser = (userData) => {
+ const token = localStorage.getItem('Token');
+
+ if (!token) {
+ console.error('No tienes el token');
+ return Promise.reject(new Error('No token available'));
+ }
+
+ return fetch('http://localhost:3000/auth/register', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ Authorization: `Bearer ${token}`,
+ },
+ body: JSON.stringify(userData),
+ })
+ .then(response => {
+ if (!response.ok) {
+ throw new Error('Network response was not ok');
+ }
+ return response.json();
+ })
+ .then(data => {
+ console.log('Success:', data);
+ window.location.href = PrivateRoutes.FILTERUSUARIO;
+ })
+ .catch((error) => {
+ console.error('Error:', error);
+ throw error;
+ });
+ };
+
+ return { registerUser };
+};
+
+export const useGetUser = (id_usuario) => {
+ const [user, setUser] = useState(null);
+ const [loading, setLoading] = useState(true);
+ const [error, setError] = useState(null);
+
+ useEffect(() => {
+ const fetchUser = async () => {
+ try {
+ const response = await fetch(`http://localhost:3000/auth/${id_usuario}`);
+ if (!response.ok) {
+ throw new Error('Network response was not ok');
+ }
+ const data = await response.json();
+ setUser(data);
+ } catch (error) {
+ setError(error);
+ } finally {
+ setLoading(false);
+ }
+ };
+
+ fetchUser();
+ }, [id_usuario]);
+
+ return { user, loading, error };
+};
+
+export const useUpdateUser = () => {
+ const [loading, setLoading] = useState(false);
+ const [error, setError] = useState(null);
+
+ const updateUser = async (id_usuario, data) => {
+ try {
+ setLoading(true);
+ const token = localStorage.getItem('Token');
+ if (!token) {
+ throw new Error('No tienes el token');
+ }
+
+ const response = await fetch(`http://localhost:3000/auth/${id_usuario}`, {
+ method: 'PUT',
+ headers: {
+ 'Content-Type': 'application/json',
+ Authorization: `Bearer ${token}`,
+ },
+ body: JSON.stringify(data),
+ });
+
+ if (!response.ok) {
+ throw new Error('Network response was not ok');
+ }
+
+ const responseData = await response.json();
+ return responseData;
+ } catch (error) {
+ setError(error);
+ throw error;
+ } finally {
+ setLoading(false);
+ }
+ };
+
+ return { updateUser, loading, error };
+};
\ No newline at end of file