2-update driver pages [fixed image display and notyfi] 3-update car pages [fixed image] 4-build tripe pages crud 5-build some role pages 6-build audit page 7-update ui compounants 8-update saidebar and topbar 9-cheange view to be ar view 10-cheange stractcher to be app,src 11-add validation layer by yup 12-add api image-proxy to broke image corse bloken
49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
// hooks/useDashboardSummary.ts
|
|
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
import { dashboardService } from "@/src/services/dashboard.service";
|
|
import { getStoredToken } from "@/src/lib/auth";
|
|
import type { DashboardSummary } from "@/src/types/dashboard";
|
|
|
|
interface State {
|
|
data: DashboardSummary | null;
|
|
error: string | null;
|
|
loading: boolean;
|
|
}
|
|
|
|
/**
|
|
* Fetches the admin dashboard summary.
|
|
* Redirects to /login if no token is present (handled by the caller).
|
|
*
|
|
* @example
|
|
* const { data, loading, error } = useDashboardSummary();
|
|
*/
|
|
export function useDashboardSummary(): State {
|
|
const [state, setState] = useState<State>({
|
|
data: null, error: null, loading: true,
|
|
});
|
|
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
const token = getStoredToken();
|
|
|
|
if (!token) {
|
|
setState({ data: null, error: "Unauthenticated", loading: false });
|
|
return;
|
|
}
|
|
|
|
dashboardService
|
|
.getSummary(token)
|
|
.then(res => {
|
|
if (!cancelled) setState({ data: res.data, error: null, loading: false });
|
|
})
|
|
.catch((err: Error) => {
|
|
if (!cancelled) setState({ data: null, error: err.message, loading: false });
|
|
});
|
|
|
|
return () => { cancelled = true; };
|
|
}, []);
|
|
|
|
return state;
|
|
} |