feature/refactorizacion-tabla-sector

This commit is contained in:
hreyes
2026-03-17 11:21:28 -06:00
parent 20a351c475
commit fbbe4370de
32 changed files with 4167 additions and 3947 deletions

View File

@@ -4,9 +4,12 @@ import { createRawSnippet } from "svelte";
import DataTableActions from "./data-table-actions.svelte";
export type Sector = {
id: number;
key: string;
description: string;
authorized: number;
authorized: boolean;
company_id: number;
tenant_id: number;
};
export function createColumns(onSuccess?: () => void): ColumnDef<Sector>[] {
@@ -42,9 +45,9 @@ export function createColumns(onSuccess?: () => void): ColumnDef<Sector>[] {
accessorKey: "authorized",
header: "Autorizado",
cell: ({ row }) => {
const authSnippet = createRawSnippet<[{ authorized: number }]>((getAuth) => {
const authSnippet = createRawSnippet<[{ authorized: boolean }]>((getAuth) => {
const { authorized } = getAuth();
const badge = authorized === 1
const badge = authorized
? '<span class="inline-flex items-center rounded-full border px-2.5 py-0.5 text-xs font-semibold transition-colors bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200">Sí</span>'
: '<span class="inline-flex items-center rounded-full border px-2.5 py-0.5 text-xs font-semibold transition-colors bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-200">No</span>';
return {

View File

@@ -4,6 +4,7 @@
import { Input } from "$lib/components/ui/input";
import { Label } from "$lib/components/ui/label";
import { sectorsApi, type Sector, type CreateSectorData, type UpdateSectorData } from "$lib/api/dashboard/reference_data/sectors";
import { companyStore } from '$lib/stores/company.svelte';
let {
open = $bindable(false),
@@ -18,7 +19,7 @@
let formData = $state({
key: "",
description: "",
authorized: 0
authorized: false
});
let loading = $state(false);
@@ -36,7 +37,7 @@
formData = {
key: "",
description: "",
authorized: 0
authorized: false
};
}
});
@@ -48,6 +49,13 @@
loading = true;
error = null;
const companyId = companyStore.activeCompany?.id;
if (!companyId) {
error = 'No hay empresa activa seleccionada';
loading = false;
return;
}
try {
let response;
if (isEditing && item) {
@@ -56,14 +64,14 @@
description: formData.description,
authorized: formData.authorized
};
response = await sectorsApi.update(item.key, payload);
response = await sectorsApi.update(item.id, payload, companyId);
} else {
const payload: CreateSectorData = {
key: formData.key,
description: formData.description,
authorized: formData.authorized
};
response = await sectorsApi.create(payload);
response = await sectorsApi.create(payload, companyId);
}
if (response.error) {
@@ -95,11 +103,10 @@
function handleOpenChange(newOpen: boolean) {
if (!newOpen) {
// Limpiar form al cerrar
formData = {
key: "",
description: "",
authorized: 0
authorized: false
};
error = null;
}
@@ -159,9 +166,9 @@
<input
type="radio"
name="authorized"
value="1"
checked={formData.authorized === 1}
onchange={() => (formData.authorized = 1)}
value="true"
checked={formData.authorized === true}
onchange={() => (formData.authorized = true)}
disabled={loading}
class="h-4 w-4 border-gray-300 text-primary focus:ring-primary"
/>
@@ -171,16 +178,15 @@
<input
type="radio"
name="authorized"
value="0"
checked={formData.authorized === 0}
onchange={() => (formData.authorized = 0)}
value="false"
checked={formData.authorized === false}
onchange={() => (formData.authorized = false)}
disabled={loading}
class="h-4 w-4 border-gray-300 text-primary focus:ring-primary"
/>
<span class="text-sm">No</span>
</label>
</div>
<p class="text-sm text-muted-foreground">1 = Autorizado, 0 = No autorizado</p>
</div>
<Dialog.Footer>

View File

@@ -3,6 +3,7 @@
import * as AlertDialog from "$lib/components/ui/alert-dialog";
import { Badge } from "$lib/components/ui/badge";
import { sectorsApi, type Sector } from "$lib/api/dashboard/reference_data/sectors";
import { companyStore } from '$lib/stores/company.svelte';
let {
open = $bindable(false),
@@ -20,11 +21,17 @@
async function handleDelete() {
if (!item) return;
const companyId = companyStore.activeCompany?.id;
if (!companyId) {
error = 'No hay empresa activa seleccionada';
return;
}
loading = true;
error = null;
try {
const response = await sectorsApi.delete(item.key);
const response = await sectorsApi.delete(item.id, companyId);
if (response.error) {
error = response.error;
@@ -70,9 +77,9 @@
</div>
<div class="flex items-center justify-between text-sm">
<span class="font-medium">Autorizado:</span>
<Badge variant={item.authorized === 1 ? "default" : "destructive"}>
{item.authorized === 1 ? "Sí" : "No"}
</Badge>
<Badge variant={item.authorized ? "default" : "destructive"}>
{item.authorized ? "Sí" : "No"}
</Badge>
</div>
</div>
{/if}

View File

@@ -50,9 +50,9 @@
<div class="space-y-2">
<div class="flex items-center justify-between">
<span class="text-sm font-medium text-muted-foreground">Autorizado</span>
<Badge variant={item.authorized === 1 ? "default" : "destructive"}>
{item.authorized === 1 ? "Sí" : "No"}
</Badge>
<Badge variant={item.authorized ? "default" : "destructive"}>
{item.authorized ? "Sí" : "No"}
</Badge>
</div>
<Separator />
</div>