Private
Public Access
feat: añadido crear y eliminar usuarios
This commit is contained in:
1 parent
3d8883cac6
commit
69395cc6d7
3 files changed
+82
-3
No files matched your search
@@ -0,0 +1,28 @@
|
||||
import { invalidate } from '$app/navigation';
|
||||
import { API_URL } from '$lib/env';
|
||||
import type { Usuario } from '../../types';
|
||||
import { getToken } from './token';
|
||||
|
||||
export async function crearUsuario(dto: Partial<Usuario & { password?: string }>) {
|
||||
if (!dto.password || dto.password?.length <= 8 || !dto.email || !dto.name) return 'Faltan Datos';
|
||||
console.log(dto);
|
||||
try {
|
||||
const req = await fetch(`${API_URL}/usuarios`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${getToken()}`
|
||||
},
|
||||
body: JSON.stringify(dto)
|
||||
});
|
||||
|
||||
let data = await req.json();
|
||||
if (!req.ok) {
|
||||
return data.error || 'Error';
|
||||
}
|
||||
invalidate('usuarios:load');
|
||||
return 'Ok';
|
||||
} catch {
|
||||
return 'No se pudo conectar con el servidor';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { invalidate } from '$app/navigation';
|
||||
import { API_URL } from '$lib/env';
|
||||
import { getToken } from './token';
|
||||
|
||||
export async function eliminarUsuario(id: number) {
|
||||
try {
|
||||
const req = await fetch(`${API_URL}/usuarios/${id}`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
Authorization: `Bearer ${getToken()}`
|
||||
}
|
||||
});
|
||||
|
||||
let data = await req.json();
|
||||
if (!req.ok) {
|
||||
return data.error || 'Error';
|
||||
}
|
||||
invalidate('usuarios:load');
|
||||
return 'Ok';
|
||||
} catch {
|
||||
return 'No se pudo conectar con el servidor';
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
<script lang="ts">
|
||||
import Header from '$lib/components/Header.svelte';
|
||||
import { actualizarUsuario } from '$lib/hooks/actualizarUsuario.js';
|
||||
import { crearUsuario } from '$lib/hooks/crearUsuario.js';
|
||||
import { eliminarUsuario } from '$lib/hooks/eliminarUsuario.js';
|
||||
import {
|
||||
Button,
|
||||
Modal,
|
||||
@@ -25,8 +27,9 @@
|
||||
let showCambiarContraseñaModal = $state(false);
|
||||
let showError: string | Promise<string> = $state('');
|
||||
|
||||
let usuarioCrear = $state({ name: '', email: '', password: '' });
|
||||
let usuarioModificar = $state({ name: '', email: '' });
|
||||
let usuarioEliminar = $state();
|
||||
let usuarioEliminar = $state({ id: 0 });
|
||||
let usuarioCambiarContraseña = $state();
|
||||
|
||||
function cerrarModal() {
|
||||
@@ -128,6 +131,9 @@
|
||||
id="crearForm"
|
||||
onsubmit={(e) => {
|
||||
e.preventDefault();
|
||||
showError = crearUsuario(usuarioCrear);
|
||||
showCrearModal = false;
|
||||
usuarioCrear = { name: '', email: '', password: '' };
|
||||
}}
|
||||
>
|
||||
<Modal
|
||||
@@ -137,7 +143,21 @@
|
||||
primaryButtonText="Crear"
|
||||
secondaryButtonText="Cerrar"
|
||||
on:click:button--secondary={cerrarModal}
|
||||
></Modal>
|
||||
>
|
||||
<TextInput bind:value={usuarioCrear.name} labelText="Nombre" placeholder="Tu nombre" />
|
||||
<TextInput
|
||||
bind:value={usuarioCrear.email}
|
||||
labelText="Email"
|
||||
placeholder="tu@email.com"
|
||||
type="email"
|
||||
/>
|
||||
<PasswordInput
|
||||
bind:value={usuarioCrear.password}
|
||||
labelText="Contraseña"
|
||||
placeholder="••••••••"
|
||||
minlength={8}
|
||||
/>
|
||||
</Modal>
|
||||
</form>
|
||||
<form
|
||||
id="editarForm"
|
||||
@@ -163,6 +183,12 @@
|
||||
id="eliminarForm"
|
||||
onsubmit={(e) => {
|
||||
e.preventDefault();
|
||||
if (!usuarioEliminar && !usuarioEliminar.id) {
|
||||
showError = 'No se pudo obtener la id del usuario? recarga la pagina.';
|
||||
} else {
|
||||
showError = eliminarUsuario(usuarioEliminar.id);
|
||||
}
|
||||
showEliminarModal = false;
|
||||
}}
|
||||
>
|
||||
<Modal
|
||||
@@ -172,7 +198,9 @@
|
||||
primaryButtonText="Eliminar"
|
||||
secondaryButtonText="Cerrar"
|
||||
on:click:button--secondary={cerrarModal}
|
||||
></Modal>
|
||||
>
|
||||
Esta seguro que quiere borrar este usuario?
|
||||
</Modal>
|
||||
</form>
|
||||
<form
|
||||
id="cambiarForm"
|
||||
|
||||
Reference in new issue
Block a user