Private
Public Access
feat: hecho lo de actualizar usuario
This commit is contained in:
1 parent
d0949e2560
commit
3d8883cac6
7 files changed
+52
-36
No files matched your search
@@ -1,7 +1,6 @@
|
||||
export { login } from './hooks/login';
|
||||
export { register } from './hooks/register';
|
||||
export { obtenerUsuarioPorId } from './hooks/obtenerUsuarioPorId';
|
||||
export { modificarUsuario } from './hooks/modificarUsuario';
|
||||
export { borrarUsuario } from './hooks/borrarUsuario';
|
||||
export { ObtenerGruposDeUsuario } from './hooks/ObtenerGruposDeUsuario';
|
||||
export { obtenerPermisos } from './hooks/obtenerPermisos';
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import { invalidate } from '$app/navigation';
|
||||
import { API_URL } from '$lib/env';
|
||||
import type { Usuario } from '../../types';
|
||||
import { getToken } from './token';
|
||||
|
||||
export async function actualizarUsuario(dto: Partial<Usuario>) {
|
||||
if (dto.id <= 0 || !dto.email || !dto.name) return 'Faltan Datos';
|
||||
try {
|
||||
const req = await fetch(`${API_URL}/usuarios/${dto.id}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${getToken()}`
|
||||
},
|
||||
body: JSON.stringify(dto)
|
||||
});
|
||||
|
||||
let data = await req.json();
|
||||
if (!req.ok) {
|
||||
return data.error || 'Error';
|
||||
}
|
||||
invalidate('usuarios:load');
|
||||
return 'Ok';
|
||||
} catch {
|
||||
return 'No se pudo conectar con el servidor';
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
import type { Usuario } from '../../types';
|
||||
import { API_URL } from '../env';
|
||||
import { getToken } from './token';
|
||||
|
||||
async function handleResponse<T>(response: Response): Promise<T> {
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({ message: 'Error desconocido' }));
|
||||
throw new Error(error.message || 'Error en la petición');
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function modificarUsuario(
|
||||
id: number,
|
||||
data: Partial<{ email: string; name: string }>
|
||||
): Promise<Usuario> {
|
||||
const response = await fetch(`${API_URL}/users/${id}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${getToken()}`
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
return handleResponse<Usuario>(response);
|
||||
}
|
||||
Reference in new issue
Block a user