Admin: akcje usera w menu kontekstowym i czytelne rozroznienie czlonkostwa od uprawnien
- UserDetail: wszystkie operacje (Edytuj, sekcje, Usun) przeniesione do menu kontekstowego (topbar) zamiast hero+strefy; usuwanie przez ConfirmDialog - UserForm: Zapisz jako akcja w menu kontekstowym (dostepny na dlugim formularzu) - UserForm: rozroznienie 'Dodaj do sirowni' (czlonkostwo) od 'Uprawnienia' (Trener/Wlasciciel) - wiersz + intro + opis roli Wlasciciel
This commit is contained in:
@@ -30,7 +30,7 @@ export const GYM_ROLE_META: Record<GymRole, RoleMeta> = {
|
||||
},
|
||||
OWNER: {
|
||||
label: 'Właściciel',
|
||||
summary: 'Pełne zarządzanie daną siłownią.',
|
||||
summary: 'Zarządza siłownią — to uprawnienie, nie samo członkostwo.',
|
||||
grants: [
|
||||
'Dane, godziny i adres siłowni',
|
||||
'Sprzęt, zdjęcia i grafik',
|
||||
|
||||
@@ -1,22 +1,23 @@
|
||||
import { useState } from 'react';
|
||||
import { Link, useNavigate, useParams } from 'react-router-dom';
|
||||
import {
|
||||
Activity,
|
||||
Building2,
|
||||
ChevronRight,
|
||||
Dumbbell,
|
||||
FileText,
|
||||
Flame,
|
||||
History,
|
||||
Pencil,
|
||||
ShieldCheck,
|
||||
Scale,
|
||||
TrendingUp,
|
||||
User,
|
||||
Users,
|
||||
UserCog,
|
||||
type LucideIcon,
|
||||
} from 'lucide-react';
|
||||
import { useTopbar } from '../../topbar';
|
||||
import { useTopbar, type TopbarCustomGroup } from '../../topbar';
|
||||
import { api } from '../../api/client';
|
||||
import { Badge, Button, Card, ErrorText, Loading } from '../../components/ui';
|
||||
import { buttonVariants } from '../../components/ui/button';
|
||||
import { Badge, Card, ConfirmDialog, ErrorText, Loading } from '../../components/ui';
|
||||
import { Avatar } from '../../components/Avatar';
|
||||
import { displayName, fmtDate, fmtVolume } from '../../lib/format';
|
||||
import {
|
||||
@@ -118,10 +119,50 @@ export default function UserDetail() {
|
||||
const { data: hist } = useUserHistory(id);
|
||||
const { data: ks } = useUserKillstreak(id);
|
||||
const { data: claimsData } = useUserClaims(id);
|
||||
const [confirmOpen, setConfirmOpen] = useState(false);
|
||||
const [busy, setBusy] = useState(false);
|
||||
|
||||
// Operacje admina w menu kontekstowym (topbar) zamiast przycisków w treści.
|
||||
const groups: TopbarCustomGroup[] | undefined = user
|
||||
? [
|
||||
{
|
||||
id: 'data',
|
||||
label: 'Dane',
|
||||
icon: UserCog,
|
||||
actions: [
|
||||
{ label: 'Edytuj', to: `/users/${user.id}/edit` },
|
||||
{ label: 'Dane osobowe', to: `/users/${user.id}/info` },
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 'training',
|
||||
label: 'Trening',
|
||||
icon: Activity,
|
||||
actions: [
|
||||
{ label: 'Parametry ciała', to: `/users/${user.id}/body-parameters` },
|
||||
{ label: 'Historia treningów', to: `/users/${user.id}/history` },
|
||||
{ label: 'Postępy', to: `/users/${user.id}/progress` },
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 'access',
|
||||
label: 'Dostęp',
|
||||
icon: ShieldCheck,
|
||||
actions: [
|
||||
{ label: 'Członkostwa', to: `/users/${user.id}/memberships` },
|
||||
{ label: 'Wnioski', to: `/users/${user.id}/claims` },
|
||||
],
|
||||
},
|
||||
]
|
||||
: undefined;
|
||||
|
||||
useTopbar({
|
||||
title: user ? userDisplayName(user) : 'Użytkownik',
|
||||
back: '/users',
|
||||
groups,
|
||||
actions: user
|
||||
? [{ label: 'Usuń', variant: 'danger', onClick: () => setConfirmOpen(true) }]
|
||||
: [],
|
||||
});
|
||||
|
||||
if (isLoading) return <Loading />;
|
||||
@@ -129,12 +170,14 @@ export default function UserDetail() {
|
||||
if (!user) return <ErrorText>Nie znaleziono użytkownika.</ErrorText>;
|
||||
|
||||
const remove = async () => {
|
||||
if (!confirm('Usunąć użytkownika? Jego plany i sesje zostaną usunięte.')) return;
|
||||
setBusy(true);
|
||||
try {
|
||||
await api(`/admin/users/${user.id}`, { method: 'DELETE' });
|
||||
navigate('/users');
|
||||
} catch (err) {
|
||||
alert(err instanceof Error ? err.message : 'Błąd');
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -195,19 +238,15 @@ export default function UserDetail() {
|
||||
{name}
|
||||
</div>
|
||||
<div className="truncate text-dim text-[0.8125rem]">{user.email}</div>
|
||||
<div className="text-[0.75rem] text-dim">
|
||||
Utworzony {new Date(user.createdAt).toLocaleDateString('pl-PL')}
|
||||
</div>
|
||||
</div>
|
||||
<span className="flex flex-wrap items-center gap-1.5">
|
||||
{user.nickname && <Badge color="blue">@{user.nickname}</Badge>}
|
||||
{user.isAdmin && <Badge color="blue">Admin</Badge>}
|
||||
{memberships.length > 0 && <Badge>{memberships.length}</Badge>}
|
||||
</span>
|
||||
<Link
|
||||
to={`/users/${user.id}/edit`}
|
||||
className={buttonVariants({ variant: 'secondary', size: 'sm' })}
|
||||
>
|
||||
<Pencil className="h-4 w-4" />
|
||||
Edytuj
|
||||
</Link>
|
||||
</div>
|
||||
|
||||
{/* Pasek mini-statystyk: 2×2 na mobile, 4 w rzędzie na desktop */}
|
||||
@@ -298,23 +337,16 @@ export default function UserDetail() {
|
||||
</div>
|
||||
</Card>
|
||||
|
||||
{/* Strefa administratora — akcje poza wzorcem z aplikacji */}
|
||||
<Card>
|
||||
<div className="mb-3 flex items-center gap-2.5">
|
||||
<span className="grid h-8 w-8 place-items-center rounded-[10px] bg-danger/10 text-danger">
|
||||
<Users className="h-4 w-4" />
|
||||
</span>
|
||||
<div>
|
||||
<div className="text-[0.95rem] font-semibold">Strefa administratora</div>
|
||||
<div className="text-[0.8125rem] text-dim">
|
||||
Użytkownik utworzony {new Date(user.createdAt).toLocaleDateString('pl-PL')}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<Button variant="danger" onClick={() => void remove()}>
|
||||
Usuń użytkownika
|
||||
</Button>
|
||||
</Card>
|
||||
<ConfirmDialog
|
||||
open={confirmOpen}
|
||||
onClose={() => setConfirmOpen(false)}
|
||||
onConfirm={() => void remove()}
|
||||
title="Usunąć użytkownika?"
|
||||
description="Jego plany i sesje zostaną usunięte. Tej operacji nie da się cofnąć."
|
||||
confirmLabel="Usuń"
|
||||
danger
|
||||
busy={busy}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,14 @@
|
||||
import { useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { useNavigate, useParams } from 'react-router-dom';
|
||||
import { ChevronDown, ShieldCheck, Trash2, UserPlus } from 'lucide-react';
|
||||
import {
|
||||
Building2,
|
||||
Check,
|
||||
ChevronDown,
|
||||
KeyRound,
|
||||
ShieldCheck,
|
||||
Trash2,
|
||||
UserPlus,
|
||||
} from 'lucide-react';
|
||||
import { api } from '../../api/client';
|
||||
import type { Gym, GymRole, User } from '../../api/types';
|
||||
import { ADMIN_ROLE_META } from '../../lib/roles';
|
||||
@@ -48,12 +56,20 @@ function AdminExplain({ open }: { open: boolean }) {
|
||||
);
|
||||
}
|
||||
|
||||
/** Krótkie intro sekcji ról — szczegóły każdej roli pokazuje `GymRolePicker`. */
|
||||
/** Krótkie intro sekcji ról — rozróżnia „dodanie do siłowni” od „nadania uprawnień”. */
|
||||
function RoleIntro() {
|
||||
return (
|
||||
<p className="mb-4 text-[0.8125rem] text-dim">
|
||||
Wybierz siłownię i rolę — po kliknięciu roli zobaczysz, co dokładnie jej udostępnia.
|
||||
</p>
|
||||
<div className="mb-4 space-y-1 text-[0.8125rem] text-dim">
|
||||
<p>
|
||||
<span className="font-semibold text-ink">Dodanie do siłowni</span> = osoba może tam trenować
|
||||
(rola „Ćwiczący”).
|
||||
</p>
|
||||
<p>
|
||||
<span className="font-semibold text-ink">Uprawnienia</span> (Trener, Właściciel) to coś
|
||||
więcej niż członkostwo — dają zarządzanie ludźmi lub całą siłownią. Nie musisz ich nadawać,
|
||||
żeby po prostu dopisać kogoś na listę.
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -63,10 +79,21 @@ export default function UserForm() {
|
||||
const navigate = useNavigate();
|
||||
const { user, isLoading } = useAdminUser(id);
|
||||
const { data: gymsData } = useUserGyms();
|
||||
const formRef = useRef<HTMLFormElement>(null);
|
||||
const [busy, setBusy] = useState(false);
|
||||
|
||||
// Zapis jako akcja w menu kontekstowym (topbar) — formularz bywa długi,
|
||||
// przycisk zapisu powinien być dostępny zawsze, nie tylko na dole strony.
|
||||
const submitFromMenu = () => {
|
||||
formRef.current?.requestSubmit();
|
||||
};
|
||||
|
||||
useTopbar({
|
||||
title: 'Użytkownicy',
|
||||
back: isEdit && id ? `/users/${id}` : '/users',
|
||||
actions: [{ label: 'Zapisz', icon: Check, onClick: submitFromMenu, disabled: busy }],
|
||||
alwaysInlineActions: true,
|
||||
flatActionsMobile: true,
|
||||
});
|
||||
|
||||
const gyms: Gym[] = gymsData?.gyms ?? [];
|
||||
@@ -84,7 +111,6 @@ export default function UserForm() {
|
||||
{ key: nextKey(), gymId: '', roles: ['MEMBER'], existing: false },
|
||||
]);
|
||||
const [error, setError] = useState('');
|
||||
const [busy, setBusy] = useState(false);
|
||||
|
||||
// W edycji: wypełnij formularz danymi użytkownika (raz, po załadowaniu).
|
||||
const seededRef = useRef(false);
|
||||
@@ -226,7 +252,7 @@ export default function UserForm() {
|
||||
</p>
|
||||
)}
|
||||
|
||||
<form onSubmit={(e) => void submit(e)} className="space-y-4">
|
||||
<form ref={formRef} onSubmit={(e) => void submit(e)} className="space-y-4">
|
||||
{/* Krok 1 — dane */}
|
||||
<Card>
|
||||
<div className="mb-3 flex items-center gap-2.5">
|
||||
@@ -356,7 +382,10 @@ export default function UserForm() {
|
||||
</div>
|
||||
<div className="grid grid-cols-1 gap-x-4 gap-y-3 lg:grid-cols-2">
|
||||
<div>
|
||||
<div className="mb-1.5 text-[0.8125rem] font-semibold text-dim">Siłownia</div>
|
||||
<div className="mb-1.5 flex items-center gap-1.5 text-[0.8125rem] font-semibold text-dim">
|
||||
<Building2 className="h-3.5 w-3.5" />
|
||||
Dodaj do siłowni
|
||||
</div>
|
||||
<GymPicker
|
||||
gyms={gymsFor(r.key)}
|
||||
value={r.gymId}
|
||||
@@ -364,7 +393,10 @@ export default function UserForm() {
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<div className="mb-1.5 text-[0.8125rem] font-semibold text-dim">Role</div>
|
||||
<div className="mb-1.5 flex items-center gap-1.5 text-[0.8125rem] font-semibold text-dim">
|
||||
<KeyRound className="h-3.5 w-3.5" />
|
||||
Uprawnienia (opcjonalne)
|
||||
</div>
|
||||
<GymRolePicker
|
||||
value={r.roles}
|
||||
onChange={(roles) => updateRow(r.key, { roles })}
|
||||
|
||||
@@ -181,7 +181,7 @@ test('kreator edycji użytkownika: wyjaśnia role i zapisuje zmianę roli PATCHe
|
||||
await loginAsAdmin(page);
|
||||
await page.getByRole('link', { name: 'Użytkownicy' }).click();
|
||||
await page.getByRole('link', { name: 'Jan Kowalski' }).click();
|
||||
await page.getByRole('link', { name: 'Edytuj' }).click();
|
||||
await page.getByRole('button', { name: 'Edytuj' }).click();
|
||||
await expect(page).toHaveURL(/\/users\/user-1\/edit$/);
|
||||
await expect(page.getByRole('heading', { name: 'Edycja użytkownika' })).toBeVisible();
|
||||
// dane i istniejące członkostwo wczytane
|
||||
@@ -216,6 +216,40 @@ test('przycisk w prawym górnym rogu prowadzi do formularza dodawania', async ({
|
||||
await expect(page.getByRole('button', { name: 'Nowy użytkownik' })).toBeVisible();
|
||||
});
|
||||
|
||||
test('menu kontekstowe formularza „Nowy użytkownik” ma Zapisz, który wysyła POST', async ({
|
||||
page,
|
||||
}) => {
|
||||
mockApi(page, { gyms: { gyms: [TEST_GYM] } });
|
||||
await loginAsAdmin(page);
|
||||
await page.getByRole('link', { name: 'Użytkownicy' }).click();
|
||||
await page.getByRole('button', { name: 'Nowy użytkownik' }).click();
|
||||
await expect(page.getByRole('heading', { name: 'Nowy użytkownik' })).toBeVisible();
|
||||
|
||||
// akcja Zapisz w menu kontekstowym (nagłówku) — widoczna, mimo że formularz jej nie wymaga
|
||||
await expect(page.getByRole('button', { name: 'Zapisz' })).toBeVisible();
|
||||
|
||||
// POST tworzenia zwraca usera (mock listy zwraca { users }, tu nadpisujemy na { user })
|
||||
await page.route('**/api/v1/admin/users', (route) => {
|
||||
if (route.request().method() !== 'POST') return route.fallback();
|
||||
return route.fulfill({
|
||||
status: 200,
|
||||
contentType: 'application/json',
|
||||
body: JSON.stringify({ user: { ...TEST_USER, id: 'user-new', email: 'nowy@fitos.pl' } }),
|
||||
});
|
||||
});
|
||||
|
||||
await page.locator('input[type="email"]').fill('nowy@fitos.pl');
|
||||
await page.locator('input[type="password"]').fill('Password123!');
|
||||
|
||||
const postPromise = page.waitForRequest(
|
||||
(req) => req.method() === 'POST' && req.url().endsWith('/admin/users'),
|
||||
);
|
||||
await page.getByRole('button', { name: 'Zapisz' }).click();
|
||||
const req = await postPromise;
|
||||
expect(req.postDataJSON()).toMatchObject({ email: 'nowy@fitos.pl', isAdmin: false });
|
||||
await expect(page).toHaveURL(/\/users\/user-new$/);
|
||||
});
|
||||
|
||||
test('wyszukiwarka w menu kontekstowym filtruje listę użytkowników', async ({ page }) => {
|
||||
const anna = {
|
||||
...TEST_USER,
|
||||
|
||||
Reference in New Issue
Block a user