From 0370a958b4965aae6168763f67896488ea881882 Mon Sep 17 00:00:00 2001 From: fede Date: Mon, 11 May 2026 20:26:13 -0300 Subject: [PATCH] con este par de cambios ya tengo que muestra los grupos --- front/src/app.html | 2 +- front/src/lib/api.ts | 3 +- front/src/lib/components/Card.svelte | 11 +++-- front/src/lib/components/CardError.svelte | 9 ++++ front/src/lib/hooks/ObtenerGruposDeUsuario.ts | 25 +++++++++- front/src/lib/hooks/login.ts | 11 +++-- .../{updateUser.ts => modificarUsuario.ts} | 0 front/src/routes/+page.svelte | 47 ++++++++++++------- front/src/types.d.ts | 18 ++++++- 9 files changed, 98 insertions(+), 28 deletions(-) create mode 100644 front/src/lib/components/CardError.svelte rename front/src/lib/hooks/{updateUser.ts => modificarUsuario.ts} (100%) diff --git a/front/src/app.html b/front/src/app.html index a65c569..6a2bb58 100644 --- a/front/src/app.html +++ b/front/src/app.html @@ -6,7 +6,7 @@ %sveltekit.head% - +
%sveltekit.body%
diff --git a/front/src/lib/api.ts b/front/src/lib/api.ts index cfd652d..845ab4f 100644 --- a/front/src/lib/api.ts +++ b/front/src/lib/api.ts @@ -2,7 +2,8 @@ export { login } from './hooks/login'; export { register } from './hooks/register'; export { obtenerUsuarios } from './hooks/obtenerUsuarios'; export { obtenerUsuarioPorId } from './hooks/obtenerUsuarioPorId'; -export { modificarUsuario } from './hooks/updateUser'; +export { modificarUsuario } from './hooks/modificarUsuario'; export { borrarUsuario } from './hooks/borrarUsuario'; +export { ObtenerGruposDeUsuario } from './hooks/ObtenerGruposDeUsuario'; export { setToken, removeToken, getToken } from './hooks/token'; export { getCurrentUser, setCurrentUser, removeCurrentUser } from './hooks/user'; diff --git a/front/src/lib/components/Card.svelte b/front/src/lib/components/Card.svelte index eb28c5c..8f4fd7a 100644 --- a/front/src/lib/components/Card.svelte +++ b/front/src/lib/components/Card.svelte @@ -4,12 +4,13 @@ interface Props { title?: string; children: Snippet; + err?: Boolean; } - let { title, children }: Props = $props(); + let { title, children, err = false }: Props = $props(); -
+
{#if title}

{title}

{/if} @@ -32,4 +33,8 @@ font-size: 1.5rem; font-weight: 600; } - \ No newline at end of file + + .err { + background: rgba(255, 0, 0, 0.7) !important; + } + diff --git a/front/src/lib/components/CardError.svelte b/front/src/lib/components/CardError.svelte new file mode 100644 index 0000000..b3d8aba --- /dev/null +++ b/front/src/lib/components/CardError.svelte @@ -0,0 +1,9 @@ + + + + {@render children()} + diff --git a/front/src/lib/hooks/ObtenerGruposDeUsuario.ts b/front/src/lib/hooks/ObtenerGruposDeUsuario.ts index 95988ed..275dcaf 100644 --- a/front/src/lib/hooks/ObtenerGruposDeUsuario.ts +++ b/front/src/lib/hooks/ObtenerGruposDeUsuario.ts @@ -1,3 +1,24 @@ -import type { Usuario } from '../../types'; +import type { Usuario, GruposYPermisosResponse } from '../../types'; +import { API_URL } from '../env'; +import { getToken } from './token'; -export async function ObtenerGruposDeUsuario(usu: Usuario) {} +async function handleResponse(response: Response): Promise { + if (!response.ok) { + const error = await response.json().catch(() => ({ message: 'Error desconocido' })); + throw new Error(error.message || error.error || 'Error en la petición'); + } + return response.json(); +} + +export async function ObtenerGruposDeUsuario(usu: Usuario): Promise { + const response = await fetch(`${API_URL}/usuarios/grupos-permisos`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${getToken()}` + }, + + body: JSON.stringify({ email: usu.email }) + }); + return handleResponse(response); +} diff --git a/front/src/lib/hooks/login.ts b/front/src/lib/hooks/login.ts index 2069f14..59f2b21 100644 --- a/front/src/lib/hooks/login.ts +++ b/front/src/lib/hooks/login.ts @@ -17,9 +17,14 @@ export async function login(credentials: LoginRequest): Promise { headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(credentials) }); - const data = await handleResponse(response); - setAuthTokens(data); - setCurrentUser({ email: credentials.email, name: credentials.email[0] }); + const data = await handleResponse(response); + setAuthTokens({ + access_token: data.access_token, + expires_in: data.expires_in, + refresh_token: data.refresh_token, + token_type: data.token_type + }); + setCurrentUser({ id: data.id, email: credentials.email, name: credentials.email[0] }); setToken(data.access_token); return data; } diff --git a/front/src/lib/hooks/updateUser.ts b/front/src/lib/hooks/modificarUsuario.ts similarity index 100% rename from front/src/lib/hooks/updateUser.ts rename to front/src/lib/hooks/modificarUsuario.ts diff --git a/front/src/routes/+page.svelte b/front/src/routes/+page.svelte index 08fc262..07eecd0 100644 --- a/front/src/routes/+page.svelte +++ b/front/src/routes/+page.svelte @@ -1,10 +1,13 @@
@@ -15,20 +18,30 @@

Tu tienda de repuestos de confianza

-
- {#await grupos} -

- Cargando Grupos - -

- {:then gg} - {#each gg as g} - -

Catálogo completo

-

Explorá nuestro amplio catálogo de repuestos para todas las marcas.

-
- {/each} - {/await} +
+
+ {#await grupos} + + {:then gg} + + {#each gg.groups as g} + + {#if g.permisos?.length == 0} +

No hay permisos en este grupo

+ {:else} + {#each g.permisos as p} + {p.nombre} + {/each} + {/if} +
+ {/each} +
+ {:catch err} + + {err} + + {/await} +
diff --git a/front/src/types.d.ts b/front/src/types.d.ts index b95935d..b607e4d 100644 --- a/front/src/types.d.ts +++ b/front/src/types.d.ts @@ -3,7 +3,23 @@ export interface Usuario { name: string; } -export interface Grupo {} +export interface Grupo { + id: number; + nombre: string; + permisos?: Permiso[]; + grupos?: Grupo[]; + usuarios?: Usuario[]; +} + +export interface Permiso { + id: number; + nombre: string; +} + +export interface GruposYPermisosResponse { + email: string; + groups: Grupo[]; +} export interface LoginRequest { email: string;