mirror of
https://github.com/emailerfacu-spec/minix-front.git
synced 2026-04-01 13:10:44 -03:00
add UsersAdmin
This commit is contained in:
25
src/lib/hooks/UsuariosAdmin.ts
Normal file
25
src/lib/hooks/UsuariosAdmin.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { apiBase } from "@/stores/url";
|
||||
import { sesionStore } from "@/stores/usuario";
|
||||
import { redirect } from "@sveltejs/kit";
|
||||
import { get } from "svelte/store";
|
||||
import type { UserResponseDto } from "../../types";
|
||||
|
||||
export async function fetchUsuariosAdmin(page: number, limit: number) {
|
||||
let response = await fetch(get(apiBase) + `/api/admin/users?page=${page}&pageSize=${limit}`,{
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${get(sesionStore)?.accessToken}`
|
||||
}
|
||||
});
|
||||
|
||||
if (response.status === 401) {
|
||||
throw redirect(302, '/');
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
return { error: true };
|
||||
}
|
||||
const usuarios: UserResponseDto[] = await response.json();
|
||||
return { usuarios, error: false };
|
||||
}
|
||||
@@ -1,28 +1,18 @@
|
||||
import { apiBase } from '@/stores/url.js';
|
||||
import { sesionStore } from '@/stores/usuario';
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
import { get } from 'svelte/store';
|
||||
import type { UserResponseDto } from '../../../types.js';
|
||||
import type { PageLoad } from './$types.js';
|
||||
import { fetchUsuariosAdmin } from '@/hooks/UsuariosAdmin.js';
|
||||
|
||||
export const ssr = false;
|
||||
|
||||
export async function load({ depends, fetch }) {
|
||||
export const load: PageLoad = async ({ depends}) => {
|
||||
depends('admin:load');
|
||||
const response = await fetch(get(apiBase) + '/api/admin/users', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${get(sesionStore)?.accessToken}`
|
||||
}
|
||||
});
|
||||
if (response.status === 401) {
|
||||
throw redirect(302, '/');
|
||||
}
|
||||
if (!response.ok) {
|
||||
const result = await fetchUsuariosAdmin(1, 10);
|
||||
|
||||
if (result.error) {
|
||||
return { error: true };
|
||||
}
|
||||
|
||||
const usuarios: UserResponseDto[] = await response.json();
|
||||
|
||||
return { usuarios, error: false };
|
||||
return {
|
||||
usuarios: result.usuarios,
|
||||
error: false
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user