split notification listeners into separate files

This commit is contained in:
2026-01-09 14:39:19 +01:00
parent c462385b91
commit 5fb97ccd90
2 changed files with 45 additions and 26 deletions
+28 -26
View File
@@ -6,6 +6,8 @@ import deleteNotificationUser from "../../utils/notifications/deleteNotification
import initEcho from "../../utils/echo/initEcho.js" import initEcho from "../../utils/echo/initEcho.js"
import readNotifications from "../../utils/notifications/readNotifications.js"; import readNotifications from "../../utils/notifications/readNotifications.js";
import {AuthContext} from "../../contexts/auth/AuthContext.js"; import {AuthContext} from "../../contexts/auth/AuthContext.js";
import getUser from "../../utils/getUser.js";
import { userCreatedListener } from "../../utils/echo/listeners/userCreatedListener";
function Header() { function Header() {
@@ -16,38 +18,36 @@ function Header() {
const [notifications, setNotifications] = useState([]); const [notifications, setNotifications] = useState([]);
const notificationRef = useRef(null); const notificationRef = useRef(null);
useEffect(() => { useEffect(() => {
const echo = initEcho(); let echoInstance = null;
let channelsToLeave = [];
echo.private("admins-private") async function initializeHeader() {
.listen(".user.created", (event) => { try {
const newNotification = { const [userData, notifData] = await Promise.all([
id: Date.now(), getUser(),
content: `Nouvel demande d'inscription : ${event.user.name} ${event.user.lastname}`, getUserNotifications()
is_new: true ]);
};
setNotifications(prevNotifications => [newNotification, ...prevNotifications]);
setunreadNotification(true);
});
return () => { const data = notifData || [];
echo.leave("admins-private"); setNotifications(data);
}; const hasUnread = data.some(n => n.pivot && n.pivot.unread === 1);
}, []); setunreadNotification(hasUnread);
useEffect(() => { const echo = initEcho();
echoInstance = echo;
async function loadNotifications() { const activeChannels = [
const notifData = await getUserNotifications(); userCreatedListener(echo, userData, setNotifications, setunreadNotification),
const data = notifData || []; ];
setNotifications(notifData || []); channelsToLeave = activeChannels.filter(name => name !== null);
const hasUnread = data.some(n => n.pivot && n.pivot.unread === 1);
setunreadNotification(hasUnread); } catch (err) {
console.error("Erreur initialisation Header:", err);
}
} }
loadNotifications(); initializeHeader();
const handleClickOutside = (event) => { const handleClickOutside = (event) => {
if ( if (
@@ -60,15 +60,17 @@ function Header() {
}; };
document.addEventListener("mousedown", handleClickOutside); document.addEventListener("mousedown", handleClickOutside);
return () => { return () => {
document.removeEventListener("mousedown", handleClickOutside); document.removeEventListener("mousedown", handleClickOutside);
if (echoInstance) {
channelsToLeave.forEach(chan => echoInstance.leave(chan));
}
}; };
}, []); }, []);
const toggleNotificationMenu = async () => { const toggleNotificationMenu = async () => {
try { try {
setnotificationMenu(!notificationMenu); setnotificationMenu(!notificationMenu);
@@ -0,0 +1,17 @@
export const userCreatedListener = (echo, userData, setNotifications, setunreadNotification) => {
if (userData.isAdmin || userData.role === "Gérant") {
const channelName = "users.registration";
echo.private(channelName)
.listen(".users.registration", (event) => {
const newNotification = {
id: Date.now(),
content: `Nouvelle demande d'inscription : ${event.user.name} ${event.user.lastname}`,
pivot: { unread: 1 }
};
setNotifications(prev => [newNotification, ...prev]);
setunreadNotification(true);
});
return channelName;
}
return null;
};