feat(panel): connect dashboard page to representation monthly stats
تبدیل صفحه dashboard پنل به Server Component که آمار ماهانه نماینده را از API میگیرد (year/month میلادی جاری) و با adaptRepresentationDashboard به propهای کامپوننت map میکند. حذف داده mock. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -1,8 +1,40 @@
|
|||||||
import DashboardPage from "@/components/panel/dashboard";
|
import DashboardPage from "@/components/panel/dashboard";
|
||||||
import Information from "@/data/information.json";
|
import { cookies } from "next/headers";
|
||||||
|
import { fetchReq } from "@/lib/req";
|
||||||
|
import { safeJsonParse } from "@/lib/sanitize";
|
||||||
|
import { adaptRepresentationDashboard } from "@/helper";
|
||||||
|
|
||||||
function Dashboard() {
|
export const dynamic = "force-dynamic";
|
||||||
return <DashboardPage data={Information} />;
|
|
||||||
|
async function Dashboard() {
|
||||||
|
const API_URL = process.env.NEXT_PUBLIC_API_URL;
|
||||||
|
let dashboard = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const cookieStore = await cookies();
|
||||||
|
const userInfo = cookieStore.get("userInfo");
|
||||||
|
const accessToken = cookieStore.get("access_token");
|
||||||
|
|
||||||
|
if (userInfo && accessToken) {
|
||||||
|
const parsedUserInfo = safeJsonParse(userInfo.value);
|
||||||
|
const representationUuid = parsedUserInfo?.representation_uuid;
|
||||||
|
|
||||||
|
if (representationUuid) {
|
||||||
|
const now = new Date();
|
||||||
|
const year = now.getFullYear();
|
||||||
|
const month = now.getMonth() + 1;
|
||||||
|
|
||||||
|
dashboard = await fetchReq(
|
||||||
|
`${API_URL}/api/v1/representation/${representationUuid}/dashboard/monthly?year=${year}&month=${month}`,
|
||||||
|
{ headers: { Authorization: `Bearer ${accessToken.value}` } }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching representation dashboard:", error);
|
||||||
|
}
|
||||||
|
|
||||||
|
return <DashboardPage data={adaptRepresentationDashboard(dashboard)} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Dashboard;
|
export default Dashboard;
|
||||||
|
|||||||
@@ -696,3 +696,18 @@ export function adaptRepresentationInfo(representationInfo) {
|
|||||||
chart_amount_income: [],
|
chart_amount_income: [],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function adaptRepresentationDashboard(dashboard) {
|
||||||
|
const stats = dashboard?.data?.stats ?? dashboard?.stats ?? {};
|
||||||
|
const daily = Array.isArray(stats.daily) ? stats.daily : [];
|
||||||
|
|
||||||
|
return {
|
||||||
|
number_of_patients: stats.total_appointments ?? 0,
|
||||||
|
today_turns: daily.length ? daily[daily.length - 1].appointments ?? 0 : 0,
|
||||||
|
payments: stats.total_revenue_rials ?? 0,
|
||||||
|
commission_rials: stats.commission_rials ?? 0,
|
||||||
|
chart_amount_income: daily.map((d) => d.revenue ?? 0),
|
||||||
|
list_of_doctors_appointments: [],
|
||||||
|
todays_activities: [],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user