Start integrating the onboarding flow with backend 3
This commit is contained in:
@@ -87,6 +87,26 @@ export const SSEProvider: React.FC<SSEProviderProps> = ({ children }) => {
|
||||
}
|
||||
};
|
||||
|
||||
// Handle connection events from backend
|
||||
eventSource.addEventListener('connection', (event) => {
|
||||
try {
|
||||
const data = JSON.parse(event.data);
|
||||
console.log('SSE connection confirmed:', data.message);
|
||||
} catch (error) {
|
||||
console.error('Error parsing connection event:', error);
|
||||
}
|
||||
});
|
||||
|
||||
// Handle heartbeat events
|
||||
eventSource.addEventListener('heartbeat', (event) => {
|
||||
try {
|
||||
const data = JSON.parse(event.data);
|
||||
console.log('SSE heartbeat received:', new Date(data.timestamp * 1000));
|
||||
} catch (error) {
|
||||
console.error('Error parsing heartbeat event:', error);
|
||||
}
|
||||
});
|
||||
|
||||
eventSource.onerror = (error) => {
|
||||
console.error('SSE connection error:', error);
|
||||
setIsConnected(false);
|
||||
@@ -104,7 +124,7 @@ export const SSEProvider: React.FC<SSEProviderProps> = ({ children }) => {
|
||||
}
|
||||
};
|
||||
|
||||
// Handle custom event types
|
||||
// Handle notification events (alerts and recommendations from alert_processor)
|
||||
eventSource.addEventListener('notification', (event) => {
|
||||
try {
|
||||
const data = JSON.parse(event.data);
|
||||
@@ -116,12 +136,23 @@ export const SSEProvider: React.FC<SSEProviderProps> = ({ children }) => {
|
||||
|
||||
setLastEvent(sseEvent);
|
||||
|
||||
// Determine toast type based on severity and item_type
|
||||
let toastType: 'info' | 'success' | 'warning' | 'error' = 'info';
|
||||
if (data.item_type === 'alert') {
|
||||
if (data.severity === 'urgent') toastType = 'error';
|
||||
else if (data.severity === 'high') toastType = 'error';
|
||||
else if (data.severity === 'medium') toastType = 'warning';
|
||||
else toastType = 'info';
|
||||
} else if (data.item_type === 'recommendation') {
|
||||
toastType = 'info';
|
||||
}
|
||||
|
||||
// Show toast notification
|
||||
showToast({
|
||||
type: data.type || 'info',
|
||||
type: toastType,
|
||||
title: data.title || 'Notificación',
|
||||
message: data.message,
|
||||
duration: data.persistent ? 0 : 5000,
|
||||
duration: data.severity === 'urgent' ? 0 : 5000, // Keep urgent alerts until dismissed
|
||||
});
|
||||
|
||||
// Trigger registered listeners
|
||||
@@ -145,10 +176,10 @@ export const SSEProvider: React.FC<SSEProviderProps> = ({ children }) => {
|
||||
|
||||
setLastEvent(sseEvent);
|
||||
|
||||
// Show inventory alert
|
||||
// Show inventory alert (high/urgent alerts from alert_processor)
|
||||
showToast({
|
||||
type: 'warning',
|
||||
title: 'Alerta de Inventario',
|
||||
type: data.severity === 'urgent' ? 'error' : 'warning',
|
||||
title: data.title || 'Alerta de Inventario',
|
||||
message: data.message,
|
||||
duration: 0, // Keep until dismissed
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user