add user,teacher services
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 = () => {
|
||||
|
||||
|
||||
@@ -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 <div>Loading...</div>;
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return <div>Error: {error.message}</div>;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="crear-usuario-container">
|
||||
<form onSubmit={handleSubmit}>
|
||||
@@ -87,8 +72,8 @@ const MDUser = () => {
|
||||
<input
|
||||
type="text"
|
||||
id="nombreUsuario"
|
||||
value={nusuario}
|
||||
onChange={(e) => setNusuario(e.target.value)}
|
||||
value={usuario}
|
||||
onChange={(e) => setUsuario(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -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';
|
||||
|
||||
|
||||
@@ -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 };
|
||||
};
|
||||
}
|
||||
@@ -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 };
|
||||
};
|
||||
|
||||
|
||||
@@ -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 };
|
||||
};
|
||||
Reference in New Issue
Block a user