con este par de cambios ya tengo que muestra los grupos

This commit is contained in:
fede committed 2026-05-11 22:43:29 -03:00
1 parent 617583def5
commit 0370a958b4
9 files changed
+98 -28

No files matched your search

+1 -1
View File
@@ -6,7 +6,7 @@
<meta name="text-scale" content="scale" />
%sveltekit.head%
</head>
<body data-sveltekit-preload-data="hover" theme="g100">
<body data-sveltekit-preload-data="hover">
<div style="display: contents">%sveltekit.body%</div>
</body>
</html>
+2 -1
View File
@@ -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';
+8 -3
View File
@@ -4,12 +4,13 @@
interface Props {
title?: string;
children: Snippet;
err?: Boolean;
}
let { title, children }: Props = $props();
let { title, children, err = false }: Props = $props();
</script>
<div class="card">
<div class="card {err ? 'err' : ''}">
{#if title}
<h2 class="card-title">{title}</h2>
{/if}
@@ -32,4 +33,8 @@
font-size: 1.5rem;
font-weight: 600;
}
</style>
.err {
background: rgba(255, 0, 0, 0.7) !important;
}
</style>
@@ -0,0 +1,9 @@
<script>
import Card from './Card.svelte';
let { children } = $props();
</script>
<Card err={true}>
{@render children()}
</Card>
+23 -2
View File
@@ -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<T>(response: Response): Promise<T> {
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<GruposYPermisosResponse> {
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<GruposYPermisosResponse>(response);
}
+8 -3
View File
@@ -17,9 +17,14 @@ export async function login(credentials: LoginRequest): Promise<LoginResponse> {
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(credentials)
});
const data = await handleResponse<LoginResponse>(response);
setAuthTokens(data);
setCurrentUser({ email: credentials.email, name: credentials.email[0] });
const data = await handleResponse<LoginResponse & { id: number }>(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;
}
File renamed without changes.
+30 -17
View File
@@ -1,10 +1,13 @@
<script lang="ts">
import { getCurrentUser } from '$lib/api';
import { getCurrentUser, ObtenerGruposDeUsuario } from '$lib/api';
import CardError from '$lib/components/CardError.svelte';
import Header from '$lib/components/Header.svelte';
import { Loading, Tile } from 'carbon-components-svelte';
import { Accordion, AccordionItem, Loading, Tile } from 'carbon-components-svelte';
import type { Grupo } from '../types';
let usuario = getCurrentUser();
let grupos: Promise<Grupos> = ObtenerGruposDeUsuario(usuario);
let grupos = usuario ? ObtenerGruposDeUsuario(usuario) : Promise.resolve({ groups: [] });
</script>
<Header />
@@ -15,20 +18,30 @@
<p class="hero-subtitle">Tu tienda de repuestos de confianza</p>
</div>
<div class="flex justify-around gap-2">
{#await grupos}
<p>
Cargando Grupos
<Loading />
</p>
{:then gg}
{#each gg as g}
<Tile class="feature-tile">
<h3>Catálogo completo</h3>
<p>Explorá nuestro amplio catálogo de repuestos para todas las marcas.</p>
</Tile>
{/each}
{/await}
<div class="flex w-full justify-center gap-2">
<div class="w-[75%]">
{#await grupos}
<Accordion skeleton></Accordion>
{:then gg}
<Accordion>
{#each gg.groups as g}
<AccordionItem title={g.nombre}>
{#if g.permisos?.length == 0}
<p class="text-neutral-500">No hay permisos en este grupo</p>
{:else}
{#each g.permisos as p}
<a href={p.nombre}>{p.nombre}</a>
{/each}
{/if}
</AccordionItem>
{/each}
</Accordion>
{:catch err}
<CardError>
{err}
</CardError>
{/await}
</div>
</div>
</div>
+17 -1
View File
@@ -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;