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 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";
function Header() {
@@ -16,38 +18,36 @@ function Header() {
const [notifications, setNotifications] = useState([]);
const notificationRef = useRef(null);
useEffect(() => {
const echo = initEcho();
let echoInstance = null;
let channelsToLeave = [];
echo.private("admins-private")
.listen(".user.created", (event) => {
const newNotification = {
id: Date.now(),
content: `Nouvel demande d'inscription : ${event.user.name} ${event.user.lastname}`,
is_new: true
};
setNotifications(prevNotifications => [newNotification, ...prevNotifications]);
setunreadNotification(true);
});
async function initializeHeader() {
try {
const [userData, notifData] = await Promise.all([
getUser(),
getUserNotifications()
]);
return () => {
echo.leave("admins-private");
};
}, []);
const data = notifData || [];
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 notifData = await getUserNotifications();
const data = notifData || [];
setNotifications(notifData || []);
const hasUnread = data.some(n => n.pivot && n.pivot.unread === 1);
const activeChannels = [
userCreatedListener(echo, userData, setNotifications, setunreadNotification),
];
channelsToLeave = activeChannels.filter(name => name !== null);
setunreadNotification(hasUnread);
} catch (err) {
console.error("Erreur initialisation Header:", err);
}
}
loadNotifications();
initializeHeader();
const handleClickOutside = (event) => {
if (
@@ -60,15 +60,17 @@ function Header() {
};
document.addEventListener("mousedown", handleClickOutside);
return () => {
document.removeEventListener("mousedown", handleClickOutside);
if (echoInstance) {
channelsToLeave.forEach(chan => echoInstance.leave(chan));
}
};
}, []);
const toggleNotificationMenu = async () => {
try {
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;
};