Merge branch 'feature/catalogo-conversiones' into feature/catalog-importation
This commit is contained in:
@@ -76,6 +76,7 @@ export async function updateUnitConversion(
|
||||
}
|
||||
|
||||
export async function deleteUnitConversion(id: number, companyId: number): Promise<void> {
|
||||
const response = await api.delete(`/v1/a76/unit-conversions/${id}/?company_id=${companyId}`);
|
||||
// Backend DELETE route is defined without trailing slash: /unit-conversions/{id}
|
||||
const response = await api.delete(`/v1/a76/unit-conversions/${id}?company_id=${companyId}`);
|
||||
if (response.error) throw new Error(response.error);
|
||||
}
|
||||
@@ -3,6 +3,8 @@
|
||||
import * as Dialog from '$lib/components/ui/dialog';
|
||||
import { Input } from '$lib/components/ui/input';
|
||||
import { Label } from '$lib/components/ui/label';
|
||||
import UnitMeasureSelectorDialog from '$lib/components/dashboard/goods/modales/unit-measure-dialog.svelte';
|
||||
import { FolderSearch, Scale } from 'lucide-svelte';
|
||||
import {
|
||||
createUnitConversion,
|
||||
updateUnitConversion,
|
||||
@@ -36,6 +38,21 @@
|
||||
|
||||
let loading = $state(false);
|
||||
let error = $state<string | null>(null);
|
||||
let showFromUomModal = $state(false);
|
||||
let showToUomModal = $state(false);
|
||||
let wasOpen = $state(false);
|
||||
|
||||
function resetForm() {
|
||||
formData = {
|
||||
from_unit_code: '',
|
||||
to_unit_code: '',
|
||||
conversion_factor: ''
|
||||
};
|
||||
error = null;
|
||||
loading = false;
|
||||
showFromUomModal = false;
|
||||
showToUomModal = false;
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
if (conversion) {
|
||||
@@ -53,6 +70,14 @@
|
||||
}
|
||||
});
|
||||
|
||||
$effect(() => {
|
||||
// Reset each time the dialog is opened in create mode
|
||||
if (open && !wasOpen && !isEdit) {
|
||||
resetForm();
|
||||
}
|
||||
wasOpen = open;
|
||||
});
|
||||
|
||||
async function handleSubmit() {
|
||||
loading = true;
|
||||
error = null;
|
||||
@@ -114,24 +139,60 @@
|
||||
<div class="grid gap-4 py-4">
|
||||
<div class="grid gap-2">
|
||||
<Label for="from_unit_code">Código origen <span class="text-destructive">*</span></Label>
|
||||
<Input
|
||||
id="from_unit_code"
|
||||
bind:value={formData.from_unit_code}
|
||||
maxlength={5}
|
||||
disabled={loading}
|
||||
required
|
||||
/>
|
||||
<div class="flex gap-2">
|
||||
<div class="relative w-full">
|
||||
<Scale class="absolute top-2.5 left-3 h-4 w-4 text-muted-foreground" />
|
||||
<Input
|
||||
id="from_unit_code"
|
||||
bind:value={formData.from_unit_code}
|
||||
readonly
|
||||
onclick={() => (showFromUomModal = true)}
|
||||
class="cursor-pointer pl-9 font-mono"
|
||||
placeholder="Seleccione..."
|
||||
disabled={loading}
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="icon"
|
||||
type="button"
|
||||
onclick={() => (showFromUomModal = true)}
|
||||
disabled={loading}
|
||||
class="shrink-0"
|
||||
>
|
||||
<FolderSearch class="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="grid gap-2">
|
||||
<Label for="to_unit_code">Código destino <span class="text-destructive">*</span></Label>
|
||||
<Input
|
||||
id="to_unit_code"
|
||||
bind:value={formData.to_unit_code}
|
||||
maxlength={5}
|
||||
disabled={loading}
|
||||
required
|
||||
/>
|
||||
<div class="flex gap-2">
|
||||
<div class="relative w-full">
|
||||
<Scale class="absolute top-2.5 left-3 h-4 w-4 text-muted-foreground" />
|
||||
<Input
|
||||
id="to_unit_code"
|
||||
bind:value={formData.to_unit_code}
|
||||
readonly
|
||||
onclick={() => (showToUomModal = true)}
|
||||
class="cursor-pointer pl-9 font-mono"
|
||||
placeholder="Seleccione..."
|
||||
disabled={loading}
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="icon"
|
||||
type="button"
|
||||
onclick={() => (showToUomModal = true)}
|
||||
disabled={loading}
|
||||
class="shrink-0"
|
||||
>
|
||||
<FolderSearch class="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="grid gap-2">
|
||||
@@ -160,3 +221,17 @@
|
||||
</form>
|
||||
</Dialog.Content>
|
||||
</Dialog.Root>
|
||||
|
||||
<UnitMeasureSelectorDialog
|
||||
bind:open={showFromUomModal}
|
||||
onSelect={(u) => {
|
||||
formData.from_unit_code = u.code;
|
||||
}}
|
||||
/>
|
||||
|
||||
<UnitMeasureSelectorDialog
|
||||
bind:open={showToUomModal}
|
||||
onSelect={(u) => {
|
||||
formData.to_unit_code = u.code;
|
||||
}}
|
||||
/>
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
import { companyStore } from "$lib/stores/company.svelte";
|
||||
import { EllipsisVertical, Pencil, LoaderCircle, Trash2 } from 'lucide-svelte';
|
||||
import CreateEditDialog from "./create-edit-dialog.svelte";
|
||||
import { toast } from "svelte-sonner";
|
||||
|
||||
let {
|
||||
conversion,
|
||||
@@ -23,6 +24,9 @@
|
||||
}
|
||||
|
||||
if (!companyStore.activeCompany) {
|
||||
toast.error('Selecciona una compañía', {
|
||||
description: 'No se puede eliminar sin una compañía activa.'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -30,11 +34,13 @@
|
||||
|
||||
try {
|
||||
await deleteUnitConversion(conversion.id, companyStore.activeCompany.id);
|
||||
toast.success('Conversión eliminada');
|
||||
if (onSuccess) {
|
||||
onSuccess();
|
||||
}
|
||||
} catch (e) {
|
||||
const errorMsg = e instanceof Error ? e.message : 'Error desconocido al eliminar el registro';
|
||||
toast.error('No se pudo eliminar', { description: errorMsg });
|
||||
} finally {
|
||||
loading = false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user