multiplex user notifications over a single private channel

This commit is contained in:
2026-01-11 12:10:44 +01:00
parent 5fb97ccd90
commit c906cb733e
2 changed files with 50 additions and 2 deletions
@@ -0,0 +1,43 @@
export const userNotificationsListener = (echo, userData, setNotifications, setunreadNotification) => {
if (!userData?.id) return null;
const channelName = `user.${userData.id}`;
return echo.private(channelName)
.listen('.event.participation.cancelled', (event) => {
const newNotification = {
id: Date.now(),
content: `L'événement ${event.event.name} a été supprimé ! Vous n'y participez donc plus.`,
pivot: { unread: 1 }
};
setNotifications(prev => [newNotification, ...prev]);
setunreadNotification(true);
})
.listen(`.task.participation.cancelled`, (event) => {
const newNotification = {
id: Date.now(),
content: `La tâche ${event.task.name} de l'événement ${event.event.name} a été supprimé ! Vous n'y participez donc plus.`,
pivot: { unread: 1 }
};
setNotifications(prev => [newNotification, ...prev]);
setunreadNotification(true);
})
.listen('.volunteer.assigned.to.task', (event) => {
const newNotification = {
id: Date.now(),
content: `Vous avez été assigné à la tâche ${event.task.name} de l'événement ${event.event.name}`,
pivot: { unread: 1 }
};
setNotifications(prev => [newNotification, ...prev]);
setunreadNotification(true);
})
.listen('.volunteer.unassigned.to.task', (event) => {
const newNotification = {
id: Date.now(),
content: `Vous avez été désassigné de la tâche ${event.task.name} de l'événement ${event.event.name}`,
pivot: { unread: 1 }
};
setNotifications(prev => [newNotification, ...prev]);
setunreadNotification(true);
});
};