From a0c42b5e812bd1d17f78a96268686edef3901457 Mon Sep 17 00:00:00 2001 From: Giovanni-Josserand Date: Thu, 19 Mar 2026 16:40:53 +0100 Subject: [PATCH] remove duplicated notification handling code --- .../listeners/userNotificationsListener.js | 49 ++++++------------- 1 file changed, 16 insertions(+), 33 deletions(-) diff --git a/src/utils/echo/listeners/userNotificationsListener.js b/src/utils/echo/listeners/userNotificationsListener.js index 7d94483..7fa193d 100644 --- a/src/utils/echo/listeners/userNotificationsListener.js +++ b/src/utils/echo/listeners/userNotificationsListener.js @@ -1,46 +1,29 @@ -export const userNotificationsListener = (echo, userData, setNotifications, setunreadNotification) => { +export const userNotificationsListener = (echo, userData, setNotifications, setUnreadNotification) => { const channelName = `user.${userData.id}`; + const handleNotification = (content) => { + const newNotification = { + id: Date.now(), + content, + created_at: Date.now(), + pivot: { unread: 1 } + }; + setNotifications(prev => [newNotification, ...prev]); + setUnreadNotification(true); + }; + 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.`, - created_at: Date.now(), - pivot: { unread: 1 } - }; - setNotifications(prev => [newNotification, ...prev]); - setunreadNotification(true); + handleNotification(`L'événement ${event.event.name} a été supprimé ! Vous n'y participez donc plus.`); }) .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.`, - created_at: Date.now(), - pivot: { unread: 1 } - }; - setNotifications(prev => [newNotification, ...prev]); - setunreadNotification(true); + handleNotification(`La tâche ${event.task.name} de l'événement ${event.event.name} a été supprimée !`); }) .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}`, - created_at: Date.now(), - pivot: { unread: 1 } - }; - setNotifications(prev => [newNotification, ...prev]); - setunreadNotification(true); + handleNotification(`Vous avez été assigné à la tâche ${event.task.name} de l'événement ${event.event.name}`); }) .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}`, - created_at: Date.now(), - pivot: { unread: 1 } - }; - setNotifications(prev => [newNotification, ...prev]); - setunreadNotification(true); + handleNotification(`Vous avez été désassigné de la tâche ${event.task.name} de l'événement ${event.event.name}`); }); }; \ No newline at end of file