eliminacion y cracion de registros reactivos
This commit is contained in:
@@ -3,7 +3,7 @@ import type { UnitConversion } from '$lib/api/dashboard/a76/general_catalogs/uni
|
||||
import { renderComponent } from '$lib/components/ui/data-table';
|
||||
import DataTableActions from './data-table-actions.svelte';
|
||||
export function createColumns(
|
||||
onSuccess?: () => void,
|
||||
onSuccess?: (item?: UnitConversion) => void,
|
||||
{ canEdit = true, canDelete = true }: { canEdit?: boolean; canDelete?: boolean } = {}
|
||||
): ColumnDef<UnitConversion>[] {
|
||||
return [
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
item?: UnitConversion | null;
|
||||
conversion?: UnitConversion | null;
|
||||
mode?: 'create' | 'edit';
|
||||
onSuccess?: () => void;
|
||||
onSuccess?: (item: UnitConversion) => void;
|
||||
} = $props();
|
||||
|
||||
// Atajos
|
||||
@@ -90,14 +90,15 @@
|
||||
throw new Error('El factor de conversión debe ser un número válido');
|
||||
}
|
||||
|
||||
let saved: UnitConversion;
|
||||
if (isEdit && currentConversion) {
|
||||
await updateUnitConversion(currentConversion.id, dataToSend, companyId);
|
||||
saved = await updateUnitConversion(currentConversion.id, dataToSend, companyId);
|
||||
} else {
|
||||
await createUnitConversion(dataToSend, companyId);
|
||||
saved = await createUnitConversion(dataToSend, companyId);
|
||||
}
|
||||
|
||||
open = false;
|
||||
if (onSuccess) onSuccess();
|
||||
if (onSuccess) onSuccess(saved);
|
||||
} catch (e) {
|
||||
error = e instanceof Error ? e.message : 'Error al guardar';
|
||||
} finally {
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
canDelete = true
|
||||
}: {
|
||||
conversion: UnitConversion;
|
||||
onSuccess?: () => void;
|
||||
onSuccess?: (item?: UnitConversion, isDelete?: boolean) => void;
|
||||
canEdit?: boolean;
|
||||
canDelete?: boolean;
|
||||
} = $props();
|
||||
@@ -40,7 +40,7 @@
|
||||
await deleteUnitConversion(conversion.id, companyStore.activeCompany.id);
|
||||
toast.success('Conversión eliminada');
|
||||
if (onSuccess) {
|
||||
onSuccess();
|
||||
onSuccess(conversion, true);
|
||||
}
|
||||
} catch (e) {
|
||||
const errorMsg = e instanceof Error ? e.message : 'Error desconocido al eliminar el registro';
|
||||
@@ -86,5 +86,5 @@
|
||||
bind:open={dialogOpen}
|
||||
conversion={conversion}
|
||||
mode="edit"
|
||||
{onSuccess}
|
||||
onSuccess={(saved) => onSuccess?.(saved)}
|
||||
/>
|
||||
|
||||
@@ -155,11 +155,11 @@ loading = false;
|
||||
|
||||
async function handleDelete() {
|
||||
if (!selectedItem || !canDelete || !companyStore.activeCompany) return;
|
||||
if (confirm('¿Eliminar esta conversión?')) {
|
||||
const itemToDelete = selectedItem;
|
||||
if (confirm(`¿Eliminar la conversión "${itemToDelete.from_unit_code} → ${itemToDelete.to_unit_code}"?`)) {
|
||||
try {
|
||||
await unitConversionsApi.delete(selectedItem.id, companyStore.activeCompany.id);
|
||||
selectedIds = [];
|
||||
reloadData();
|
||||
await unitConversionsApi.delete(itemToDelete.id, companyStore.activeCompany.id);
|
||||
handleTableSuccess(itemToDelete, true);
|
||||
} catch (err) {
|
||||
error = 'Error al eliminar';
|
||||
}
|
||||
@@ -191,14 +191,31 @@ loading = false;
|
||||
}
|
||||
}
|
||||
|
||||
function handleSuccess() {
|
||||
function handleSuccess(saved: UnitConversion) {
|
||||
createDialogOpen = false;
|
||||
editingItem = null;
|
||||
selectedIds = [];
|
||||
reloadData();
|
||||
if (editingItem) {
|
||||
allItems = allItems.map((i: UnitConversion) => (i.id === saved.id ? saved : i));
|
||||
} else {
|
||||
allItems = [saved, ...allItems];
|
||||
totalItems += 1;
|
||||
}
|
||||
editingItem = null;
|
||||
}
|
||||
|
||||
const columns = $derived(createColumns(handleSuccess, { canEdit, canDelete }));
|
||||
function handleTableSuccess(item?: UnitConversion, isDelete?: boolean) {
|
||||
if (isDelete && item) {
|
||||
allItems = allItems.filter((i: UnitConversion) => i.id !== item.id);
|
||||
totalItems = Math.max(0, totalItems - 1);
|
||||
selectedIds = [];
|
||||
} else if (item) {
|
||||
allItems = allItems.map((i: UnitConversion) => (i.id === item.id ? item : i));
|
||||
} else {
|
||||
reloadData();
|
||||
}
|
||||
}
|
||||
|
||||
const columns = $derived(createColumns(handleTableSuccess, { canEdit, canDelete }));
|
||||
</script>
|
||||
|
||||
<div class="flex h-[calc(100svh-4rem)] flex-col gap-6 overflow-hidden p-6 group-has-data-[collapsible=icon]/sidebar-wrapper:h-[calc(100svh-3rem)]">
|
||||
|
||||
Reference in New Issue
Block a user