113 lines
4.4 KiB
TypeScript
113 lines
4.4 KiB
TypeScript
import { useEffect, type ReactNode } from 'react';
|
|
import { Routes, Route, Navigate } from 'react-router-dom';
|
|
import { useAuth } from './store/auth';
|
|
import { useMe } from './api/queries';
|
|
import Layout from './components/Layout';
|
|
import Login from './pages/Login';
|
|
import Register from './pages/Register';
|
|
import Dashboard from './pages/Dashboard';
|
|
import Plans from './pages/plans/Plans';
|
|
import PlanEditor from './pages/plans/PlanEditor';
|
|
import PlanDetail from './pages/plans/PlanDetail';
|
|
import Calendar from './pages/Calendar';
|
|
import DayExecution from './pages/DayExecution';
|
|
import Profile from './pages/profile/Profile';
|
|
import ProfileInfo from './pages/profile/ProfileInfo';
|
|
import ProfileBodyParameters from './pages/profile/ProfileBodyParameters';
|
|
import ProfileHistory from './pages/profile/ProfileHistory';
|
|
import ProfileProgress from './pages/profile/ProfileProgress';
|
|
import ProfileClaims from './pages/profile/ProfileClaims';
|
|
import Gyms from './pages/gyms/Gyms';
|
|
import GymDetail from './pages/gyms/GymDetail';
|
|
import Trainees from './pages/trainees/Trainees';
|
|
import TraineeDetail from './pages/trainees/TraineeDetail';
|
|
import Invitations from './pages/Invitations';
|
|
import Messages from './pages/messages/Messages';
|
|
import MessageThread from './pages/messages/MessageThread';
|
|
import Announcements from './pages/announcements/Announcements';
|
|
|
|
/**
|
|
* Trasa tylko dla trenerów — API i tak odda 403, ale nie pokazujemy pustej
|
|
* karty. Do czasu wczytania /me children zostają zamontowane (strony same
|
|
* pokazują szkielety), więc redirect nie powoduje przeładowania komponentów.
|
|
*/
|
|
function TrainerOnly({ children }: { children: ReactNode }) {
|
|
const { data, isLoading } = useMe();
|
|
const isTrainer = (data?.memberships ?? []).some((m) => m.roles.includes('TRAINER'));
|
|
if (!isLoading && data && !isTrainer) return <Navigate to="/" replace />;
|
|
return <>{children}</>;
|
|
}
|
|
|
|
export default function App() {
|
|
const { user, ready, init } = useAuth();
|
|
|
|
useEffect(() => {
|
|
void init();
|
|
}, [init]);
|
|
|
|
if (!ready) {
|
|
return <div className="flex items-center justify-center min-h-dvh text-dim">Ładowanie…</div>;
|
|
}
|
|
|
|
return (
|
|
<Routes>
|
|
<Route path="/login" element={user ? <Navigate to="/" replace /> : <Login />} />
|
|
<Route path="/register" element={user ? <Navigate to="/" replace /> : <Register />} />
|
|
<Route element={user ? <Layout /> : <Navigate to="/login" replace />}>
|
|
<Route path="/" element={<Dashboard />} />
|
|
<Route path="/plans" element={<Plans />} />
|
|
<Route path="/plans/new" element={<PlanEditor />} />
|
|
<Route path="/plans/:id/edit" element={<PlanEditor />} />
|
|
<Route path="/plans/:id" element={<PlanDetail />} />
|
|
<Route path="/calendar" element={<Calendar />} />
|
|
<Route path="/day/:dayId" element={<DayExecution />} />
|
|
<Route path="/gyms" element={<Gyms />} />
|
|
<Route path="/gyms/:id" element={<GymDetail />} />
|
|
<Route
|
|
path="/trainees"
|
|
element={
|
|
<TrainerOnly>
|
|
<Trainees />
|
|
</TrainerOnly>
|
|
}
|
|
/>
|
|
<Route
|
|
path="/trainees/:userId"
|
|
element={
|
|
<TrainerOnly>
|
|
<TraineeDetail />
|
|
</TrainerOnly>
|
|
}
|
|
/>
|
|
<Route
|
|
path="/trainees/:userId/plans/new"
|
|
element={
|
|
<TrainerOnly>
|
|
<PlanEditor />
|
|
</TrainerOnly>
|
|
}
|
|
/>
|
|
<Route
|
|
path="/trainees/:userId/plans/:planId/edit"
|
|
element={
|
|
<TrainerOnly>
|
|
<PlanEditor />
|
|
</TrainerOnly>
|
|
}
|
|
/>
|
|
<Route path="/invitations" element={<Invitations />} />
|
|
<Route path="/messages" element={<Messages />} />
|
|
<Route path="/messages/:coachingId" element={<MessageThread />} />
|
|
<Route path="/announcements" element={<Announcements />} />
|
|
<Route path="/profile" element={<Profile />} />
|
|
<Route path="/profile/info" element={<ProfileInfo />} />
|
|
<Route path="/profile/body-parameters" element={<ProfileBodyParameters />} />
|
|
<Route path="/profile/history" element={<ProfileHistory />} />
|
|
<Route path="/profile/progress" element={<ProfileProgress />} />
|
|
<Route path="/profile/claims" element={<ProfileClaims />} />
|
|
</Route>
|
|
<Route path="*" element={<Navigate to="/" replace />} />
|
|
</Routes>
|
|
);
|
|
}
|