@@ -118,6 +118,19 @@ export function LibraryProvider({ children }) {
|
||||
[request],
|
||||
);
|
||||
|
||||
// One consolidated call — creates the unit, its lessons, their objectives
|
||||
// and page-builder blocks, and attaches them, in a single request.
|
||||
const createUnitFull = useCallback(
|
||||
(payload) =>
|
||||
request(async () => {
|
||||
const { data } = await api.post(`${UNITS_BASE}/full`, payload);
|
||||
const created = data?.data?.data ?? null;
|
||||
if (created) toast("Unit created successfully.");
|
||||
return data;
|
||||
}),
|
||||
[request],
|
||||
);
|
||||
|
||||
const updateUnit = useCallback(
|
||||
(unitId, payload) =>
|
||||
request(async () => {
|
||||
@@ -311,6 +324,15 @@ export function LibraryProvider({ children }) {
|
||||
[request],
|
||||
);
|
||||
|
||||
const saveLessonPage = useCallback(
|
||||
(lessonId, payload) =>
|
||||
request(async () => {
|
||||
const { data } = await api.put(`${LESSONS_BASE}/${lessonId}/page`, payload);
|
||||
return data;
|
||||
}),
|
||||
[request],
|
||||
);
|
||||
|
||||
const archiveLesson = useCallback(
|
||||
(lessonId) =>
|
||||
request(async () => {
|
||||
@@ -401,7 +423,7 @@ export function LibraryProvider({ children }) {
|
||||
|
||||
// unit library
|
||||
units, unit, unitsFlat,
|
||||
fetchUnits, fetchUnitsFlat, fetchUnit, createUnit, updateUnit,
|
||||
fetchUnits, fetchUnitsFlat, fetchUnit, createUnit, createUnitFull, updateUnit,
|
||||
archiveUnit, archiveUnits, fetchArchivedUnits,
|
||||
restoreUnit, restoreUnits,
|
||||
permanentlyDeleteUnit, permanentlyDeleteUnits,
|
||||
@@ -411,7 +433,7 @@ export function LibraryProvider({ children }) {
|
||||
|
||||
// lesson library
|
||||
lessons, lesson, lessonsFlat,
|
||||
fetchLessons, fetchLessonsFlat, fetchLesson, createLesson, updateLesson,
|
||||
fetchLessons, fetchLessonsFlat, fetchLesson, createLesson, updateLesson, saveLessonPage,
|
||||
archiveLesson, archiveLessons, fetchArchivedLessons,
|
||||
restoreLesson, restoreLessons,
|
||||
permanentlyDeleteLesson, permanentlyDeleteLessons,
|
||||
|
||||
@@ -14,6 +14,7 @@ export function useAdminNotifications() {
|
||||
export function AdminNotificationProvider({ children }) {
|
||||
const [notifications, setNotifications] = useState([]);
|
||||
const [unseenCount, setUnseenCount] = useState(0);
|
||||
const [stickyAnnouncement, setStickyAnnouncement] = useState(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const intervalRef = useRef(null);
|
||||
|
||||
@@ -26,6 +27,15 @@ export function AdminNotificationProvider({ children }) {
|
||||
}
|
||||
}, []);
|
||||
|
||||
const fetchStickyAnnouncement = useCallback(async () => {
|
||||
try {
|
||||
const res = await api.get("/admin/notifications/sticky");
|
||||
setStickyAnnouncement(res.data?.data?.announcement ?? null);
|
||||
} catch {
|
||||
// silent
|
||||
}
|
||||
}, []);
|
||||
|
||||
const fetchNotifications = useCallback(async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
@@ -47,6 +57,7 @@ export function AdminNotificationProvider({ children }) {
|
||||
prev.map(n => n.notification_id === id ? { ...n, seen: true } : n)
|
||||
);
|
||||
setUnseenCount(prev => Math.max(0, prev - 1));
|
||||
setStickyAnnouncement(prev => (prev?.notification_id === id ? null : prev));
|
||||
} catch {
|
||||
// silent
|
||||
}
|
||||
@@ -57,22 +68,28 @@ export function AdminNotificationProvider({ children }) {
|
||||
await api.patch("/admin/notifications/seen-all");
|
||||
setNotifications(prev => prev.map(n => ({ ...n, seen: true })));
|
||||
setUnseenCount(0);
|
||||
setStickyAnnouncement(null);
|
||||
} catch {
|
||||
// silent
|
||||
}
|
||||
}, []);
|
||||
|
||||
// Initial load + start polling unseen count
|
||||
// Initial load + start polling unseen count + sticky announcement
|
||||
useEffect(() => {
|
||||
fetchUnseen();
|
||||
intervalRef.current = setInterval(fetchUnseen, POLL_INTERVAL);
|
||||
fetchStickyAnnouncement();
|
||||
intervalRef.current = setInterval(() => {
|
||||
fetchUnseen();
|
||||
fetchStickyAnnouncement();
|
||||
}, POLL_INTERVAL);
|
||||
return () => clearInterval(intervalRef.current);
|
||||
}, [fetchUnseen]);
|
||||
}, [fetchUnseen, fetchStickyAnnouncement]);
|
||||
|
||||
return (
|
||||
<AdminNotificationContext.Provider value={{
|
||||
notifications,
|
||||
unseenCount,
|
||||
stickyAnnouncement,
|
||||
loading,
|
||||
fetchNotifications,
|
||||
markSeen,
|
||||
|
||||
@@ -49,10 +49,26 @@ export function AdminNotificationTemplateProvider({ children }) {
|
||||
return data.data;
|
||||
}), [request, template]);
|
||||
|
||||
const createTemplate = useCallback((payload) =>
|
||||
request(async () => {
|
||||
const { data } = await api.post("/admin/announcement-templates", payload);
|
||||
setTemplates((prev) => [...prev, data.data]);
|
||||
toast("Announcement template created.");
|
||||
return data.data;
|
||||
}), [request]);
|
||||
|
||||
const deleteTemplate = useCallback((id) =>
|
||||
request(async () => {
|
||||
await api.delete(`/admin/announcement-templates/${id}`);
|
||||
setTemplates((prev) => prev.filter((t) => String(t.notification_template_id) !== String(id)));
|
||||
toast("Announcement template deleted.");
|
||||
return true;
|
||||
}), [request]);
|
||||
|
||||
return (
|
||||
<AdminNotificationTemplateContext.Provider value={{
|
||||
templates, template, loading,
|
||||
fetchTemplates, fetchTemplate, updateTemplate,
|
||||
fetchTemplates, fetchTemplate, updateTemplate, createTemplate, deleteTemplate,
|
||||
}}>
|
||||
{children}
|
||||
</AdminNotificationTemplateContext.Provider>
|
||||
|
||||
@@ -437,14 +437,15 @@ export function AdminTaskProvider({ children }) {
|
||||
[request]
|
||||
);
|
||||
|
||||
// Units/lessons/quizzes are standalone entities that may sit under 0..N
|
||||
// courses (junction revamp) — each row now carries a `courses[]` binding
|
||||
// array instead of a single course_title/order_index pair, and
|
||||
// RequirementBuilder's ContentPicker builds its own search string from it,
|
||||
// so no client-side `_search` precomputation is needed here anymore.
|
||||
const fetchUnitsFlat = useCallback(
|
||||
() => request(async () => {
|
||||
const res = await api.get('/admin/courses/units-flat');
|
||||
const raw = res.data?.data ?? [];
|
||||
return raw.map((u) => ({
|
||||
...u,
|
||||
_search: `${u.course_title} unit ${u.order_index + 1} ${u.title}`.toLowerCase(),
|
||||
}));
|
||||
return res.data?.data ?? [];
|
||||
}),
|
||||
[request]
|
||||
);
|
||||
@@ -452,11 +453,7 @@ export function AdminTaskProvider({ children }) {
|
||||
const fetchLessonsFlat = useCallback(
|
||||
() => request(async () => {
|
||||
const res = await api.get('/admin/courses/lessons-flat');
|
||||
const raw = res.data?.data ?? [];
|
||||
return raw.map((l) => ({
|
||||
...l,
|
||||
_search: `${l.course_title} unit ${l.unit_order + 1} ${l.unit_title} lesson ${l.order_index + 1} ${l.title}`.toLowerCase(),
|
||||
}));
|
||||
return res.data?.data ?? [];
|
||||
}),
|
||||
[request]
|
||||
);
|
||||
@@ -464,11 +461,7 @@ export function AdminTaskProvider({ children }) {
|
||||
const fetchQuizzesFlat = useCallback(
|
||||
() => request(async () => {
|
||||
const res = await api.get('/admin/courses/quizzes-flat');
|
||||
const raw = res.data?.data ?? [];
|
||||
return raw.map((q) => ({
|
||||
...q,
|
||||
_search: `${q.course_title} ${q.unit_title} ${q.title}`.toLowerCase(),
|
||||
}));
|
||||
return res.data?.data ?? [];
|
||||
}),
|
||||
[request]
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user