Private
Public Access
añadida funcionalidad para dar de baja y restaurar marcas dadas de baja
This commit is contained in:
1 parent
478fe331aa
commit
884fd6ff24
9 files changed
+206
-17
No files matched your search
@@ -0,0 +1,20 @@
|
||||
import { API_URL } from '$lib/env';
|
||||
import { getToken } from '../token';
|
||||
|
||||
export async function AltaMarca(id: number) {
|
||||
try {
|
||||
const req = await fetch(`${API_URL}/marcas/${id}/alta`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
Authorization: `Bearer ${getToken()}`
|
||||
}
|
||||
});
|
||||
if (req.ok) {
|
||||
return 'ok';
|
||||
}
|
||||
const data = await req.json();
|
||||
return data.error;
|
||||
} catch {
|
||||
return 'no se pudo alcanzar el servidor';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { API_URL } from '$lib/env';
|
||||
import { getToken } from '../token';
|
||||
|
||||
export async function eliminarMarca(id: number) {
|
||||
try {
|
||||
const req = await fetch(`${API_URL}/marcas/${id}`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
Authorization: `Bearer ${getToken()}`
|
||||
}
|
||||
});
|
||||
if (req.ok) {
|
||||
return 'Ok';
|
||||
}
|
||||
const data = await req.json();
|
||||
return data.error;
|
||||
} catch {
|
||||
return 'no se pudo alcanzar el servidor';
|
||||
}
|
||||
}
|
||||
@@ -5,12 +5,13 @@
|
||||
import type { Marca } from '../../types';
|
||||
import CrearMarcaForm from './CrearMarcaForm.svelte';
|
||||
import EditarMarcaForm from './EditarMarcaForm.svelte';
|
||||
import EliminarMarcaForm from './EliminarMarcaForm.svelte';
|
||||
import AltaMarcaForm from './AltaMarcaForm.svelte';
|
||||
|
||||
let error = $state("");
|
||||
|
||||
let { data }: {data: {marcas: string | Array<Marca>}} = $props();
|
||||
|
||||
// Ensure we have an array of Marca for iteration
|
||||
let marcas = $derived(Array.isArray(data.marcas) ? data.marcas : []);
|
||||
|
||||
$effect(()=>{
|
||||
@@ -19,7 +20,7 @@
|
||||
}
|
||||
});
|
||||
|
||||
const headers = [
|
||||
const headers: { key: keyof Marca; value: string }[] = [
|
||||
{ key: 'ID', value: 'ID' },
|
||||
{ key: 'nombre', value: 'Nombre' },
|
||||
{ key: 'CreatedAt', value: 'Creado' },
|
||||
@@ -29,7 +30,13 @@
|
||||
|
||||
let openCrear = $state(false);
|
||||
let openEdit = $state(false);
|
||||
let openDelete = $state(false);
|
||||
let marcaSeleccionada: Marca | null = $state(null);
|
||||
let marcaAEliminar: Marca | null = $state(null);
|
||||
let openAlta = $state(false);
|
||||
let marcaAAlta: Marca | null = $state(null);
|
||||
|
||||
|
||||
</script>
|
||||
|
||||
<Header />
|
||||
@@ -76,14 +83,14 @@
|
||||
{#each marcas as p}
|
||||
<TableRow>
|
||||
{#each headers as header}
|
||||
<TableCell>{String(p[header.key] ?? '-')}</TableCell>
|
||||
<TableCell>{header.key === 'DeletedAt' ? (p.DeletedAt ? '✖' : '✔') : String(p[header.key] ?? '-')}</TableCell>
|
||||
{/each}
|
||||
<TableCell>
|
||||
<Button kind="ghost" size="small" icon={Edit} onclick={() => { marcaSeleccionada = p; openEdit = true; }}>Editar</Button>
|
||||
{#if p.DeletedAt}
|
||||
<Button kind="ghost" size="small" icon={ArrowUp} onclick={() => {}}>Alta</Button>
|
||||
<Button kind="ghost" size="small" icon={ArrowUp} onclick={() => { marcaAAlta = p; openAlta = true; }}>Alta</Button>
|
||||
{:else}
|
||||
<Button kind="ghost" size="small" icon={TrashCan} onclick={() => {}}>Eliminar</Button>
|
||||
<Button kind="ghost" size="small" icon={TrashCan} onclick={() => { marcaAEliminar = p; openDelete = true; }}>Eliminar</Button>
|
||||
{/if}
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
@@ -96,4 +103,7 @@
|
||||
|
||||
|
||||
<CrearMarcaForm bind:open={openCrear} onClose={() => openCrear = false} />
|
||||
<EditarMarcaForm bind:open={openEdit} marca={marcaSeleccionada} onClose={() => { openEdit = false; marcaSeleccionada = null; }} />
|
||||
|
||||
<EliminarMarcaForm bind:open={openDelete} marca={marcaAEliminar} onClose={() => { openDelete = false; marcaAEliminar = null; }} />
|
||||
|
||||
<AltaMarcaForm bind:open={openAlta} marca={marcaAAlta} onClose={()=> openAlta = false} />
|
||||
@@ -0,0 +1,80 @@
|
||||
<script lang="ts">
|
||||
import { Modal, InlineNotification } from 'carbon-components-svelte';
|
||||
import type { Marca } from '../../types';
|
||||
import { invalidate } from '$app/navigation';
|
||||
import { AltaMarca } from '$lib/hooks/marcas/altaMarca';
|
||||
|
||||
interface Props {
|
||||
open?: boolean;
|
||||
marca?: Marca | null;
|
||||
onClose: Function
|
||||
}
|
||||
let { open = $bindable(), marca = null, onClose}:Props = $props();
|
||||
|
||||
let error = $state('');
|
||||
let loading = $state(false);
|
||||
let success = $state(false);
|
||||
|
||||
function reset() {
|
||||
error = '';
|
||||
loading = false;
|
||||
success = false;
|
||||
}
|
||||
|
||||
function handleClose() {
|
||||
reset();
|
||||
onClose();
|
||||
}
|
||||
|
||||
async function altaMarca() {
|
||||
if (!marca) return;
|
||||
loading = true;
|
||||
error = '';
|
||||
success = false;
|
||||
try {
|
||||
const result = await AltaMarca(marca.ID);
|
||||
if (result === 'ok') {
|
||||
success = true;
|
||||
invalidate('marcas:load');
|
||||
// Close after success
|
||||
handleClose();
|
||||
} else {
|
||||
throw new Error(result);
|
||||
}
|
||||
} catch (e) {
|
||||
error = e instanceof Error ? e.message : String(e);
|
||||
} finally {
|
||||
loading = false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<form
|
||||
id="altaMarcaForm"
|
||||
onsubmit={async (e) => {
|
||||
e.preventDefault();
|
||||
if (loading) return;
|
||||
await altaMarca();
|
||||
}}
|
||||
>
|
||||
<Modal
|
||||
formId="altaMarcaForm"
|
||||
bind:open
|
||||
size="sm"
|
||||
modalHeading="Dar de alta marca"
|
||||
primaryButtonText="Dar de alta"
|
||||
primaryButtonDisabled={loading}
|
||||
secondaryButtonText="Cancelar"
|
||||
on:click:button--secondary={handleClose}
|
||||
>
|
||||
{#if error}
|
||||
<InlineNotification kind="error" title="Error" subtitle={error} hideCloseButton />
|
||||
{/if}
|
||||
{#if marca}
|
||||
<p>¿Dar de alta la marca <strong>{marca.nombre}</strong>?</p>
|
||||
{/if}
|
||||
{#if success}
|
||||
<InlineNotification kind="success" title="Éxito" subtitle="Marca dada de alta correctamente" hideCloseButton />
|
||||
{/if}
|
||||
</Modal>
|
||||
</form>
|
||||
@@ -4,7 +4,6 @@
|
||||
import { crearMarca } from '$lib/hooks/marcas/crearMarca';
|
||||
import { invalidate } from '$app/navigation';
|
||||
|
||||
// Props to control modal visibility
|
||||
let { open = $bindable(), onClose } = $props();
|
||||
|
||||
let error = $state('');
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
import { actualizarMarca } from '$lib/hooks/marcas/actualizarMarca';
|
||||
import { invalidate } from '$app/navigation';
|
||||
|
||||
// Props to control modal visibility and the marca to edit
|
||||
let { open = $bindable(), onClose, marca } = $props();
|
||||
|
||||
let error = $state('');
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
<script lang="ts">
|
||||
import { Modal } from 'carbon-components-svelte';
|
||||
import { eliminarMarca } from '$lib/hooks/marcas/eliminarMarca';
|
||||
import { invalidate } from '$app/navigation';
|
||||
import type { Marca } from '../../types';
|
||||
|
||||
let {
|
||||
open = $bindable(),
|
||||
onClose,
|
||||
marca
|
||||
}: { open?: boolean; onClose: () => void; marca: Marca} = $props();
|
||||
|
||||
let error = $state('');
|
||||
|
||||
async function handleSubmit(e: Event) {
|
||||
e.preventDefault();
|
||||
error = '';
|
||||
if (!marca?.ID) {
|
||||
error = 'No se pudo obtener la id de la Marca? recarga la página.';
|
||||
return;
|
||||
}
|
||||
const result = await eliminarMarca(marca.ID);
|
||||
if (result !== 'Ok') {
|
||||
error = result;
|
||||
return;
|
||||
}
|
||||
invalidate('marcas:load');
|
||||
handleClose();
|
||||
}
|
||||
|
||||
function handleClose() {
|
||||
error = '';
|
||||
onClose();
|
||||
}
|
||||
</script>
|
||||
|
||||
<form id="eliminarMarcaForm" onsubmit={handleSubmit}>
|
||||
<Modal
|
||||
formId="eliminarMarcaForm"
|
||||
bind:open={open}
|
||||
modalHeading="Eliminar Marca"
|
||||
primaryButtonText="Eliminar"
|
||||
secondaryButtonText="Cerrar"
|
||||
on:click:button--secondary={handleClose}
|
||||
>
|
||||
{#if error}
|
||||
<div class="text-red-500">{error}</div>
|
||||
{/if}
|
||||
¿Está seguro de que desea borrar esta Marca?
|
||||
</Modal>
|
||||
</form>
|
||||
Reference in new issue
Block a user