add: more commits
Signed-off-by: Kenneth Obsequio <k80308392@gmail.com>
This commit is contained in:
@@ -12,10 +12,13 @@ export function useClientNotifications() {
|
||||
return ctx;
|
||||
}
|
||||
|
||||
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 [loading, setLoading] = useState(false);
|
||||
const [pagination, setPagination] = useState(DEFAULT_PAGINATION);
|
||||
const intervalRef = useRef(null);
|
||||
const pollSpeedRef = useRef(POLL_INTERVAL_NORMAL);
|
||||
|
||||
@@ -28,13 +31,14 @@ export function ClientNotificationProvider({ children }) {
|
||||
}
|
||||
}, []);
|
||||
|
||||
const fetchNotifications = useCallback(async () => {
|
||||
const fetchNotifications = useCallback(async (page = 1, limit = 10) => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const res = await api.get("/client/notifications?limit=20");
|
||||
const res = await api.get(`/client/notifications?limit=${limit}&page=${page}`);
|
||||
const rows = res.data?.data?.notifications ?? [];
|
||||
const pag = res.data?.data?.pagination ?? { page, limit, pages: 1, total: rows.length };
|
||||
setNotifications(rows);
|
||||
setUnseenCount(rows.filter(n => !n.seen).length);
|
||||
setPagination({ ...pag, pages: Math.max(1, pag.pages) });
|
||||
} catch {
|
||||
// silent
|
||||
} finally {
|
||||
@@ -42,6 +46,18 @@ export function ClientNotificationProvider({ children }) {
|
||||
}
|
||||
}, []);
|
||||
|
||||
const clearAll = useCallback(async () => {
|
||||
try {
|
||||
await api.delete("/client/notifications/clear-all");
|
||||
setNotifications([]);
|
||||
setUnseenCount(0);
|
||||
setPagination(DEFAULT_PAGINATION);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}, []);
|
||||
|
||||
const markSeen = useCallback(async (id) => {
|
||||
try {
|
||||
await api.patch(`/client/notifications/${id}/seen`);
|
||||
@@ -93,9 +109,11 @@ export function ClientNotificationProvider({ children }) {
|
||||
notifications,
|
||||
unseenCount,
|
||||
loading,
|
||||
pagination,
|
||||
fetchNotifications,
|
||||
markSeen,
|
||||
markAllSeen,
|
||||
clearAll,
|
||||
accelerate,
|
||||
decelerate,
|
||||
}}>
|
||||
|
||||
Reference in New Issue
Block a user