phase-1: data model, auth, permissions, masters, settings, seed
This commit is contained in:
+102
-19
@@ -1,7 +1,12 @@
|
||||
import { BrowserRouter, Routes, Route } from "react-router-dom";
|
||||
import { useEffect } from "react";
|
||||
import { BrowserRouter, Routes, Route, Navigate } from "react-router-dom";
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
import { AppShell } from "@/components/layout/AppShell";
|
||||
import { DataTableDemoPage } from "@/pages/DataTableDemoPage";
|
||||
import { LoginPage } from "@/pages/LoginPage";
|
||||
import { AccessDeniedPage } from "@/pages/AccessDeniedPage";
|
||||
import { MastersPage } from "@/pages/MastersPage";
|
||||
import { SettingsPage } from "@/pages/SettingsPage";
|
||||
import {
|
||||
DashboardPage,
|
||||
AssetsPage,
|
||||
@@ -14,34 +19,112 @@ import {
|
||||
VisitsPage,
|
||||
ReportsPage,
|
||||
NotificationsPage,
|
||||
SettingsPage,
|
||||
} from "@/pages/stubs";
|
||||
import { useAuthStore } from "@/hooks/useAuth";
|
||||
import { can, type Capability } from "@/lib/permissions";
|
||||
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: { queries: { retry: 1, staleTime: 30_000 } },
|
||||
});
|
||||
|
||||
function ProtectedRoute({ children }: { children: React.ReactNode }) {
|
||||
const { user, token, hydrating } = useAuthStore();
|
||||
|
||||
if (hydrating) {
|
||||
return (
|
||||
<div className="min-h-screen flex items-center justify-center bg-gray-50">
|
||||
<div className="text-center space-y-2">
|
||||
<div className="h-8 w-8 border-4 border-[#1e3a8a] border-t-transparent rounded-full animate-spin mx-auto" />
|
||||
<p className="text-sm text-gray-500">Loading Poona Hospital AMS...</p>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (!token || !user) {
|
||||
return <Navigate to="/login" replace />;
|
||||
}
|
||||
|
||||
return <>{children}</>;
|
||||
}
|
||||
|
||||
function RoleGuard({
|
||||
capability,
|
||||
children,
|
||||
}: {
|
||||
capability: Capability;
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
const user = useAuthStore((s) => s.user);
|
||||
if (!can(user?.role, capability)) {
|
||||
return <AccessDeniedPage />;
|
||||
}
|
||||
return <>{children}</>;
|
||||
}
|
||||
|
||||
function AppRoutes() {
|
||||
const hydrate = useAuthStore((s) => s.hydrate);
|
||||
|
||||
useEffect(() => {
|
||||
hydrate();
|
||||
}, [hydrate]);
|
||||
|
||||
return (
|
||||
<Routes>
|
||||
<Route path="/login" element={<LoginPage />} />
|
||||
<Route
|
||||
element={
|
||||
<ProtectedRoute>
|
||||
<AppShell />
|
||||
</ProtectedRoute>
|
||||
}
|
||||
>
|
||||
<Route path="/" element={<DashboardPage />} />
|
||||
<Route path="/assets" element={<AssetsPage />} />
|
||||
<Route path="/complaints" element={<ComplaintsPage />} />
|
||||
<Route path="/pm" element={<PMPage />} />
|
||||
<Route path="/calibration" element={<CalibrationPage />} />
|
||||
<Route path="/contracts" element={<ContractsPage />} />
|
||||
<Route path="/insurance" element={<InsurancePage />} />
|
||||
<Route path="/demo-equipment" element={<DemoEquipmentPage />} />
|
||||
<Route
|
||||
path="/visits"
|
||||
element={
|
||||
<RoleGuard capability="view_visits">
|
||||
<VisitsPage />
|
||||
</RoleGuard>
|
||||
}
|
||||
/>
|
||||
<Route path="/reports" element={<ReportsPage />} />
|
||||
<Route path="/notifications" element={<NotificationsPage />} />
|
||||
<Route
|
||||
path="/masters"
|
||||
element={
|
||||
<RoleGuard capability="manage_admin">
|
||||
<MastersPage />
|
||||
</RoleGuard>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/settings"
|
||||
element={
|
||||
<RoleGuard capability="manage_admin">
|
||||
<SettingsPage />
|
||||
</RoleGuard>
|
||||
}
|
||||
/>
|
||||
<Route path="/demo" element={<DataTableDemoPage />} />
|
||||
</Route>
|
||||
<Route path="*" element={<Navigate to="/" replace />} />
|
||||
</Routes>
|
||||
);
|
||||
}
|
||||
|
||||
export default function App() {
|
||||
return (
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<BrowserRouter>
|
||||
<Routes>
|
||||
<Route element={<AppShell />}>
|
||||
<Route path="/" element={<DashboardPage />} />
|
||||
<Route path="/assets" element={<AssetsPage />} />
|
||||
<Route path="/complaints" element={<ComplaintsPage />} />
|
||||
<Route path="/pm" element={<PMPage />} />
|
||||
<Route path="/calibration" element={<CalibrationPage />} />
|
||||
<Route path="/contracts" element={<ContractsPage />} />
|
||||
<Route path="/insurance" element={<InsurancePage />} />
|
||||
<Route path="/demo-equipment" element={<DemoEquipmentPage />} />
|
||||
<Route path="/visits" element={<VisitsPage />} />
|
||||
<Route path="/reports" element={<ReportsPage />} />
|
||||
<Route path="/notifications" element={<NotificationsPage />} />
|
||||
<Route path="/settings" element={<SettingsPage />} />
|
||||
<Route path="/demo" element={<DataTableDemoPage />} />
|
||||
</Route>
|
||||
</Routes>
|
||||
<AppRoutes />
|
||||
</BrowserRouter>
|
||||
</QueryClientProvider>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user