From c906cb733e018ffed42bccd9a9eaa96258f9bad5 Mon Sep 17 00:00:00 2001 From: Giovanni-Josserand Date: Sun, 11 Jan 2026 12:10:44 +0100 Subject: [PATCH] multiplex user notifications over a single private channel --- src/components/Header/Header.jsx | 9 +++- .../listeners/userNotificationsListener.js | 43 +++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 src/utils/echo/listeners/userNotificationsListener.js diff --git a/src/components/Header/Header.jsx b/src/components/Header/Header.jsx index e2f813c..3402bd2 100644 --- a/src/components/Header/Header.jsx +++ b/src/components/Header/Header.jsx @@ -1,13 +1,17 @@ -import styles from "./Header.module.css" import { Link, NavLink } from "react-router"; import {useContext, useEffect, useRef, useState} from "react"; +import {AuthContext} from "../../contexts/auth/AuthContext.js"; + +import styles from "./Header.module.css" + import getUserNotifications from "../../utils/notifications/getUserNotifications.js"; import deleteNotificationUser from "../../utils/notifications/deleteNotificationUser.js"; import initEcho from "../../utils/echo/initEcho.js" import readNotifications from "../../utils/notifications/readNotifications.js"; -import {AuthContext} from "../../contexts/auth/AuthContext.js"; import getUser from "../../utils/getUser.js"; import { userCreatedListener } from "../../utils/echo/listeners/userCreatedListener"; +import { userNotificationsListener } from "../../utils/echo/listeners/userNotificationsListener.js"; + function Header() { @@ -39,6 +43,7 @@ function Header() { const activeChannels = [ userCreatedListener(echo, userData, setNotifications, setunreadNotification), + userNotificationsListener(echo, userData, setNotifications, setunreadNotification), ]; channelsToLeave = activeChannels.filter(name => name !== null); diff --git a/src/utils/echo/listeners/userNotificationsListener.js b/src/utils/echo/listeners/userNotificationsListener.js new file mode 100644 index 0000000..d51bcee --- /dev/null +++ b/src/utils/echo/listeners/userNotificationsListener.js @@ -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); + }); +}; \ No newline at end of file