Fix new services implementation 5
This commit is contained in:
@@ -11,7 +11,7 @@ export { useTraining } from './useTraining';
|
||||
export { useForecast } from './useForecast';
|
||||
export { useNotification } from './useNotification';
|
||||
export { useOnboarding, useOnboardingStep } from './useOnboarding';
|
||||
export { useInventory, useInventoryDashboard, useInventoryItem } from './useInventory';
|
||||
export { useInventory, useInventoryDashboard, useInventoryItem, useInventoryProducts } from './useInventory';
|
||||
export { useRecipes, useProduction } from './useRecipes';
|
||||
|
||||
// Import hooks for combined usage
|
||||
@@ -23,6 +23,7 @@ import { useTraining } from './useTraining';
|
||||
import { useForecast } from './useForecast';
|
||||
import { useNotification } from './useNotification';
|
||||
import { useOnboarding } from './useOnboarding';
|
||||
import { useInventory } from './useInventory';
|
||||
|
||||
// Combined hook for common operations
|
||||
export const useApiHooks = () => {
|
||||
@@ -30,10 +31,11 @@ export const useApiHooks = () => {
|
||||
const tenant = useTenant();
|
||||
const sales = useSales();
|
||||
const external = useExternal();
|
||||
const training = useTraining();
|
||||
const training = useTraining({ disablePolling: true }); // Disable polling by default
|
||||
const forecast = useForecast();
|
||||
const notification = useNotification();
|
||||
const onboarding = useOnboarding();
|
||||
const inventory = useInventory();
|
||||
|
||||
return {
|
||||
auth,
|
||||
@@ -44,5 +46,6 @@ export const useApiHooks = () => {
|
||||
forecast,
|
||||
notification,
|
||||
onboarding,
|
||||
inventory
|
||||
};
|
||||
};
|
||||
@@ -31,7 +31,7 @@ export const useForecast = () => {
|
||||
setError(null);
|
||||
|
||||
const newForecasts = await forecastingService.createSingleForecast(tenantId, request);
|
||||
setForecasts(prev => [...newForecasts, ...prev]);
|
||||
setForecasts(prev => [...newForecasts, ...(prev || [])]);
|
||||
|
||||
return newForecasts;
|
||||
} catch (error) {
|
||||
@@ -52,7 +52,7 @@ export const useForecast = () => {
|
||||
setError(null);
|
||||
|
||||
const batchForecast = await forecastingService.createBatchForecast(tenantId, request);
|
||||
setBatchForecasts(prev => [batchForecast, ...prev]);
|
||||
setBatchForecasts(prev => [batchForecast, ...(prev || [])]);
|
||||
|
||||
return batchForecast;
|
||||
} catch (error) {
|
||||
@@ -90,7 +90,7 @@ export const useForecast = () => {
|
||||
const batchForecast = await forecastingService.getBatchForecastStatus(tenantId, batchId);
|
||||
|
||||
// Update batch forecast in state
|
||||
setBatchForecasts(prev => prev.map(bf =>
|
||||
setBatchForecasts(prev => (prev || []).map(bf =>
|
||||
bf.id === batchId ? batchForecast : bf
|
||||
));
|
||||
|
||||
@@ -147,7 +147,7 @@ export const useForecast = () => {
|
||||
setError(null);
|
||||
|
||||
const acknowledgedAlert = await forecastingService.acknowledgeForecastAlert(tenantId, alertId);
|
||||
setAlerts(prev => prev.map(alert =>
|
||||
setAlerts(prev => (prev || []).map(alert =>
|
||||
alert.id === alertId ? acknowledgedAlert : alert
|
||||
));
|
||||
} catch (error) {
|
||||
|
||||
@@ -20,6 +20,7 @@ import {
|
||||
PaginatedResponse,
|
||||
InventoryDashboardData
|
||||
} from '../services/inventory.service';
|
||||
import type { ProductInfo } from '../types';
|
||||
|
||||
import { useTenantId } from '../../hooks/useTenantId';
|
||||
|
||||
@@ -117,17 +118,29 @@ export const useInventory = (autoLoad = true): UseInventoryReturn => {
|
||||
|
||||
try {
|
||||
const response = await inventoryService.getInventoryItems(tenantId, params);
|
||||
setItems(response.items);
|
||||
console.log('🔄 useInventory: Loaded items:', response.items);
|
||||
setItems(response.items || []); // Ensure it's always an array
|
||||
setPagination({
|
||||
page: response.page,
|
||||
limit: response.limit,
|
||||
total: response.total,
|
||||
totalPages: response.total_pages
|
||||
page: response.page || 1,
|
||||
limit: response.limit || 20,
|
||||
total: response.total || 0,
|
||||
totalPages: response.total_pages || 0
|
||||
});
|
||||
} catch (err: any) {
|
||||
console.error('❌ useInventory: Error loading items:', err);
|
||||
const errorMessage = err.response?.data?.detail || err.message || 'Error loading inventory items';
|
||||
|
||||
setError(errorMessage);
|
||||
toast.error(errorMessage);
|
||||
setItems([]); // Set empty array on error
|
||||
|
||||
// Show appropriate error message
|
||||
if (err.response?.status === 401) {
|
||||
console.error('❌ useInventory: Authentication failed');
|
||||
} else if (err.response?.status === 403) {
|
||||
toast.error('No tienes permisos para acceder a este inventario');
|
||||
} else {
|
||||
toast.error(errorMessage);
|
||||
}
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
@@ -222,6 +235,7 @@ export const useInventory = (autoLoad = true): UseInventoryReturn => {
|
||||
setStockLevels(levelMap);
|
||||
} catch (err: any) {
|
||||
console.error('Error loading stock levels:', err);
|
||||
// Don't show toast error for this as it's not critical for forecast page
|
||||
}
|
||||
}, [tenantId]);
|
||||
|
||||
@@ -273,6 +287,7 @@ export const useInventory = (autoLoad = true): UseInventoryReturn => {
|
||||
setAlerts(alertsData);
|
||||
} catch (err: any) {
|
||||
console.error('Error loading alerts:', err);
|
||||
// Don't show toast error for this as it's not critical for forecast page
|
||||
}
|
||||
}, [tenantId]);
|
||||
|
||||
@@ -301,6 +316,7 @@ export const useInventory = (autoLoad = true): UseInventoryReturn => {
|
||||
setDashboardData(data);
|
||||
} catch (err: any) {
|
||||
console.error('Error loading dashboard:', err);
|
||||
// Don't show toast error for this as it's not critical for forecast page
|
||||
}
|
||||
}, [tenantId]);
|
||||
|
||||
@@ -507,4 +523,61 @@ export const useInventoryItem = (itemId: string): UseInventoryItemReturn => {
|
||||
adjustStock,
|
||||
refresh
|
||||
};
|
||||
};
|
||||
|
||||
// ========== SIMPLE PRODUCTS HOOK FOR FORECASTING ==========
|
||||
|
||||
export const useInventoryProducts = () => {
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
/**
|
||||
* Get Products List for Forecasting
|
||||
*/
|
||||
const getProductsList = useCallback(async (tenantId: string): Promise<ProductInfo[]> => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
|
||||
const products = await inventoryService.getProductsList(tenantId);
|
||||
|
||||
return products;
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : 'Failed to get products list';
|
||||
setError(message);
|
||||
throw error;
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
/**
|
||||
* Get Product by ID
|
||||
*/
|
||||
const getProductById = useCallback(async (tenantId: string, productId: string): Promise<ProductInfo | null> => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
|
||||
const product = await inventoryService.getProductById(tenantId, productId);
|
||||
|
||||
return product;
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : 'Failed to get product';
|
||||
setError(message);
|
||||
throw error;
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
return {
|
||||
// State
|
||||
isLoading,
|
||||
error,
|
||||
|
||||
// Actions
|
||||
getProductsList,
|
||||
getProductById,
|
||||
};
|
||||
};
|
||||
@@ -156,25 +156,6 @@ export const useSales = () => {
|
||||
}
|
||||
}, []);
|
||||
|
||||
/**
|
||||
* Get Products List
|
||||
*/
|
||||
const getProductsList = useCallback(async (tenantId: string): Promise<string[]> => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
|
||||
const products = await salesService.getProductsList(tenantId);
|
||||
|
||||
return products;
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : 'Failed to get products list';
|
||||
setError(message);
|
||||
throw error;
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
/**
|
||||
* Get Sales Analytics
|
||||
@@ -213,7 +194,6 @@ export const useSales = () => {
|
||||
getDashboardStats,
|
||||
getRecentActivity,
|
||||
exportSalesData,
|
||||
getProductsList,
|
||||
getSalesAnalytics,
|
||||
clearError: () => setError(null),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user