Private
Public Access
todas las cosas para tener lo de los grupos funcional
This commit is contained in:
1 parent
f3edc5ceb2
commit
ad0612c4d8
16 files changed
+668
-112
No files matched your search
@@ -13,5 +13,10 @@ export { obtenerGrupos } from './hooks/obtenerGrupos';
|
||||
export { crearGrupo } from './hooks/crearGrupo';
|
||||
export { actualizarGrupo } from './hooks/actualizarGrupo';
|
||||
export { eliminarGrupo } from './hooks/eliminarGrupo';
|
||||
export { obtenerGrupo } from './hooks/obtenerGrupo';
|
||||
export { asignarPermisoAGrupo } from './hooks/asignarPermisoAGrupo';
|
||||
export { quitarPermisoAGrupo } from './hooks/quitarPermisoAGrupo';
|
||||
export { agregarSubGrupo } from './hooks/agregarSubGrupo';
|
||||
export { quitarSubGrupo } from './hooks/quitarSubGrupo';
|
||||
export { setToken, removeToken, getToken } from './hooks/token';
|
||||
export { getCurrentUser, setCurrentUser, removeCurrentUser } from './hooks/user';
|
||||
@@ -1,18 +1,48 @@
|
||||
<script lang="ts">
|
||||
import {
|
||||
Breadcrumb,
|
||||
Header as CarbonHeader,
|
||||
HeaderNav,
|
||||
HeaderNavItem,
|
||||
HeaderUtilities
|
||||
} from 'carbon-components-svelte';
|
||||
import UserMenu from './UserMenu.svelte';
|
||||
import type { GruposYPermisosResponse, Usuario } from '../../types';
|
||||
import { getCurrentUser, ObtenerGruposDeUsuario } from '$lib/api';
|
||||
import { onMount } from 'svelte';
|
||||
|
||||
interface Prop {
|
||||
usuario?: Usuario;
|
||||
grupos?: Promise<GruposYPermisosResponse> | {};
|
||||
}
|
||||
|
||||
let { usuario, grupos }: Prop = $props();
|
||||
|
||||
onMount(() => {
|
||||
if (typeof usuario === 'undefined') {
|
||||
usuario = getCurrentUser();
|
||||
}
|
||||
if (typeof grupos === 'undefined') {
|
||||
grupos = usuario ? ObtenerGruposDeUsuario(usuario) : Promise.resolve({});
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<CarbonHeader companyName="Repuestera" href="/">
|
||||
<HeaderNav>
|
||||
<HeaderNavItem text="Inicio" href="/" />
|
||||
<HeaderNavItem text="About" href="/about" />
|
||||
<HeaderNavItem text="Contact" href="/contact" />
|
||||
{#if !usuario}
|
||||
<HeaderNavItem text="Inicio" href="/" />
|
||||
<HeaderNavItem text="About" href="/about" />
|
||||
<HeaderNavItem text="Contact" href="/contact" />
|
||||
{:else}
|
||||
{#await grupos}
|
||||
<Breadcrumb noTrailingSlash skeleton count={3} />
|
||||
{:then g}
|
||||
{#each g.groups as gg}
|
||||
<HeaderNavItem text={gg.nombre} href="/grupos/{gg.id}" />
|
||||
{/each}
|
||||
{/await}
|
||||
{/if}
|
||||
</HeaderNav>
|
||||
<HeaderUtilities>
|
||||
<div class="flex h-full items-center">
|
||||
|
||||
@@ -22,41 +22,43 @@
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="relative">
|
||||
{#if user}
|
||||
<div class="relative">
|
||||
<button
|
||||
type="button"
|
||||
class="flex h-8 w-auto items-center gap-1 rounded-full px-3 text-white transition-colors hover:bg-white/10"
|
||||
onclick={toggleDropdown}
|
||||
aria-expanded={dropdownOpen}
|
||||
>
|
||||
<div class="flex h-6 w-6 items-center justify-center rounded-full bg-white/20">
|
||||
<span class="text-xs font-medium select-none"
|
||||
>{(user?.name?.[0] || user?.email?.[0])?.toUpperCase()}</span
|
||||
>
|
||||
</div>
|
||||
<ChevronDown size={16} />
|
||||
</button>
|
||||
{#if dropdownOpen}
|
||||
<div
|
||||
class="bg-grey-800 absolute top-full right-0 z-50 mt-1 overflow-hidden border shadow-lg"
|
||||
role="menu"
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
class="flex w-full items-center gap-2 px-4 py-2.5 text-sm text-gray-300 transition-colors hover:bg-gray-700"
|
||||
role="menuitem"
|
||||
onclick={handleLogout}
|
||||
>
|
||||
<Logout size={16} />
|
||||
Cerrar Sesion
|
||||
</button>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
class="flex h-8 w-auto items-center gap-1 rounded-full px-3 text-white transition-colors hover:bg-white/10"
|
||||
onclick={toggleDropdown}
|
||||
aria-expanded={dropdownOpen}
|
||||
>
|
||||
<div class="flex h-6 w-6 items-center justify-center rounded-full bg-white/20">
|
||||
<span class="text-xs font-medium select-none"
|
||||
>{(user?.name?.[0] || user?.email?.[0])?.toUpperCase()}</span
|
||||
>
|
||||
</div>
|
||||
<ChevronDown size={16} />
|
||||
</button>
|
||||
{#if dropdownOpen}
|
||||
<div
|
||||
class="bg-grey-800 absolute top-full right-0 z-50 mt-1 overflow-hidden border shadow-lg"
|
||||
role="menu"
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
class="flex w-full items-center gap-2 px-4 py-2.5 text-sm text-gray-300 transition-colors hover:bg-gray-700"
|
||||
role="menuitem"
|
||||
onclick={handleLogout}
|
||||
>
|
||||
<Logout size={16} />
|
||||
Cerrar Sesion
|
||||
</button>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
class="fixed inset-0 z-40 cursor-default bg-black/0"
|
||||
class:hidden={!dropdownOpen}
|
||||
onclick={closeDropdown}
|
||||
aria-label="Cerrar menu"
|
||||
></button>
|
||||
class="fixed inset-0 z-40 cursor-default bg-black/0"
|
||||
class:hidden={!dropdownOpen}
|
||||
onclick={closeDropdown}
|
||||
aria-label="Cerrar menu"
|
||||
></button>
|
||||
{/if}
|
||||
@@ -2,13 +2,16 @@ import { goto } from '$app/navigation';
|
||||
import type { Usuario, GruposYPermisosResponse } from '../../types';
|
||||
import { API_URL } from '../env';
|
||||
import { getToken, removeToken, removeAuthTokens } from './token';
|
||||
import { removeCurrentUser } from './user';
|
||||
|
||||
async function handleResponse<T>(response: Response): Promise<T> {
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({ message: 'Error desconocido' }));
|
||||
console.log(error);
|
||||
if (error.error) {
|
||||
removeToken();
|
||||
removeAuthTokens();
|
||||
removeCurrentUser();
|
||||
goto('/');
|
||||
}
|
||||
throw new Error(error.message || error.error || 'Error en la petición');
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import { API_URL } from '../env';
|
||||
import { getToken } from './token';
|
||||
|
||||
export async function agregarSubGrupo(grupoId: number, childGroupId: number): Promise<void> {
|
||||
const response = await fetch(`${API_URL}/grupos/${grupoId}/subgrupos`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${getToken()}`
|
||||
},
|
||||
body: JSON.stringify({ child_group_id: childGroupId })
|
||||
});
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({ message: 'Error desconocido' }));
|
||||
throw new Error(error.message || 'Error al agregar subgrupo');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { API_URL } from '../env';
|
||||
import { getToken } from './token';
|
||||
|
||||
export async function asignarPermisoAGrupo(grupoId: number, permissionId: number): Promise<void> {
|
||||
const response = await fetch(`${API_URL}/grupos/${grupoId}/permisos`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${getToken()}`
|
||||
},
|
||||
body: JSON.stringify({ permission_id: permissionId })
|
||||
});
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({ message: 'Error desconocido' }));
|
||||
throw new Error(error.message || 'Error al asignar permiso');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import type { Grupo } 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.error || 'Error en la petición');
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function obtenerGrupo(id: number, fetch?: Function): Promise<Grupo> {
|
||||
if (!fetch) fetch = window.fetch;
|
||||
const response = await fetch(`${API_URL}/grupos/${id}`, {
|
||||
headers: { Authorization: `Bearer ${getToken()}` }
|
||||
});
|
||||
return handleResponse<Grupo>(response);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { API_URL } from '../env';
|
||||
import { getToken } from './token';
|
||||
|
||||
export async function quitarPermisoAGrupo(grupoId: number, permissionId: number): Promise<void> {
|
||||
const response = await fetch(`${API_URL}/grupos/${grupoId}/permisos`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${getToken()}`
|
||||
},
|
||||
body: JSON.stringify({ permission_id: permissionId })
|
||||
});
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({ message: 'Error desconocido' }));
|
||||
throw new Error(error.message || 'Error al quitar permiso');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { API_URL } from '../env';
|
||||
import { getToken } from './token';
|
||||
|
||||
export async function quitarSubGrupo(grupoId: number, childGroupId: number): Promise<void> {
|
||||
const response = await fetch(`${API_URL}/grupos/${grupoId}/subgrupos`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${getToken()}`
|
||||
},
|
||||
body: JSON.stringify({ child_group_id: childGroupId })
|
||||
});
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({ message: 'Error desconocido' }));
|
||||
throw new Error(error.message || 'Error al quitar subgrupo');
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user