@@ -1,76 +0,0 @@
|
||||
import { createContext, useCallback, useContext, useState } from "react";
|
||||
import api from "@/utils/api.util";
|
||||
import { toast } from "sonner";
|
||||
|
||||
const AdminAchievementsContext = createContext(null);
|
||||
|
||||
export function useAdminAchievements() {
|
||||
const ctx = useContext(AdminAchievementsContext);
|
||||
if (!ctx) throw new Error("useAdminAchievements must be used inside AdminAchievementsProvider");
|
||||
return ctx;
|
||||
}
|
||||
|
||||
export function AdminAchievementsProvider({ children }) {
|
||||
const [achievements, setAchievements] = useState([]);
|
||||
const [achievement, setAchievement] = useState(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const request = useCallback(async (fn) => {
|
||||
setLoading(true);
|
||||
try { return await fn(); }
|
||||
catch (err) {
|
||||
toast(err?.response?.data?.message ?? "Something went wrong.");
|
||||
return null;
|
||||
} finally { setLoading(false); }
|
||||
}, []);
|
||||
|
||||
const fetchAchievements = useCallback(() =>
|
||||
request(async () => {
|
||||
const { data } = await api.get("/admin/achievements");
|
||||
setAchievements(data.data ?? []);
|
||||
return data.data;
|
||||
}), [request]);
|
||||
|
||||
const fetchAchievement = useCallback((id) =>
|
||||
request(async () => {
|
||||
const { data } = await api.get(`/admin/achievements/${id}`);
|
||||
setAchievement(data.data ?? null);
|
||||
return data.data;
|
||||
}), [request]);
|
||||
|
||||
const createAchievement = useCallback((payload) =>
|
||||
request(async () => {
|
||||
const { data } = await api.post("/admin/achievements", payload);
|
||||
toast("Achievement created.");
|
||||
return data.data;
|
||||
}), [request]);
|
||||
|
||||
const updateAchievement = useCallback((id, payload) =>
|
||||
request(async () => {
|
||||
const { data } = await api.put(`/admin/achievements/${id}`, payload);
|
||||
setAchievements((prev) =>
|
||||
prev.map((a) => (String(a.achievement_definition_id) === String(id) ? data.data : a))
|
||||
);
|
||||
if (achievement && String(achievement.achievement_definition_id) === String(id)) setAchievement(data.data);
|
||||
toast("Achievement updated.");
|
||||
return data.data;
|
||||
}), [request, achievement]);
|
||||
|
||||
const deleteAchievement = useCallback((id) =>
|
||||
request(async () => {
|
||||
await api.delete(`/admin/achievements/${id}`);
|
||||
setAchievements((prev) => prev.filter((a) => String(a.achievement_definition_id) !== String(id)));
|
||||
toast("Achievement deleted.");
|
||||
return true;
|
||||
}), [request]);
|
||||
|
||||
return (
|
||||
<AdminAchievementsContext.Provider value={{
|
||||
achievements, achievement, loading,
|
||||
fetchAchievements, fetchAchievement,
|
||||
createAchievement, updateAchievement, deleteAchievement,
|
||||
}}>
|
||||
{children}
|
||||
</AdminAchievementsContext.Provider>
|
||||
);
|
||||
}
|
||||
@@ -218,9 +218,11 @@ export function AssetsProvider({ children }) {
|
||||
});
|
||||
if (file) formData.append("file", file);
|
||||
|
||||
const res = await api.patch(`/admin/assets/${assetId}`, formData, {
|
||||
headers: { "Content-Type": "multipart/form-data" },
|
||||
});
|
||||
// No explicit Content-Type here — axios/the browser must set it
|
||||
// itself so the multipart boundary is included. A hardcoded
|
||||
// "multipart/form-data" header (no boundary) makes multer fail
|
||||
// to parse the body, silently dropping the file and every field.
|
||||
const res = await api.patch(`/admin/assets/${assetId}`, formData);
|
||||
|
||||
const asset = res.data?.data?.data ?? null;
|
||||
if (asset) {
|
||||
|
||||
@@ -26,6 +26,7 @@ export function NotificationBroadcastsProvider({ children }) {
|
||||
const [attributes, setAttributes] = useState([]);
|
||||
const [pagination, setPagination] = useState(PAGINATION_INIT);
|
||||
const [selectedBroadcast, setSelectedBroadcast] = useState(null);
|
||||
const [stickyBannerSetting, setStickyBannerSetting] = useState(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const request = useCallback(async (fn) => {
|
||||
@@ -207,6 +208,35 @@ export function NotificationBroadcastsProvider({ children }) {
|
||||
[request]
|
||||
);
|
||||
|
||||
// ─── GET /api/admin/announcements/sticky-banner ───────────────────────────
|
||||
// Shared banner image for the whole rotating sticky bar — one image for
|
||||
// all (up to 3) concurrently-active announcements, not one per announcement.
|
||||
const fetchStickyBannerSetting = useCallback(
|
||||
() =>
|
||||
request(async () => {
|
||||
const res = await api.get("/admin/announcements/sticky-banner");
|
||||
const setting = res.data?.data?.data ?? null;
|
||||
setStickyBannerSetting(setting);
|
||||
return res.data;
|
||||
}),
|
||||
[request]
|
||||
);
|
||||
|
||||
// ─── PATCH /api/admin/announcements/sticky-banner ────────────────────────
|
||||
const updateStickyBannerSetting = useCallback(
|
||||
(fields) =>
|
||||
request(async () => {
|
||||
const res = await api.patch("/admin/announcements/sticky-banner", fields);
|
||||
const setting = res.data?.data?.data ?? null;
|
||||
if (setting) {
|
||||
setStickyBannerSetting((prev) => ({ ...prev, ...setting }));
|
||||
toast("Sticky banner image updated.");
|
||||
}
|
||||
return res.data;
|
||||
}),
|
||||
[request]
|
||||
);
|
||||
|
||||
// ─── DELETE /api/admin/announcements/bulk/permanent ──────────────────────
|
||||
const permanentlyDeleteBroadcasts = useCallback(
|
||||
({ ids }) =>
|
||||
@@ -225,6 +255,7 @@ export function NotificationBroadcastsProvider({ children }) {
|
||||
attributes,
|
||||
pagination,
|
||||
selectedBroadcast,
|
||||
stickyBannerSetting,
|
||||
loading,
|
||||
setPagination,
|
||||
setSelectedBroadcast,
|
||||
@@ -240,6 +271,8 @@ export function NotificationBroadcastsProvider({ children }) {
|
||||
restoreBroadcasts,
|
||||
permanentlyDeleteBroadcast,
|
||||
permanentlyDeleteBroadcasts,
|
||||
fetchStickyBannerSetting,
|
||||
updateStickyBannerSetting,
|
||||
}}>
|
||||
{children}
|
||||
</NotificationBroadcastsContext.Provider>
|
||||
|
||||
@@ -14,7 +14,8 @@ export function useAdminNotifications() {
|
||||
export function AdminNotificationProvider({ children }) {
|
||||
const [notifications, setNotifications] = useState([]);
|
||||
const [unseenCount, setUnseenCount] = useState(0);
|
||||
const [stickyAnnouncement, setStickyAnnouncement] = useState(null);
|
||||
const [stickyAnnouncements, setStickyAnnouncements] = useState([]);
|
||||
const [bannerImage, setBannerImage] = useState(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const intervalRef = useRef(null);
|
||||
|
||||
@@ -30,7 +31,8 @@ export function AdminNotificationProvider({ children }) {
|
||||
const fetchStickyAnnouncement = useCallback(async () => {
|
||||
try {
|
||||
const res = await api.get("/admin/notifications/sticky");
|
||||
setStickyAnnouncement(res.data?.data?.announcement ?? null);
|
||||
setStickyAnnouncements(res.data?.data?.announcements ?? []);
|
||||
setBannerImage(res.data?.data?.bannerImage ?? null);
|
||||
} catch {
|
||||
// silent
|
||||
}
|
||||
@@ -57,7 +59,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));
|
||||
setStickyAnnouncements(prev => prev.filter(a => a.notification_id !== id));
|
||||
} catch {
|
||||
// silent
|
||||
}
|
||||
@@ -68,7 +70,7 @@ export function AdminNotificationProvider({ children }) {
|
||||
await api.patch("/admin/notifications/seen-all");
|
||||
setNotifications(prev => prev.map(n => ({ ...n, seen: true })));
|
||||
setUnseenCount(0);
|
||||
setStickyAnnouncement(null);
|
||||
setStickyAnnouncements([]);
|
||||
} catch {
|
||||
// silent
|
||||
}
|
||||
@@ -89,7 +91,8 @@ export function AdminNotificationProvider({ children }) {
|
||||
<AdminNotificationContext.Provider value={{
|
||||
notifications,
|
||||
unseenCount,
|
||||
stickyAnnouncement,
|
||||
stickyAnnouncements,
|
||||
bannerImage,
|
||||
loading,
|
||||
fetchNotifications,
|
||||
markSeen,
|
||||
|
||||
@@ -1,76 +0,0 @@
|
||||
import { createContext, useCallback, useContext, useState } from "react";
|
||||
import api from "@/utils/api.util";
|
||||
import { toast } from "sonner";
|
||||
|
||||
const AdminNotificationTemplateContext = createContext(null);
|
||||
|
||||
export function useAdminNotificationTemplates() {
|
||||
const ctx = useContext(AdminNotificationTemplateContext);
|
||||
if (!ctx) throw new Error("useAdminNotificationTemplates must be used inside AdminNotificationTemplateProvider");
|
||||
return ctx;
|
||||
}
|
||||
|
||||
export function AdminNotificationTemplateProvider({ children }) {
|
||||
const [templates, setTemplates] = useState([]);
|
||||
const [template, setTemplate] = useState(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const request = useCallback(async (fn) => {
|
||||
setLoading(true);
|
||||
try { return await fn(); }
|
||||
catch (err) {
|
||||
toast(err?.response?.data?.message ?? "Something went wrong.");
|
||||
return null;
|
||||
} finally { setLoading(false); }
|
||||
}, []);
|
||||
|
||||
const fetchTemplates = useCallback(() =>
|
||||
request(async () => {
|
||||
const { data } = await api.get("/admin/announcement-templates");
|
||||
setTemplates(data.data ?? []);
|
||||
return data.data;
|
||||
}), [request]);
|
||||
|
||||
const fetchTemplate = useCallback((id) =>
|
||||
request(async () => {
|
||||
const { data } = await api.get(`/admin/announcement-templates/${id}`);
|
||||
setTemplate(data.data ?? null);
|
||||
return data.data;
|
||||
}), [request]);
|
||||
|
||||
const updateTemplate = useCallback((id, payload) =>
|
||||
request(async () => {
|
||||
const { data } = await api.put(`/admin/announcement-templates/${id}`, payload);
|
||||
setTemplates((prev) =>
|
||||
prev.map((t) => (String(t.notification_template_id) === String(id) ? data.data : t))
|
||||
);
|
||||
if (template && String(template.notification_template_id) === String(id)) setTemplate(data.data);
|
||||
toast("Notification template updated.");
|
||||
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, createTemplate, deleteTemplate,
|
||||
}}>
|
||||
{children}
|
||||
</AdminNotificationTemplateContext.Provider>
|
||||
);
|
||||
}
|
||||
@@ -29,7 +29,7 @@ export function useClientAdvertisements() {
|
||||
|
||||
export function ClientAdvertisementsProvider({ children }) {
|
||||
const navigate = useNavigate();
|
||||
const { profile, getProfile, updateProfile } = useProfile();
|
||||
const { profile, getProfile } = useProfile();
|
||||
|
||||
// Keyed by placement so multiple slots on the same page (e.g. dashboard.hero +
|
||||
// dashboard.popup) can be fetched independently without clobbering each other.
|
||||
@@ -43,7 +43,6 @@ export function ClientAdvertisementsProvider({ children }) {
|
||||
const [listLoading, setListLoading] = useState({});
|
||||
const [clickCounts, setClickCounts] = useState(loadClickCounts);
|
||||
const [pendingClick, setPendingClick] = useState(null); // { ad, cta } awaiting confirmation
|
||||
const [dismissConfirmOpen, setDismissConfirmOpen] = useState(false);
|
||||
|
||||
// Ad fetches must know the real preference before deciding visibility — never
|
||||
// assume "show" as a default just because profile hasn't loaded yet. profileRef
|
||||
@@ -63,14 +62,11 @@ export function ClientAdvertisementsProvider({ children }) {
|
||||
return fresh;
|
||||
}, [getProfile]);
|
||||
|
||||
// Popups are gated separately from hero/banner/sidebar so "Don't show this
|
||||
// ad again" only ever touches popups, per the Settings → Advertisements toggles.
|
||||
// Gated by the Settings → Advertisements "Other ads" toggle.
|
||||
const resolveVisibility = (profileData, ad) => {
|
||||
if (!ad) return ad;
|
||||
const showPopupAds = profileData?.personal_info?.show_popup_ads ?? true;
|
||||
const showOtherAds = profileData?.personal_info?.show_other_ads ?? true;
|
||||
const hidden = ad.type === "popup" ? !showPopupAds : !showOtherAds;
|
||||
return hidden ? null : ad;
|
||||
return showOtherAds ? ad : null;
|
||||
};
|
||||
|
||||
// ─── GET /api/client/advertisements/active?placement=dashboard.hero ───────
|
||||
@@ -171,15 +167,18 @@ export function ClientAdvertisementsProvider({ children }) {
|
||||
[]
|
||||
);
|
||||
|
||||
// Tracks the click then follows the CTA link (external → new tab, internal → router nav).
|
||||
// Tracks the click then follows the link: the CTA's own link wins if present,
|
||||
// else the ad's redirect_link, else its internal Page Builder landing page
|
||||
// (/ads/:uuid) if one was authored. External links open in a new tab.
|
||||
const goToCta = useCallback(
|
||||
(ad, cta) => {
|
||||
trackClick(ad?.advertisement_id);
|
||||
if (!cta?.link) return;
|
||||
if (/^https?:\/\//.test(cta.link)) {
|
||||
window.open(cta.link, "_blank", "noopener,noreferrer");
|
||||
const link = cta?.link || ad?.redirect_link || (ad?.landing_page ? `/ads/${ad.uuid}` : null);
|
||||
if (!link) return;
|
||||
if (/^https?:\/\//.test(link)) {
|
||||
window.open(link, "_blank", "noopener,noreferrer");
|
||||
} else {
|
||||
navigate(cta.link);
|
||||
navigate(link);
|
||||
}
|
||||
},
|
||||
[navigate, trackClick]
|
||||
@@ -219,20 +218,6 @@ export function ClientAdvertisementsProvider({ children }) {
|
||||
setPendingClick(null);
|
||||
};
|
||||
|
||||
// ─── "Don't show this ad again" (popups only) ──────────────────────────
|
||||
// Persists the preference to the account (so it follows across devices),
|
||||
// then shows a one-time confirmation pointing at where to turn it back on.
|
||||
const dismissPopupForever = useCallback(async () => {
|
||||
const result = await updateProfile({ show_popup_ads: false });
|
||||
if (result?.data) profileRef.current = result.data;
|
||||
setDismissConfirmOpen(true);
|
||||
}, [updateProfile]);
|
||||
|
||||
const goToAdSettings = () => {
|
||||
setDismissConfirmOpen(false);
|
||||
navigate("/settings");
|
||||
};
|
||||
|
||||
return (
|
||||
<ClientAdvertisementsContext.Provider value={{
|
||||
advertisements,
|
||||
@@ -244,7 +229,6 @@ export function ClientAdvertisementsProvider({ children }) {
|
||||
getActiveAdvertisementList,
|
||||
trackClick,
|
||||
handleAdCtaClick,
|
||||
dismissPopupForever,
|
||||
}}>
|
||||
{children}
|
||||
|
||||
@@ -260,19 +244,6 @@ export function ClientAdvertisementsProvider({ children }) {
|
||||
</>
|
||||
}
|
||||
/>
|
||||
|
||||
<ResponsiveModal
|
||||
open={dismissConfirmOpen}
|
||||
onOpenChange={setDismissConfirmOpen}
|
||||
title="Popup ads turned off"
|
||||
description="You won't see popup ads anymore. You can turn them back on anytime in Settings → Advertisements."
|
||||
footer={
|
||||
<>
|
||||
<Button variant="outline" onClick={() => setDismissConfirmOpen(false)}>Got it</Button>
|
||||
<Button onClick={goToAdSettings}>Go to Settings</Button>
|
||||
</>
|
||||
}
|
||||
/>
|
||||
</ClientAdvertisementsContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -17,7 +17,8 @@ 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 [stickyAnnouncements, setStickyAnnouncements] = useState([]);
|
||||
const [bannerImage, setBannerImage] = useState(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [pagination, setPagination] = useState(DEFAULT_PAGINATION);
|
||||
const intervalRef = useRef(null);
|
||||
@@ -35,7 +36,8 @@ export function ClientNotificationProvider({ children }) {
|
||||
const fetchStickyAnnouncement = useCallback(async () => {
|
||||
try {
|
||||
const res = await api.get("/client/notifications/sticky");
|
||||
setStickyAnnouncement(res.data?.data?.announcement ?? null);
|
||||
setStickyAnnouncements(res.data?.data?.announcements ?? []);
|
||||
setBannerImage(res.data?.data?.bannerImage ?? null);
|
||||
} catch {
|
||||
// silent
|
||||
}
|
||||
@@ -66,7 +68,7 @@ export function ClientNotificationProvider({ children }) {
|
||||
setNotifications([]);
|
||||
setUnseenCount(0);
|
||||
setPagination(DEFAULT_PAGINATION);
|
||||
setStickyAnnouncement(null);
|
||||
setStickyAnnouncements([]);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
@@ -80,9 +82,7 @@ export function ClientNotificationProvider({ children }) {
|
||||
prev.map(n => n.notification_id === id ? { ...n, seen: true } : n)
|
||||
);
|
||||
|
||||
if (stickyAnnouncement?.notification_id === id) {
|
||||
setStickyAnnouncement(null);
|
||||
}
|
||||
setStickyAnnouncements(prev => prev.filter(a => a.notification_id !== id));
|
||||
|
||||
// Re-sync badge + sticky immediately (handles cases where the marked
|
||||
// row isn't present in the currently loaded notifications page).
|
||||
@@ -90,14 +90,14 @@ export function ClientNotificationProvider({ children }) {
|
||||
} catch {
|
||||
// silent
|
||||
}
|
||||
}, [stickyAnnouncement?.notification_id, fetchUnseen, fetchStickyAnnouncement]);
|
||||
}, [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);
|
||||
setStickyAnnouncements([]);
|
||||
} catch {
|
||||
// silent
|
||||
}
|
||||
@@ -131,7 +131,8 @@ export function ClientNotificationProvider({ children }) {
|
||||
<ClientNotificationContext.Provider value={{
|
||||
notifications,
|
||||
unseenCount,
|
||||
stickyAnnouncement,
|
||||
stickyAnnouncements,
|
||||
bannerImage,
|
||||
loading,
|
||||
pagination,
|
||||
fetchNotifications,
|
||||
|
||||
Reference in New Issue
Block a user