mirror of
https://github.com/rgrgogu/new_starr.git
synced 2026-09-27 00:12:54 +08:00
64 lines
2.1 KiB
React
64 lines
2.1 KiB
React
import { createContext, useCallback, useContext, useState } from "react";
|
|
import api from "@/utils/api.util";
|
|
import { toast } from "sonner";
|
|
|
|
const AdminDashboardContext = createContext(null);
|
|
|
|
export function useDashboard() {
|
|
const ctx = useContext(AdminDashboardContext);
|
|
if (!ctx) throw new Error("useDashboard must be used within a DashboardProvider");
|
|
return ctx;
|
|
}
|
|
|
|
export function AdminDashboardProvider({ children }) {
|
|
const [usersDashboard, setUsersDashboard] = useState(null);
|
|
const [groupsDashboard, setGroupsDashboard] = useState(null);
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const request = useCallback(async (fn) => {
|
|
setLoading(true);
|
|
try {
|
|
return await fn();
|
|
} catch (err) {
|
|
const message = err?.response?.data?.message ?? "Something went wrong.";
|
|
toast(message);
|
|
return null;
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}, []);
|
|
|
|
// ─── GET /api/admin/dashboard/users ───────────────────────────────────────
|
|
const fetchUsersDashboard = useCallback(
|
|
() =>
|
|
request(async () => {
|
|
const res = await api.get("/admin/dashboard/users");
|
|
setUsersDashboard(res.data?.data ?? null);
|
|
return res.data;
|
|
}),
|
|
[request]
|
|
);
|
|
|
|
// ─── GET /api/admin/dashboard/groups ──────────────────────────────────────
|
|
const fetchGroupsDashboard = useCallback(
|
|
() =>
|
|
request(async () => {
|
|
const res = await api.get("/admin/dashboard/groups");
|
|
setGroupsDashboard(res.data?.data ?? null);
|
|
return res.data;
|
|
}),
|
|
[request]
|
|
);
|
|
|
|
return (
|
|
<AdminDashboardContext.Provider value={{
|
|
usersDashboard,
|
|
groupsDashboard,
|
|
loading,
|
|
fetchUsersDashboard,
|
|
fetchGroupsDashboard,
|
|
}}>
|
|
{children}
|
|
</AdminDashboardContext.Provider>
|
|
);
|
|
} |