units,lesson as standalone
This commit is contained in:
@@ -17,6 +17,7 @@ const DEFAULT_PAGINATION = { page: 1, limit: 10, pages: 1, total: 0 };
|
||||
export function ClientNotificationProvider({ children }) {
|
||||
const [notifications, setNotifications] = useState([]);
|
||||
const [unseenCount, setUnseenCount] = useState(0);
|
||||
const [stickyAnnouncement, setStickyAnnouncement] = useState(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [pagination, setPagination] = useState(DEFAULT_PAGINATION);
|
||||
const intervalRef = useRef(null);
|
||||
@@ -31,6 +32,19 @@ export function ClientNotificationProvider({ children }) {
|
||||
}
|
||||
}, []);
|
||||
|
||||
const fetchStickyAnnouncement = useCallback(async () => {
|
||||
try {
|
||||
const res = await api.get("/client/notifications/sticky");
|
||||
setStickyAnnouncement(res.data?.data?.announcement ?? null);
|
||||
} catch {
|
||||
// silent
|
||||
}
|
||||
}, []);
|
||||
|
||||
const poll = useCallback(async () => {
|
||||
await Promise.allSettled([fetchUnseen(), fetchStickyAnnouncement()]);
|
||||
}, [fetchUnseen, fetchStickyAnnouncement]);
|
||||
|
||||
const fetchNotifications = useCallback(async (page = 1, limit = 10) => {
|
||||
setLoading(true);
|
||||
try {
|
||||
@@ -52,6 +66,7 @@ export function ClientNotificationProvider({ children }) {
|
||||
setNotifications([]);
|
||||
setUnseenCount(0);
|
||||
setPagination(DEFAULT_PAGINATION);
|
||||
setStickyAnnouncement(null);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
@@ -64,17 +79,25 @@ export function ClientNotificationProvider({ children }) {
|
||||
setNotifications(prev =>
|
||||
prev.map(n => n.notification_id === id ? { ...n, seen: true } : n)
|
||||
);
|
||||
setUnseenCount(prev => Math.max(0, prev - 1));
|
||||
|
||||
if (stickyAnnouncement?.notification_id === id) {
|
||||
setStickyAnnouncement(null);
|
||||
}
|
||||
|
||||
// Re-sync badge + sticky immediately (handles cases where the marked
|
||||
// row isn't present in the currently loaded notifications page).
|
||||
await Promise.allSettled([fetchUnseen(), fetchStickyAnnouncement()]);
|
||||
} catch {
|
||||
// silent
|
||||
}
|
||||
}, []);
|
||||
}, [stickyAnnouncement?.notification_id, fetchUnseen, fetchStickyAnnouncement]);
|
||||
|
||||
const markAllSeen = useCallback(async () => {
|
||||
try {
|
||||
await api.patch("/client/notifications/seen-all");
|
||||
setNotifications(prev => prev.map(n => ({ ...n, seen: true })));
|
||||
setUnseenCount(0);
|
||||
setStickyAnnouncement(null);
|
||||
} catch {
|
||||
// silent
|
||||
}
|
||||
@@ -82,8 +105,8 @@ export function ClientNotificationProvider({ children }) {
|
||||
|
||||
const restartPoll = useCallback((interval) => {
|
||||
clearInterval(intervalRef.current);
|
||||
intervalRef.current = setInterval(fetchUnseen, interval);
|
||||
}, [fetchUnseen]);
|
||||
intervalRef.current = setInterval(poll, interval);
|
||||
}, [poll]);
|
||||
|
||||
// Speed up polling while a student is mid-assessment; restore when done
|
||||
const accelerate = useCallback(() => {
|
||||
@@ -99,15 +122,16 @@ export function ClientNotificationProvider({ children }) {
|
||||
}, [restartPoll]);
|
||||
|
||||
useEffect(() => {
|
||||
fetchUnseen();
|
||||
intervalRef.current = setInterval(fetchUnseen, POLL_INTERVAL_NORMAL);
|
||||
poll();
|
||||
intervalRef.current = setInterval(poll, POLL_INTERVAL_NORMAL);
|
||||
return () => clearInterval(intervalRef.current);
|
||||
}, [fetchUnseen]);
|
||||
}, [poll]);
|
||||
|
||||
return (
|
||||
<ClientNotificationContext.Provider value={{
|
||||
notifications,
|
||||
unseenCount,
|
||||
stickyAnnouncement,
|
||||
loading,
|
||||
pagination,
|
||||
fetchNotifications,
|
||||
|
||||
Reference in New Issue
Block a user