38 lines
1.2 KiB
JavaScript
38 lines
1.2 KiB
JavaScript
// hooks/useSSENotifications.js
|
|
import { useEffect, useCallback } from 'react';
|
|
|
|
export const useSSENotifications = (token, collaborateurId, onEventReceived) => {
|
|
useEffect(() => {
|
|
if (!token || !collaborateurId) return;
|
|
|
|
const eventSource = new EventSource(
|
|
`/api/events?token=${encodeURIComponent(token)}`
|
|
);
|
|
|
|
eventSource.onmessage = (event) => {
|
|
try {
|
|
const data = JSON.parse(event.data);
|
|
|
|
console.log('📨 SSE reçu:', data);
|
|
|
|
// Log spécifique pour les récupérations
|
|
if (data.type === 'demande-validated' && data.typeConge === 'Récupération') {
|
|
console.log('🎨 Couleur reçue:', data.couleurHex);
|
|
}
|
|
|
|
onEventReceived(data);
|
|
} catch (error) {
|
|
console.error('❌ Erreur parsing SSE:', error);
|
|
}
|
|
};
|
|
|
|
eventSource.onerror = (error) => {
|
|
console.error('❌ Erreur SSE:', error);
|
|
eventSource.close();
|
|
};
|
|
|
|
return () => {
|
|
eventSource.close();
|
|
};
|
|
}, [token, collaborateurId, onEventReceived]);
|
|
}; |