diff --git a/front/src/lib/api.ts b/front/src/lib/api.ts index 845ab4f..3d1e18d 100644 --- a/front/src/lib/api.ts +++ b/front/src/lib/api.ts @@ -5,5 +5,9 @@ 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'; +export { crearPermiso } from './hooks/crearPermiso'; +export { actualizarPermiso } from './hooks/actualizarPermiso'; +export { eliminarPermiso } from './hooks/eliminarPermiso'; export { setToken, removeToken, getToken } from './hooks/token'; export { getCurrentUser, setCurrentUser, removeCurrentUser } from './hooks/user'; diff --git a/front/src/lib/hooks/actualizarPermiso.ts b/front/src/lib/hooks/actualizarPermiso.ts new file mode 100644 index 0000000..0660607 --- /dev/null +++ b/front/src/lib/hooks/actualizarPermiso.ts @@ -0,0 +1,23 @@ +import type { Permiso } from '../../types'; +import { API_URL } from '../env'; +import { getToken } from './token'; + +async function handleResponse(response: Response): Promise { + 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 actualizarPermiso(id: number, data: Partial): Promise { + const response = await fetch(`${API_URL}/permisos/${id}`, { + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${getToken()}` + }, + body: JSON.stringify(data) + }); + return handleResponse(response); +} diff --git a/front/src/lib/hooks/crearPermiso.ts b/front/src/lib/hooks/crearPermiso.ts new file mode 100644 index 0000000..239e191 --- /dev/null +++ b/front/src/lib/hooks/crearPermiso.ts @@ -0,0 +1,23 @@ +import type { Permiso } from '../../types'; +import { API_URL } from '../env'; +import { getToken } from './token'; + +async function handleResponse(response: Response): Promise { + 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 crearPermiso(data: Partial): Promise { + const response = await fetch(`${API_URL}/permisos`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${getToken()}` + }, + body: JSON.stringify(data) + }); + return handleResponse(response); +} diff --git a/front/src/lib/hooks/eliminarPermiso.ts b/front/src/lib/hooks/eliminarPermiso.ts new file mode 100644 index 0000000..8953b5e --- /dev/null +++ b/front/src/lib/hooks/eliminarPermiso.ts @@ -0,0 +1,10 @@ +import { API_URL } from '../env'; +import { getToken } from './token'; + +export async function eliminarPermiso(id: number): Promise { + const response = await fetch(`${API_URL}/permisos/${id}`, { + method: 'DELETE', + headers: { Authorization: `Bearer ${getToken()}` } + }); + if (!response.ok) throw new Error('Error al eliminar permiso'); +} diff --git a/front/src/lib/hooks/obtenerPermisos.ts b/front/src/lib/hooks/obtenerPermisos.ts new file mode 100644 index 0000000..8f7062a --- /dev/null +++ b/front/src/lib/hooks/obtenerPermisos.ts @@ -0,0 +1,18 @@ +import type { Permiso } from '../../types'; +import { API_URL } from '../env'; +import { getToken } from './token'; + +async function handleResponse(response: Response): Promise { + 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 obtenerPermisos(): Promise { + const response = await fetch(`${API_URL}/permisos`, { + headers: { Authorization: `Bearer ${getToken()}` } + }); + return handleResponse(response); +} diff --git a/front/src/lib/hooks/token.ts b/front/src/lib/hooks/token.ts index 1bd7e93..b6bc78c 100644 --- a/front/src/lib/hooks/token.ts +++ b/front/src/lib/hooks/token.ts @@ -42,4 +42,4 @@ export function removeAuthTokens(): void { if (typeof window !== 'undefined') { localStorage.removeItem('auth'); } -} \ No newline at end of file +} diff --git a/front/src/routes/permisos/+page.svelte b/front/src/routes/permisos/+page.svelte new file mode 100644 index 0000000..f286238 --- /dev/null +++ b/front/src/routes/permisos/+page.svelte @@ -0,0 +1,286 @@ + + +
+ +
+
+

Gestión de Permisos

+ +
+ + {#if error} + + {/if} + + {#if loading} + + {:else} + + + + + + {#each headers as header} + {header.value} + {/each} + Acciones + + + + {#each permisos as p (p.id)} + + {#each headers as header} + {String(p[header.key] ?? '-')} + {/each} + + + + + + {/each} + +
+
+
+ {/if} +
+ +
+ + + +