Admin: stopka usera w sidebarze jak w aplikacji (imie + role Admin/Trener/Cwiczcacy z /me)
This commit is contained in:
17
admin/src/api/queries.ts
Normal file
17
admin/src/api/queries.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { api } from './client';
|
||||
import type { User } from './types';
|
||||
|
||||
/** Bieżący użytkownik (admin) — profil + członkostwa, jak `useMe` w aplikacji. */
|
||||
export function useMe() {
|
||||
return useQuery({
|
||||
queryKey: ['me'],
|
||||
queryFn: () => api<{ user: User; memberships: NonNullable<User['memberships']> }>('/me'),
|
||||
});
|
||||
}
|
||||
|
||||
/** True, gdy użytkownik ma rolę TRAINER w co najmniej jednej siłowni. */
|
||||
export function useIsTrainer() {
|
||||
const { data } = useMe();
|
||||
return (data?.memberships ?? []).some((m) => m.roles.includes('TRAINER'));
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
type LucideIcon,
|
||||
} from 'lucide-react';
|
||||
import { useAuth } from '../store/auth';
|
||||
import { useIsTrainer } from '../api/queries';
|
||||
import { APP_URL } from '../config';
|
||||
import { Button, ThemeToggle } from './ui';
|
||||
import { Topbar } from '../topbar';
|
||||
@@ -182,7 +183,12 @@ export default function Layout() {
|
||||
return next;
|
||||
});
|
||||
|
||||
const fullName = user?.email ?? 'Admin';
|
||||
const isTrainer = useIsTrainer();
|
||||
const fullName =
|
||||
[user?.firstName, user?.lastName].filter(Boolean).join(' ') || user?.email || '?';
|
||||
const roles = [user?.isAdmin ? 'Admin' : null, isTrainer ? 'Trener' : 'Ćwiczący']
|
||||
.filter(Boolean)
|
||||
.join(' · ');
|
||||
|
||||
const doLogout = async () => {
|
||||
await logout();
|
||||
@@ -195,8 +201,8 @@ export default function Layout() {
|
||||
<div className="mb-3 flex items-center gap-2.5">
|
||||
<Avatar name={fullName} size={32} />
|
||||
<div className="min-w-0">
|
||||
<div className="truncate text-[13px] font-bold">{user?.email}</div>
|
||||
<div className="text-[11.5px] text-dim">Administrator</div>
|
||||
<div className="truncate text-[13px] font-bold">{user?.firstName || user?.email}</div>
|
||||
<div className="text-[11.5px] text-dim">{roles}</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
|
||||
@@ -108,7 +108,8 @@ test('strona członkostw jest tylko do odczytu — role jako badge, brak kontrol
|
||||
// dane widoczne (nazwa siłowni + rola jako badge), ale żadnej kontroki edycji —
|
||||
// jedyne miejsce zmiany członkostw to kreator edycji
|
||||
await expect(page.getByText('Fit Factory')).toBeVisible();
|
||||
await expect(page.getByText('Ćwiczący')).toBeVisible();
|
||||
// (exact: „Admin · Ćwiczący” w stopce sidebaru też zawiera ten tekst)
|
||||
await expect(page.getByText('Ćwiczący', { exact: true })).toBeVisible();
|
||||
await expect(page.getByRole('checkbox')).toHaveCount(0);
|
||||
await expect(page.getByRole('button', { name: 'Dodaj członkostwo' })).toHaveCount(0);
|
||||
await expect(page.getByRole('button', { name: 'Usuń', exact: true })).toHaveCount(0);
|
||||
|
||||
Reference in New Issue
Block a user