Refactor API calls in dashboard components to include company ID for data fetching. Updated methods in reference data files to accept companyId as a parameter, ensuring accurate data retrieval based on the selected company. Enhanced user experience by preventing errors related to missing company context.
This commit is contained in:
@@ -36,12 +36,13 @@ export interface UpdateCodePedimentoRegimenData {
|
||||
export const codePedimentoRegimensApi = {
|
||||
/**
|
||||
* Lista todos los code pedimento regimens con paginación y búsqueda
|
||||
* @param companyId - ID de la empresa
|
||||
* @param page - Número de página (por defecto 1)
|
||||
* @param pageSize - Tamaño de página (por defecto 50)
|
||||
* @param search - Término de búsqueda (opcional)
|
||||
*/
|
||||
list: (page = 1, pageSize = 50, search?: string) => {
|
||||
let url = `/v1/public/reference_data/code-pedimento-regimens/?page=${page}&page_size=${pageSize}`;
|
||||
list: (companyId: number, page = 1, pageSize = 50, search?: string) => {
|
||||
let url = `/v1/public/reference_data/code-pedimento-regimens/?company_id=${companyId}&page=${page}&page_size=${pageSize}`;
|
||||
if (search) {
|
||||
url += `&search=${encodeURIComponent(search)}`;
|
||||
}
|
||||
|
||||
@@ -32,12 +32,13 @@ export interface UpdateContainerData {
|
||||
export const containersApi = {
|
||||
/**
|
||||
* Lista todos los containers con paginación y búsqueda
|
||||
* @param companyId - ID de la empresa
|
||||
* @param page - Número de página (por defecto 1)
|
||||
* @param pageSize - Tamaño de página (por defecto 50)
|
||||
* @param search - Término de búsqueda (opcional)
|
||||
*/
|
||||
list: (page = 1, pageSize = 50, search?: string) => {
|
||||
let url = `/v1/public/reference_data/containers/?page=${page}&page_size=${pageSize}`;
|
||||
list: (companyId: number, page = 1, pageSize = 50, search?: string) => {
|
||||
let url = `/v1/public/reference_data/containers/?company_id=${companyId}&page=${page}&page_size=${pageSize}`;
|
||||
if (search) {
|
||||
url += `&search=${encodeURIComponent(search)}`;
|
||||
}
|
||||
|
||||
@@ -35,12 +35,13 @@ export interface UpdateCurrencyTypeData {
|
||||
export const currencyTypesApi = {
|
||||
/**
|
||||
* Lista todos los tipos de moneda con paginación y búsqueda
|
||||
* @param companyId - ID de la empresa
|
||||
* @param page - Número de página (por defecto 1)
|
||||
* @param pageSize - Tamaño de página (por defecto 50)
|
||||
* @param search - Término de búsqueda (opcional)
|
||||
*/
|
||||
list: (page = 1, pageSize = 50, search?: string) => {
|
||||
let url = `/v1/public/reference_data/currency-types/?page=${page}&page_size=${pageSize}`;
|
||||
list: (companyId: number, page = 1, pageSize = 50, search?: string) => {
|
||||
let url = `/v1/public/reference_data/currency-types/?company_id=${companyId}&page=${page}&page_size=${pageSize}`;
|
||||
if (search) {
|
||||
url += `&search=${encodeURIComponent(search)}`;
|
||||
}
|
||||
|
||||
@@ -35,12 +35,13 @@ export interface UpdateCustomsWarehouseData {
|
||||
export const customsWarehousesApi = {
|
||||
/**
|
||||
* Lista todos los recintos fiscalizados con paginación y búsqueda
|
||||
* @param companyId - ID de la empresa
|
||||
* @param page - Número de página (por defecto 1)
|
||||
* @param pageSize - Tamaño de página (por defecto 50)
|
||||
* @param search - Término de búsqueda (opcional)
|
||||
*/
|
||||
list: (page = 1, pageSize = 50, search?: string) => {
|
||||
let url = `/v1/public/reference_data/customs-warehouses/?page=${page}&page_size=${pageSize}`;
|
||||
list: (companyId: number, page = 1, pageSize = 50, search?: string) => {
|
||||
let url = `/v1/public/reference_data/customs-warehouses/?company_id=${companyId}&page=${page}&page_size=${pageSize}`;
|
||||
if (search) {
|
||||
url += `&search=${encodeURIComponent(search)}`;
|
||||
}
|
||||
|
||||
@@ -35,13 +35,14 @@ export interface UpdateIncotermData {
|
||||
export const incotermsApi = {
|
||||
/**
|
||||
* Lista todos los incoterms con paginación y filtros
|
||||
* @param companyId - ID de la empresa
|
||||
* @param page - Número de página (por defecto 1)
|
||||
* @param pageSize - Tamaño de página (por defecto 50)
|
||||
* @param code - Filtrar por clave (opcional)
|
||||
* @param description - Filtrar por descripción (opcional)
|
||||
*/
|
||||
list: (page = 1, pageSize = 50, code?: string, description?: string) => {
|
||||
let url = `/v1/public/reference_data/incoterms/?page=${page}&page_size=${pageSize}`;
|
||||
list: (companyId: number, page = 1, pageSize = 50, code?: string, description?: string) => {
|
||||
let url = `/v1/public/reference_data/incoterms/?company_id=${companyId}&page=${page}&page_size=${pageSize}`;
|
||||
if (code) url += `&code=${encodeURIComponent(code)}`;
|
||||
if (description) url += `&description=${encodeURIComponent(description)}`;
|
||||
return api.get<IncotermListResponse>(url);
|
||||
|
||||
@@ -39,13 +39,15 @@ export interface UpdateInvoiceTypeData {
|
||||
export const invoiceTypesApi = {
|
||||
/**
|
||||
* Lista todos los tipos de factura con paginación y búsqueda
|
||||
* @param companyId - ID de la empresa
|
||||
* @param page - Número de página (por defecto 1)
|
||||
* @param pageSize - Tamaño de página (por defecto 50)
|
||||
* @param operation - Filtrar por tipo de operación (imp, exp)
|
||||
* @param search - Término de búsqueda (opcional)
|
||||
*/
|
||||
list: (page = 1, pageSize = 50, operation?: string, search?: string) => {
|
||||
list: (companyId: number, page = 1, pageSize = 50, operation?: string, search?: string) => {
|
||||
const params = new URLSearchParams({
|
||||
company_id: companyId.toString(),
|
||||
page: page.toString(),
|
||||
page_size: pageSize.toString()
|
||||
});
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
import { toast } from "svelte-sonner";
|
||||
import { Search, Loader2, DollarSign } from "lucide-svelte";
|
||||
import { currencyTypesApi, type CurrencyType } from "$lib/api/dashboard/reference_data/currency_types";
|
||||
import { companyStore } from "$lib/stores/company.svelte";
|
||||
|
||||
// --- PROPS ---
|
||||
let {
|
||||
@@ -41,11 +42,15 @@
|
||||
});
|
||||
|
||||
async function loadCurrencies() {
|
||||
loading = true;
|
||||
if (!companyStore.activeCompany) {
|
||||
toast.error("Selecciona una empresa antes de cargar monedas");
|
||||
return;
|
||||
}
|
||||
loading = true;
|
||||
try {
|
||||
// FIX: Usar API pública, sin company_id
|
||||
const response = await currencyTypesApi.list(1, 100);
|
||||
|
||||
const response = await currencyTypesApi.list(companyStore.activeCompany.id, 1, 100);
|
||||
|
||||
|
||||
if (response.error) {
|
||||
console.error("CurrencySelectorDialog Error:", response.error);
|
||||
toast.error(`Error al cargar monedas: ${response.error}`);
|
||||
|
||||
@@ -197,10 +197,10 @@
|
||||
classesResponse
|
||||
] = await Promise.all([
|
||||
clientsProvidersApi.list(companyId, 1, 1000),
|
||||
invoiceTypesApi.list(1, 1000),
|
||||
pedimentoCodesApi.list(1, 1000),
|
||||
materialTypesApi.list(1, 1000),
|
||||
customsSectionsApi.list(1, 1000),
|
||||
invoiceTypesApi.list(companyId, 1, 1000),
|
||||
pedimentoCodesApi.list(companyId, 1, 1000),
|
||||
materialTypesApi.list(companyId, 1, 1000),
|
||||
customsSectionsApi.list(companyId, 1, 1000),
|
||||
partsApi.list({ company_id: companyId, page: 1, page_size: 1000 }),
|
||||
classesApi.getWithFAData({ company_id: companyId, page: 1, page_size: 1000 })
|
||||
]);
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
import { currentUser } from '$lib/auth';
|
||||
import { userHasPedimentoRegimensRefAction } from '$lib/permissions/reference-data/pedimento-regimens-ref-permissions';
|
||||
import ErrorState from '$lib/components/dashboard/common/error-state.svelte';
|
||||
import { companyStore } from '$lib/stores/company.svelte';
|
||||
|
||||
// Los datos iniciales vienen del servidor
|
||||
let { data }: { data: PageData } = $props();
|
||||
@@ -55,10 +56,16 @@
|
||||
if (!browser) return;
|
||||
clearTimeout(timeout);
|
||||
timeout = setTimeout(async () => {
|
||||
if (!companyStore.activeCompany) return;
|
||||
loading = true;
|
||||
error = null;
|
||||
try {
|
||||
const response = await codePedimentoRegimensApi.list(1, pageSize, searchQuery);
|
||||
const response = await codePedimentoRegimensApi.list(
|
||||
companyStore.activeCompany.id,
|
||||
1,
|
||||
pageSize,
|
||||
searchQuery
|
||||
);
|
||||
if (!response.error && response.data) {
|
||||
allItems = response.data.items;
|
||||
currentPage = 1;
|
||||
@@ -79,11 +86,16 @@
|
||||
}
|
||||
|
||||
async function loadMore() {
|
||||
if (loading || !hasMore) return;
|
||||
if (loading || !hasMore || !companyStore.activeCompany) return;
|
||||
loading = true;
|
||||
error = null;
|
||||
try {
|
||||
const response = await codePedimentoRegimensApi.list(currentPage + 1, pageSize, searchQuery);
|
||||
const response = await codePedimentoRegimensApi.list(
|
||||
companyStore.activeCompany.id,
|
||||
currentPage + 1,
|
||||
pageSize,
|
||||
searchQuery
|
||||
);
|
||||
if (response.error) {
|
||||
if (response.status === 401 || response.status === 403) {
|
||||
error = 'Sesión expirada. Recargando página...';
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
import { currentUser } from '$lib/auth';
|
||||
import { userHasContainersRefAction } from '$lib/permissions/reference-data/containers-ref-permissions';
|
||||
import ErrorState from '$lib/components/dashboard/common/error-state.svelte';
|
||||
import { companyStore } from '$lib/stores/company.svelte';
|
||||
|
||||
// Los datos iniciales vienen del servidor
|
||||
let { data }: { data: PageData } = $props();
|
||||
@@ -44,10 +45,16 @@
|
||||
if (!browser) return;
|
||||
clearTimeout(timeout);
|
||||
timeout = setTimeout(async () => {
|
||||
if (!companyStore.activeCompany) return;
|
||||
loading = true;
|
||||
error = null;
|
||||
try {
|
||||
const response = await containersApi.list(1, pageSize, searchQuery);
|
||||
const response = await containersApi.list(
|
||||
companyStore.activeCompany.id,
|
||||
1,
|
||||
pageSize,
|
||||
searchQuery
|
||||
);
|
||||
if (!response.error && response.data) {
|
||||
allItems = response.data.items;
|
||||
currentPage = 1;
|
||||
@@ -68,11 +75,16 @@
|
||||
}
|
||||
|
||||
async function loadMore() {
|
||||
if (loading || !hasMore) return;
|
||||
if (loading || !hasMore || !companyStore.activeCompany) return;
|
||||
loading = true;
|
||||
error = null;
|
||||
try {
|
||||
const response = await containersApi.list(currentPage + 1, pageSize, searchQuery);
|
||||
try {
|
||||
const response = await containersApi.list(
|
||||
companyStore.activeCompany.id,
|
||||
currentPage + 1,
|
||||
pageSize,
|
||||
searchQuery
|
||||
);
|
||||
if (response.error) {
|
||||
if (response.status === 401 || response.status === 403) {
|
||||
error = 'Sesión expirada. Recargando página...';
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
import { userHasCurrencyTypesRefAction } from '$lib/permissions/reference-data/currency-types-ref-permissions';
|
||||
import { getAccessTokenFromDocument } from '$lib/access-token-cookie-browser';
|
||||
import ErrorState from '$lib/components/dashboard/common/error-state.svelte';
|
||||
import { companyStore } from '$lib/stores/company.svelte';
|
||||
|
||||
// Los datos iniciales vienen del servidor
|
||||
let { data }: { data: PageData } = $props();
|
||||
@@ -78,10 +79,16 @@
|
||||
if (!browser) return;
|
||||
clearTimeout(timeout);
|
||||
timeout = setTimeout(async () => {
|
||||
if (!companyStore.activeCompany) return;
|
||||
loading = true;
|
||||
error = null;
|
||||
try {
|
||||
const response = await currencyTypesApi.list(1, pageSize, searchQuery);
|
||||
const response = await currencyTypesApi.list(
|
||||
companyStore.activeCompany.id,
|
||||
1,
|
||||
pageSize,
|
||||
searchQuery
|
||||
);
|
||||
if (!response.error && response.data) {
|
||||
allItems = response.data.items;
|
||||
currentPage = 1;
|
||||
@@ -102,11 +109,16 @@
|
||||
}
|
||||
|
||||
async function loadMore() {
|
||||
if (loading || !hasMore) return;
|
||||
if (loading || !hasMore || !companyStore.activeCompany) return;
|
||||
loading = true;
|
||||
error = null;
|
||||
try {
|
||||
const response = await currencyTypesApi.list(currentPage + 1, pageSize, searchQuery);
|
||||
try {
|
||||
const response = await currencyTypesApi.list(
|
||||
companyStore.activeCompany.id,
|
||||
currentPage + 1,
|
||||
pageSize,
|
||||
searchQuery
|
||||
);
|
||||
if (response.error) {
|
||||
if (response.status === 401 || response.status === 403) {
|
||||
error = 'Sesión expirada. Recargando página...';
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
import { currentUser } from '$lib/auth';
|
||||
import { userHasCustomsWarehousesRefAction } from '$lib/permissions/reference-data/customs-warehouses-ref-permissions';
|
||||
import ErrorState from '$lib/components/dashboard/common/error-state.svelte';
|
||||
import { companyStore } from '$lib/stores/company.svelte';
|
||||
|
||||
// Los datos iniciales vienen del servidor
|
||||
let { data }: { data: PageData } = $props();
|
||||
@@ -53,10 +54,16 @@
|
||||
if (!browser) return;
|
||||
clearTimeout(timeout);
|
||||
timeout = setTimeout(async () => {
|
||||
if (!companyStore.activeCompany) return;
|
||||
loading = true;
|
||||
error = null;
|
||||
try {
|
||||
const response = await customsWarehousesApi.list(1, pageSize, searchQuery);
|
||||
const response = await customsWarehousesApi.list(
|
||||
companyStore.activeCompany.id,
|
||||
1,
|
||||
pageSize,
|
||||
searchQuery
|
||||
);
|
||||
if (!response.error && response.data) {
|
||||
allItems = response.data.items;
|
||||
currentPage = 1;
|
||||
@@ -77,11 +84,16 @@
|
||||
}
|
||||
|
||||
async function loadMore() {
|
||||
if (loading || !hasMore) return;
|
||||
if (loading || !hasMore || !companyStore.activeCompany) return;
|
||||
loading = true;
|
||||
error = null;
|
||||
try {
|
||||
const response = await customsWarehousesApi.list(currentPage + 1, pageSize, searchQuery);
|
||||
try {
|
||||
const response = await customsWarehousesApi.list(
|
||||
companyStore.activeCompany.id,
|
||||
currentPage + 1,
|
||||
pageSize,
|
||||
searchQuery
|
||||
);
|
||||
if (response.error) {
|
||||
if (response.status === 401 || response.status === 403) {
|
||||
error = 'Sesión expirada. Recargando página...';
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
import { currentUser } from '$lib/auth';
|
||||
import { userHasIncotermsRefAction } from '$lib/permissions/reference-data/incoterms-ref-permissions';
|
||||
import ErrorState from '$lib/components/dashboard/common/error-state.svelte';
|
||||
import { companyStore } from '$lib/stores/company.svelte';
|
||||
|
||||
// Los datos iniciales vienen del servidor
|
||||
let { data }: { data: PageData } = $props();
|
||||
@@ -54,10 +55,17 @@
|
||||
if (!browser) return;
|
||||
clearTimeout(timeout);
|
||||
timeout = setTimeout(async () => {
|
||||
if (!companyStore.activeCompany) return;
|
||||
loading = true;
|
||||
error = null;
|
||||
try {
|
||||
const response = await incotermsApi.list(1, pageSize, searchCode, searchDescription);
|
||||
const response = await incotermsApi.list(
|
||||
companyStore.activeCompany.id,
|
||||
1,
|
||||
pageSize,
|
||||
searchCode,
|
||||
searchDescription
|
||||
);
|
||||
if (!response.error && response.data) {
|
||||
allItems = response.data.items;
|
||||
currentPage = 1;
|
||||
@@ -81,11 +89,17 @@
|
||||
}
|
||||
|
||||
async function loadMore() {
|
||||
if (loading || !hasMore) return;
|
||||
if (loading || !hasMore || !companyStore.activeCompany) return;
|
||||
loading = true;
|
||||
error = null;
|
||||
try {
|
||||
const response = await incotermsApi.list(currentPage + 1, pageSize, searchCode, searchDescription);
|
||||
try {
|
||||
const response = await incotermsApi.list(
|
||||
companyStore.activeCompany.id,
|
||||
currentPage + 1,
|
||||
pageSize,
|
||||
searchCode,
|
||||
searchDescription
|
||||
);
|
||||
if (response.error) {
|
||||
if (response.status === 401 || response.status === 403) {
|
||||
error = 'Sesión expirada. Recargando página...';
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
import { currentUser } from '$lib/auth';
|
||||
import { userHasInvoiceTypesRefAction } from '$lib/permissions/reference-data/invoice-types-ref-permissions';
|
||||
import ErrorState from '$lib/components/dashboard/common/error-state.svelte';
|
||||
import { companyStore } from '$lib/stores/company.svelte';
|
||||
|
||||
// Los datos iniciales vienen del servidor
|
||||
let { data }: { data: PageData } = $props();
|
||||
@@ -53,10 +54,17 @@
|
||||
if (!browser) return;
|
||||
clearTimeout(timeout);
|
||||
timeout = setTimeout(async () => {
|
||||
if (!companyStore.activeCompany) return;
|
||||
loading = true;
|
||||
error = null;
|
||||
try {
|
||||
const response = await invoiceTypesApi.list(1, pageSize, undefined, searchQuery);
|
||||
const response = await invoiceTypesApi.list(
|
||||
companyStore.activeCompany.id,
|
||||
1,
|
||||
pageSize,
|
||||
undefined,
|
||||
searchQuery
|
||||
);
|
||||
if (!response.error && response.data) {
|
||||
allItems = response.data.items;
|
||||
currentPage = 1;
|
||||
@@ -77,11 +85,17 @@
|
||||
}
|
||||
|
||||
async function loadMore() {
|
||||
if (loading || !hasMore) return;
|
||||
if (loading || !hasMore || !companyStore.activeCompany) return;
|
||||
loading = true;
|
||||
error = null;
|
||||
try {
|
||||
const response = await invoiceTypesApi.list(currentPage + 1, pageSize, undefined, searchQuery);
|
||||
try {
|
||||
const response = await invoiceTypesApi.list(
|
||||
companyStore.activeCompany.id,
|
||||
currentPage + 1,
|
||||
pageSize,
|
||||
undefined,
|
||||
searchQuery
|
||||
);
|
||||
if (response.error) {
|
||||
if (response.status === 401 || response.status === 403) {
|
||||
error = 'Sesión expirada. Recargando página...';
|
||||
|
||||
Reference in New Issue
Block a user