Private
Public Access
feat: hecha interfaz y hooks para clientes
Signed-off-by: fede <federico.nicolas.polidoro@gmail.com>
This commit is contained in:
1 parent
1948f1f426
commit
d9b76701e9
10 files changed
+557
No files matched your search
@@ -0,0 +1,26 @@
|
||||
import { API_URL } from '$lib/env';
|
||||
import { getToken } from '../token';
|
||||
|
||||
export interface ActualizarClienteDto {
|
||||
nombre: string;
|
||||
cuit: string;
|
||||
email?: string;
|
||||
}
|
||||
|
||||
export async function actualizarCliente(id: number, dto: ActualizarClienteDto) {
|
||||
try {
|
||||
const req = await fetch(`${API_URL}/clientes/${id}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${getToken()}`
|
||||
},
|
||||
body: JSON.stringify(dto)
|
||||
});
|
||||
const data = await req.json();
|
||||
if (req.ok) return 'ok';
|
||||
return data.error ?? 'No se pudo actualizar el cliente';
|
||||
} catch {
|
||||
return 'No se pudo alcanzar el servidor';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { API_URL } from '$lib/env';
|
||||
import { getToken } from '../token';
|
||||
|
||||
export async function altaCliente(id: number) {
|
||||
try {
|
||||
const req = await fetch(`${API_URL}/clientes/${id}/alta`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
Authorization: `Bearer ${getToken()}`
|
||||
}
|
||||
});
|
||||
if (req.ok) return 'Ok';
|
||||
const data = await req.json();
|
||||
return data.error ?? 'No se pudo reactivar el cliente';
|
||||
} catch {
|
||||
return 'No se pudo alcanzar el servidor';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { API_URL } from '$lib/env';
|
||||
import { getToken } from '../token';
|
||||
|
||||
export interface NuevoCliente {
|
||||
nombre: string;
|
||||
cuit: string;
|
||||
email?: string;
|
||||
}
|
||||
|
||||
export async function crearCliente(dto: NuevoCliente) {
|
||||
try {
|
||||
const req = await fetch(`${API_URL}/clientes/`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${getToken()}`
|
||||
},
|
||||
body: JSON.stringify(dto)
|
||||
});
|
||||
const data = await req.json();
|
||||
if (req.ok) return 'ok';
|
||||
return data.error ?? 'No se pudo crear el cliente';
|
||||
} catch {
|
||||
return 'No se pudo alcanzar el servidor';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { API_URL } from '$lib/env';
|
||||
import { getToken } from '../token';
|
||||
|
||||
export async function eliminarCliente(id: number) {
|
||||
try {
|
||||
const req = await fetch(`${API_URL}/clientes/${id}`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
Authorization: `Bearer ${getToken()}`
|
||||
}
|
||||
});
|
||||
if (req.ok) return 'Ok';
|
||||
const data = await req.json();
|
||||
return data.error ?? 'No se pudo eliminar el cliente';
|
||||
} catch {
|
||||
return 'No se pudo alcanzar el servidor';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
<script lang="ts">
|
||||
import Header from "$lib/components/Header.svelte";
|
||||
import { Button, DataTableSkeleton, InlineNotification, Table, TableBody, TableCell, TableContainer, TableHead, TableHeader, TableRow } from "carbon-components-svelte";
|
||||
import { Add, ArrowUp, Edit, TrashCan } from "carbon-icons-svelte";
|
||||
import type { Cliente } from "../../types.js";
|
||||
import CrearClienteForm from './CrearClienteForm.svelte';
|
||||
import EditarClienteForm from './EditarClienteForm.svelte';
|
||||
import EliminarClienteForm from './EliminarClienteForm.svelte';
|
||||
import AltaClienteForm from './AltaClienteForm.svelte';
|
||||
|
||||
let { data }:{data:{clientes:Cliente[] }} = $props();
|
||||
|
||||
let openCrear = $state(false);
|
||||
let openAlta = $state(false);
|
||||
let openDelete = $state(false);
|
||||
let openEdit = $state(false);
|
||||
|
||||
let clienteSeleccionado = $state<Cliente | null>(null);
|
||||
let clienteAEliminar = $state<Cliente | null>(null);
|
||||
let clienteAAlta = $state<Cliente | null>(null);
|
||||
|
||||
let clientes = $derived<Cliente[]>(Array.isArray(data.clientes) ? data.clientes : []);
|
||||
|
||||
let errorCarga = $derived(
|
||||
data.clientes !== undefined && data.clientes !== null && !Array.isArray(data.clientes)
|
||||
? String(data.clientes)
|
||||
: ''
|
||||
);
|
||||
|
||||
const headers: { key: keyof Cliente; value: string }[] = [
|
||||
|
||||
{ key: 'ID', value: 'ID' },
|
||||
{ key: 'nombre', value: 'Nombre' },
|
||||
{ key: 'cuit', value: 'Cuit' },
|
||||
{ key: 'email', value: 'Email' },
|
||||
{ key: 'CreatedAt', value: 'Creado' },
|
||||
{ key: 'DeletedAt', value: 'Estado' }
|
||||
];
|
||||
|
||||
</script>
|
||||
|
||||
|
||||
<Header />
|
||||
<div class="page-container">
|
||||
<div class="flex justify-between">
|
||||
<h1 class="page-title">Gestión de Clientes</h1>
|
||||
<Button onclick={() => {openCrear = true}} icon={Add}>Nuevo Cliente</Button>
|
||||
</div>
|
||||
{#if errorCarga}
|
||||
<InlineNotification kind="error" title="Error" subtitle={errorCarga} hideCloseButton />
|
||||
{/if}
|
||||
{#if typeof data.clientes === "undefined" || data.clientes === null}
|
||||
<DataTableSkeleton />
|
||||
{:else if !Array.isArray(data.clientes)}
|
||||
<TableContainer title="Clientes">
|
||||
<Table>
|
||||
<TableHead>
|
||||
<TableRow>
|
||||
{#each headers as header}
|
||||
<TableHeader>{header.value}</TableHeader>
|
||||
{/each}
|
||||
<TableHeader>Acciones</TableHeader>
|
||||
</TableRow>
|
||||
</TableHead>
|
||||
<TableBody>
|
||||
<TableRow>
|
||||
<TableCell colspan={headers.length+1}>No se pudieron cargar los clientes</TableCell>
|
||||
</TableRow>
|
||||
</TableBody>
|
||||
</Table>
|
||||
</TableContainer>
|
||||
{:else if clientes.length === 0}
|
||||
<TableContainer title="Clientes">
|
||||
<Table>
|
||||
<TableHead>
|
||||
<TableRow>
|
||||
{#each headers as header}
|
||||
<TableHeader>{header.value}</TableHeader>
|
||||
{/each}
|
||||
<TableHeader>Acciones</TableHeader>
|
||||
</TableRow>
|
||||
</TableHead>
|
||||
<TableBody>
|
||||
<TableRow>
|
||||
<TableCell colspan={headers.length+1}>No hay clientes para mostrar registre uno</TableCell>
|
||||
</TableRow>
|
||||
</TableBody>
|
||||
</Table>
|
||||
</TableContainer>
|
||||
{:else}
|
||||
<TableContainer title="Listado de Clientes">
|
||||
<Table>
|
||||
<TableHead>
|
||||
<TableRow>
|
||||
{#each headers as header}
|
||||
<TableHeader>{header.value}</TableHeader>
|
||||
{/each}
|
||||
<TableHeader>Acciones</TableHeader>
|
||||
</TableRow>
|
||||
</TableHead>
|
||||
<TableBody>
|
||||
{#each clientes as cl}
|
||||
<TableRow>
|
||||
{#each headers as header}
|
||||
<TableCell>{header.key === 'DeletedAt' ? (cl.DeletedAt ? '✖' : '✔') : String(cl[header.key] ?? '-')}</TableCell>
|
||||
{/each}
|
||||
<TableCell>
|
||||
<Button kind="ghost" size="small" icon={Edit} onclick={() => { clienteSeleccionado = cl; openEdit = true; }}>Editar</Button>
|
||||
{#if cl.DeletedAt}
|
||||
<Button kind="ghost" size="small" icon={ArrowUp} onclick={() => { clienteAAlta = cl; openAlta = true; }}>Alta</Button>
|
||||
{:else}
|
||||
<Button kind="ghost" size="small" icon={TrashCan} onclick={() => { clienteAEliminar = cl; openDelete = true; }}>Eliminar</Button>
|
||||
{/if}
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
{/each}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</TableContainer>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<CrearClienteForm bind:open={openCrear} onClose={() => openCrear = false} />
|
||||
|
||||
<EditarClienteForm
|
||||
bind:open={openEdit}
|
||||
cliente={clienteSeleccionado}
|
||||
onClose={() => {
|
||||
openEdit = false;
|
||||
clienteSeleccionado = null;
|
||||
}}
|
||||
/>
|
||||
|
||||
<EliminarClienteForm
|
||||
bind:open={openDelete}
|
||||
cliente={clienteAEliminar}
|
||||
onClose={() => {
|
||||
openDelete = false;
|
||||
clienteAEliminar = null;
|
||||
}}
|
||||
/>
|
||||
|
||||
<AltaClienteForm
|
||||
bind:open={openAlta}
|
||||
cliente={clienteAAlta}
|
||||
onClose={() => {
|
||||
openAlta = false;
|
||||
clienteAAlta = null;
|
||||
}}
|
||||
/>
|
||||
@@ -0,0 +1,10 @@
|
||||
import { obtenerClientes } from "$lib/hooks/clientes/obtenerClientes";
|
||||
|
||||
export async function load({ fetch, depends }) {
|
||||
depends("clientes:load");
|
||||
let clientes = await obtenerClientes(fetch);
|
||||
|
||||
return {
|
||||
clientes: clientes
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<script lang="ts">
|
||||
import { InlineNotification, Modal } from 'carbon-components-svelte';
|
||||
import { altaCliente } from '$lib/hooks/clientes/altaCliente';
|
||||
import { invalidate } from '$app/navigation';
|
||||
import type { Cliente } from '../../types';
|
||||
|
||||
let {
|
||||
open = $bindable(),
|
||||
onClose,
|
||||
cliente
|
||||
}: {
|
||||
open?: boolean;
|
||||
onClose?: () => void;
|
||||
cliente?: Cliente | null;
|
||||
} = $props();
|
||||
|
||||
let error = $state('');
|
||||
|
||||
async function handleSubmit(e: Event) {
|
||||
e.preventDefault();
|
||||
error = '';
|
||||
if (!cliente?.ID) {
|
||||
error = 'No se pudo obtener el cliente. Recargá la página.';
|
||||
return;
|
||||
}
|
||||
const result = await altaCliente(cliente.ID);
|
||||
if (result !== 'Ok') {
|
||||
error = result;
|
||||
return;
|
||||
}
|
||||
await invalidate('clientes:load');
|
||||
handleClose();
|
||||
}
|
||||
|
||||
function handleClose() {
|
||||
error = '';
|
||||
onClose?.();
|
||||
}
|
||||
</script>
|
||||
|
||||
<form id="altaClienteForm" onsubmit={handleSubmit}>
|
||||
<Modal
|
||||
formId="altaClienteForm"
|
||||
bind:open
|
||||
size="lg"
|
||||
modalHeading="Reactivar cliente"
|
||||
primaryButtonText="Reactivar"
|
||||
secondaryButtonText="Cancelar"
|
||||
on:click:button--secondary={handleClose}
|
||||
>
|
||||
{#if error}
|
||||
<div class="mb-4">
|
||||
<InlineNotification kind="error" title="Error" subtitle={error} hideCloseButton />
|
||||
</div>
|
||||
{/if}
|
||||
<p>¿Está seguro de que desea reactivar este cliente?</p>
|
||||
</Modal>
|
||||
</form>
|
||||
@@ -0,0 +1,99 @@
|
||||
<script lang="ts">
|
||||
import { invalidate } from '$app/navigation';
|
||||
import { InlineNotification, Modal, TextInput } from 'carbon-components-svelte';
|
||||
import { crearCliente } from '$lib/hooks/clientes/crearCliente';
|
||||
|
||||
let {
|
||||
open = $bindable(),
|
||||
onClose
|
||||
}: {
|
||||
open?: boolean;
|
||||
onClose?: () => void;
|
||||
} = $props();
|
||||
|
||||
let nombre = $state('');
|
||||
let cuit = $state('');
|
||||
let email = $state('');
|
||||
let enviando = $state(false);
|
||||
let error = $state('');
|
||||
|
||||
$effect(() => {
|
||||
if (open) {
|
||||
nombre = '';
|
||||
cuit = '';
|
||||
email = '';
|
||||
error = '';
|
||||
enviando = false;
|
||||
}
|
||||
});
|
||||
|
||||
function handleClose() {
|
||||
onClose?.();
|
||||
}
|
||||
|
||||
async function enviar() {
|
||||
if (enviando) return;
|
||||
enviando = true;
|
||||
error = '';
|
||||
|
||||
if (nombre.trim() === '') {
|
||||
error = 'El nombre es obligatorio.';
|
||||
} else if (cuit.trim() === '') {
|
||||
error = 'El CUIT es obligatorio.';
|
||||
}
|
||||
|
||||
if (error === '') {
|
||||
const result = await crearCliente({
|
||||
nombre: nombre.trim(),
|
||||
cuit: cuit.trim(),
|
||||
email: email.trim()
|
||||
});
|
||||
if (result === 'ok') {
|
||||
await invalidate('clientes:load');
|
||||
handleClose();
|
||||
} else {
|
||||
error = result;
|
||||
}
|
||||
}
|
||||
|
||||
enviando = false;
|
||||
}
|
||||
</script>
|
||||
|
||||
<form
|
||||
id="crearClienteForm"
|
||||
onsubmit={(e) => {
|
||||
e.preventDefault();
|
||||
void enviar();
|
||||
}}
|
||||
>
|
||||
<Modal
|
||||
formId="crearClienteForm"
|
||||
bind:open
|
||||
size="lg"
|
||||
modalHeading="Registrar cliente"
|
||||
primaryButtonText="Registrar"
|
||||
primaryButtonDisabled={enviando}
|
||||
secondaryButtonText="Cancelar"
|
||||
on:click:button--secondary={handleClose}
|
||||
>
|
||||
{#if error}
|
||||
<div class="mb-4">
|
||||
<InlineNotification kind="error" title="Error" subtitle={error} hideCloseButton />
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<TextInput labelText="Nombre" bind:value={nombre} maxlength={40} required />
|
||||
<div class="mt-1 text-sm text-gray-500">{nombre.length}/40</div>
|
||||
|
||||
<div class="mt-4">
|
||||
<TextInput labelText="CUIT" bind:value={cuit} maxlength={11} required />
|
||||
<div class="mt-1 text-sm text-gray-500">{cuit.length}/11</div>
|
||||
</div>
|
||||
|
||||
<div class="mt-4">
|
||||
<TextInput labelText="Email" type="email" bind:value={email} maxlength={80} />
|
||||
<div class="mt-1 text-sm text-gray-500">{email.length}/80</div>
|
||||
</div>
|
||||
</Modal>
|
||||
</form>
|
||||
@@ -0,0 +1,94 @@
|
||||
<script lang="ts">
|
||||
import { InlineNotification, Modal, TextInput } from 'carbon-components-svelte';
|
||||
import type { Cliente } from '../../types';
|
||||
import { actualizarCliente } from '$lib/hooks/clientes/actualizarCliente';
|
||||
import { invalidate } from '$app/navigation';
|
||||
|
||||
let { open = $bindable(), onClose, cliente } = $props();
|
||||
|
||||
let error = $state('');
|
||||
let enviando = $state(false);
|
||||
let nombre = $state('');
|
||||
let cuit = $state('');
|
||||
let email = $state('');
|
||||
|
||||
$effect(() => {
|
||||
if (cliente) {
|
||||
nombre = cliente.nombre ?? '';
|
||||
cuit = cliente.cuit ?? '';
|
||||
email = cliente.email ?? '';
|
||||
}
|
||||
});
|
||||
|
||||
function handleClose() {
|
||||
error = '';
|
||||
enviando = false;
|
||||
onClose?.();
|
||||
}
|
||||
|
||||
async function enviar() {
|
||||
if (enviando) return;
|
||||
enviando = true;
|
||||
error = '';
|
||||
|
||||
if (nombre.trim() === '') {
|
||||
error = 'El nombre es obligatorio.';
|
||||
} else if (cuit.trim() === '') {
|
||||
error = 'El CUIT es obligatorio.';
|
||||
}
|
||||
|
||||
if (error === '' && cliente) {
|
||||
const result = await actualizarCliente(cliente.ID, {
|
||||
nombre: nombre.trim(),
|
||||
cuit: cuit.trim(),
|
||||
email: email.trim()
|
||||
});
|
||||
if (result === 'ok') {
|
||||
await invalidate('clientes:load');
|
||||
handleClose();
|
||||
} else {
|
||||
error = result;
|
||||
}
|
||||
}
|
||||
|
||||
enviando = false;
|
||||
}
|
||||
</script>
|
||||
|
||||
<form
|
||||
id="editarClienteForm"
|
||||
onsubmit={(e) => {
|
||||
e.preventDefault();
|
||||
void enviar();
|
||||
}}
|
||||
>
|
||||
<Modal
|
||||
formId="editarClienteForm"
|
||||
bind:open
|
||||
size="lg"
|
||||
modalHeading="Editar cliente"
|
||||
primaryButtonText="Guardar"
|
||||
primaryButtonDisabled={enviando}
|
||||
secondaryButtonText="Cancelar"
|
||||
on:click:button--secondary={handleClose}
|
||||
>
|
||||
{#if error}
|
||||
<div class="mb-4">
|
||||
<InlineNotification kind="error" title="Error" subtitle={error} hideCloseButton />
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<TextInput labelText="Nombre" bind:value={nombre} maxlength={40} required />
|
||||
<div class="mt-1 text-sm text-gray-500">{nombre.length}/40</div>
|
||||
|
||||
<div class="mt-4">
|
||||
<TextInput labelText="CUIT" bind:value={cuit} maxlength={11} required />
|
||||
<div class="mt-1 text-sm text-gray-500">{cuit.length}/11</div>
|
||||
</div>
|
||||
|
||||
<div class="mt-4">
|
||||
<TextInput labelText="Email" type="email" bind:value={email} maxlength={80} />
|
||||
<div class="mt-1 text-sm text-gray-500">{email.length}/80</div>
|
||||
</div>
|
||||
</Modal>
|
||||
</form>
|
||||
@@ -0,0 +1,58 @@
|
||||
<script lang="ts">
|
||||
import { InlineNotification, Modal } from 'carbon-components-svelte';
|
||||
import { eliminarCliente } from '$lib/hooks/clientes/eliminarCliente';
|
||||
import { invalidate } from '$app/navigation';
|
||||
import type { Cliente } from '../../types';
|
||||
|
||||
let {
|
||||
open = $bindable(),
|
||||
onClose,
|
||||
cliente
|
||||
}: {
|
||||
open?: boolean;
|
||||
onClose?: () => void;
|
||||
cliente?: Cliente | null;
|
||||
} = $props();
|
||||
|
||||
let error = $state('');
|
||||
|
||||
async function handleSubmit(e: Event) {
|
||||
e.preventDefault();
|
||||
error = '';
|
||||
if (!cliente?.ID) {
|
||||
error = 'No se pudo obtener el cliente. Recargá la página.';
|
||||
return;
|
||||
}
|
||||
const result = await eliminarCliente(cliente.ID);
|
||||
if (result !== 'Ok') {
|
||||
error = result;
|
||||
return;
|
||||
}
|
||||
await invalidate('clientes:load');
|
||||
handleClose();
|
||||
}
|
||||
|
||||
function handleClose() {
|
||||
error = '';
|
||||
onClose?.();
|
||||
}
|
||||
</script>
|
||||
|
||||
<form id="eliminarClienteForm" onsubmit={handleSubmit}>
|
||||
<Modal
|
||||
formId="eliminarClienteForm"
|
||||
bind:open
|
||||
size="lg"
|
||||
modalHeading="Eliminar cliente"
|
||||
primaryButtonText="Eliminar"
|
||||
secondaryButtonText="Cancelar"
|
||||
on:click:button--secondary={handleClose}
|
||||
>
|
||||
{#if error}
|
||||
<div class="mb-4">
|
||||
<InlineNotification kind="error" title="Error" subtitle={error} hideCloseButton />
|
||||
</div>
|
||||
{/if}
|
||||
<p>¿Está seguro de que desea eliminar este cliente?</p>
|
||||
</Modal>
|
||||
</form>
|
||||
Reference in new issue
Block a user