correcion de boton de eliminacion, reactividad para nuevos registros, estabilidad del formulario
This commit is contained in:
@@ -117,15 +117,23 @@
|
||||
if (fraction) {
|
||||
// Update
|
||||
const updateData: HistoricalFractionUpdate = baseData;
|
||||
await updateHistoricalFraction(companyId, fraction.id, updateData);
|
||||
const updateResponse = await updateHistoricalFraction(companyId, fraction.id, updateData);
|
||||
if (updateResponse.error || !updateResponse.data) {
|
||||
toast.error(updateResponse.error || 'Error al actualizar la fracción');
|
||||
return;
|
||||
}
|
||||
toast.success('Fracción actualizada correctamente');
|
||||
} else {
|
||||
// Create
|
||||
const createData: HistoricalFractionCreate = {
|
||||
...baseData,
|
||||
historical_fraction: historicalFractionCode // Required in create
|
||||
historical_fraction: historicalFractionCode
|
||||
};
|
||||
await createHistoricalFraction(companyId, createData);
|
||||
const createResponse = await createHistoricalFraction(companyId, createData);
|
||||
if (createResponse.error || !createResponse.data) {
|
||||
toast.error(createResponse.error || 'Error al crear la fracción');
|
||||
return;
|
||||
}
|
||||
toast.success('Fracción creada correctamente');
|
||||
}
|
||||
onSuccess();
|
||||
@@ -140,7 +148,7 @@
|
||||
</script>
|
||||
|
||||
<Dialog.Root bind:open>
|
||||
<Dialog.Content class="sm:max-w-[700px]">
|
||||
<Dialog.Content class="sm:max-w-[700px]" onInteractOutside={(e) => e.preventDefault()}>
|
||||
<Dialog.Header>
|
||||
<Dialog.Title>{fraction ? 'Editar' : 'Crear'} Fracción Histórica</Dialog.Title>
|
||||
<Dialog.Description>
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
import { Search, Loader2, Plus, Pencil, Trash2 } from 'lucide-svelte';
|
||||
import { toast } from 'svelte-sonner';
|
||||
import HistoricalFractionDialog from './HistoricalFractionDialog.svelte';
|
||||
import * as AlertDialog from '$lib/components/ui/alert-dialog';
|
||||
import { currentUser } from '$lib/auth';
|
||||
import ErrorState from '$lib/components/dashboard/common/error-state.svelte';
|
||||
import {
|
||||
@@ -55,6 +56,8 @@ let scrollContainer = $state<HTMLDivElement | null>(null);
|
||||
let dialogOpen = $state(false);
|
||||
let editingFraction = $state<HistoricalFraction | null>(null);
|
||||
let deletingFractionId = $state<number | null>(null);
|
||||
let showDeleteConfirm = $state(false);
|
||||
let fractionToDelete = $state<HistoricalFraction | null>(null);
|
||||
|
||||
async function loadFractions(reset = false) {
|
||||
const companyId = companyStore.activeCompany?.id;
|
||||
@@ -136,19 +139,27 @@ let scrollContainer = $state<HTMLDivElement | null>(null);
|
||||
dialogOpen = true;
|
||||
}
|
||||
|
||||
async function handleDelete(fraction: HistoricalFraction) {
|
||||
function confirmDelete(fraction: HistoricalFraction) {
|
||||
if (!canDelete) {
|
||||
toast.error('No tienes permiso para eliminar fracciones históricas');
|
||||
return;
|
||||
}
|
||||
fractionToDelete = fraction;
|
||||
showDeleteConfirm = true;
|
||||
}
|
||||
|
||||
async function handleDelete() {
|
||||
if (!canDelete || !fractionToDelete) return;
|
||||
const companyId = companyStore.activeCompany?.id;
|
||||
if (!companyId) return;
|
||||
|
||||
if (!confirm(`¿Estás seguro de eliminar la fracción ${fraction.historical_fraction}?`)) return;
|
||||
|
||||
try {
|
||||
deletingFractionId = fraction.id;
|
||||
await deleteHistoricalFraction(companyId, fraction.id);
|
||||
deletingFractionId = fractionToDelete.id;
|
||||
const response = await deleteHistoricalFraction(companyId, fractionToDelete.id);
|
||||
if (response.error) {
|
||||
toast.error(response.error || 'Error al eliminar la fracción');
|
||||
return;
|
||||
}
|
||||
toast.success('Fracción eliminada correctamente');
|
||||
loadFractions(true);
|
||||
} catch (error) {
|
||||
@@ -156,6 +167,8 @@ let scrollContainer = $state<HTMLDivElement | null>(null);
|
||||
toast.error('Error al eliminar la fracción');
|
||||
} finally {
|
||||
deletingFractionId = null;
|
||||
showDeleteConfirm = false;
|
||||
fractionToDelete = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -266,7 +279,7 @@ let scrollContainer = $state<HTMLDivElement | null>(null);
|
||||
</Table.Row>
|
||||
{:else}
|
||||
{#each fractions as fraction}
|
||||
<Table.Row class="catalog-table-row">
|
||||
<Table.Row class="catalog-table-row cursor-pointer" ondblclick={() => canEdit && handleEdit(fraction)}>
|
||||
<Table.Cell class="font-medium">{fraction.historical_fraction}</Table.Cell>
|
||||
<Table.Cell>{fraction.fraction_type || '-'}</Table.Cell>
|
||||
<Table.Cell>{fraction.unit_of_measure_code || '-'}</Table.Cell>
|
||||
@@ -288,7 +301,7 @@ let scrollContainer = $state<HTMLDivElement | null>(null);
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
class="h-8 w-8 text-destructive hover:text-destructive"
|
||||
onclick={() => handleDelete(fraction)}
|
||||
onclick={() => confirmDelete(fraction)}
|
||||
disabled={deletingFractionId === fraction.id}
|
||||
>
|
||||
{#if deletingFractionId === fraction.id}
|
||||
@@ -330,3 +343,26 @@ let scrollContainer = $state<HTMLDivElement | null>(null);
|
||||
/>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<AlertDialog.Root bind:open={showDeleteConfirm}>
|
||||
<AlertDialog.Content>
|
||||
<AlertDialog.Header>
|
||||
<AlertDialog.Title>¿Estás seguro?</AlertDialog.Title>
|
||||
<AlertDialog.Description>
|
||||
Esta acción no se puede deshacer. Se eliminará permanentemente la fracción <strong>{fractionToDelete?.historical_fraction}</strong>.
|
||||
</AlertDialog.Description>
|
||||
</AlertDialog.Header>
|
||||
<AlertDialog.Footer>
|
||||
<AlertDialog.Cancel>Cancelar</AlertDialog.Cancel>
|
||||
<AlertDialog.Action
|
||||
class="bg-destructive text-white hover:bg-destructive/90"
|
||||
onclick={handleDelete}
|
||||
>
|
||||
{#if deletingFractionId !== null}
|
||||
<Loader2 class="mr-2 h-4 w-4 animate-spin" />
|
||||
{/if}
|
||||
Eliminar
|
||||
</AlertDialog.Action>
|
||||
</AlertDialog.Footer>
|
||||
</AlertDialog.Content>
|
||||
</AlertDialog.Root>
|
||||
|
||||
Reference in New Issue
Block a user