Admin: kreator usera - krok 2 Role (panel) oddzielony od kroku 3 Czlonkostwa, stan pusty z zacheta do pierwszej silowni

This commit is contained in:
tisachi
2026-10-16 22:39:44 +02:00
parent cc7d1c6127
commit 60a4ec705f
3 changed files with 90 additions and 63 deletions

View File

@@ -1,6 +1,6 @@
import { useEffect, useMemo, useRef, useState } from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import { Check, ChevronDown, ShieldCheck, Trash2, UserPlus } from 'lucide-react';
import { Building2, Check, ChevronDown, ShieldCheck, Trash2, UserPlus } from 'lucide-react';
import { api } from '../../api/client';
import type { Gym, GymRole, User } from '../../api/types';
import { ADMIN_ROLE_META, GYM_ROLE_META } from '../../lib/roles';
@@ -95,9 +95,8 @@ export default function UserForm() {
isAdmin: false,
});
const [adminExplain, setAdminExplain] = useState(false);
const [rows, setRows] = useState<MembershipRow[]>([
{ key: nextKey(), gymId: '', roles: [], existing: false },
]);
// domyślnie zero wierszy — pierwszą siłownię dodaje admin (mile widziana w stanie pustym)
const [rows, setRows] = useState<MembershipRow[]>([]);
const [error, setError] = useState('');
// niekompletny wiersz członkostwa (rola bez siłowni / siłownia bez roli) — modal blokuje zapis
const [rowError, setRowError] = useState<string | null>(null);
@@ -312,16 +311,16 @@ export default function UserForm() {
</div>
</Card>
{/* Krok 2 — role i dostęp */}
{/* Krok 2 — role w panelu */}
<Card>
<div className="mb-3 flex items-center gap-2.5">
<span className="grid h-7 w-7 place-items-center rounded-full bg-accent/10 text-[0.8rem] font-bold text-accent-text">
2
</span>
<div>
<H3 className="mb-0">Role i dostęp</H3>
<H3 className="mb-0">Role</H3>
<p className="text-[0.8125rem] text-dim">
Ustal, co użytkownik może robić — w panelu i w poszczególnych siłowniach.
Co użytkownik może robić w panelu FitOS — opcjonalnie.
</p>
</div>
</div>
@@ -357,64 +356,85 @@ export default function UserForm() {
</div>
<AdminExplain open={adminExplain} />
</div>
</Card>
{/* Role w siłowniach */}
<div className="mt-4 border-t border-line pt-4">
<div className="mb-1 flex items-center justify-between gap-3">
<H3>Członkostwa w siłowniach</H3>
{/* Krok 3 — członkostwa w siłowniach */}
<Card>
<div className="mb-3 flex items-center gap-2.5">
<span className="grid h-7 w-7 place-items-center rounded-full bg-accent/10 text-[0.8rem] font-bold text-accent-text">
3
</span>
<div className="flex min-w-0 flex-1 items-center justify-between gap-3">
<div>
<H3 className="mb-0">Członkostwa w siłowniach</H3>
<p className="text-[0.8125rem] text-dim">
Dostęp do poszczególnych siłowni i role w nich — trenowanie, prowadzenie,
zarządzanie.
</p>
</div>
{filledCount > 0 && <Badge>{filledCount}</Badge>}
</div>
{rows.length === 0 && (
<p className="mb-3 text-[0.875rem] text-dim">
Brak przypisanej siłowni —{' '}
{form.isAdmin ? 'użytkownik będzie administratorem.' : 'to konto bez siłowni.'}
</p>
)}
<div className="space-y-4">
{rows.map((r, i) => {
const gym = gyms.find((g) => g.id === r.gymId);
return (
<div key={r.key} className="space-y-4 rounded-xl border border-line p-4">
<div className="flex justify-end">
<Button
type="button"
variant="ghost"
size="sm"
onClick={() => removeRow(r.key)}
aria-label={gym ? `Usuń ${gym.name}` : `Usuń siłownię ${i + 1}`}
>
<Trash2 className="h-4 w-4" />
</Button>
</div>
<div>
<Label>Siłownia</Label>
<GymPicker
gyms={gymsFor(r.key)}
value={r.gymId}
onChange={(gymId) => updateRow(r.key, { gymId })}
/>
</div>
<div>
<Label>Role</Label>
<GymRolePicker
value={r.roles}
onChange={(roles) => updateRow(r.key, { roles })}
labelPrefix={gym?.name ?? `siłownia ${i + 1}`}
minRoles={0}
/>
</div>
</div>
);
})}
</div>
<Button type="button" variant="secondary" size="sm" onClick={addRow} className="mt-3">
<UserPlus className="h-4 w-4" />
Dodaj kolejną siłownię
</Button>
</div>
{rows.length === 0 ? (
<div className="rounded-xl border border-dashed border-line px-4 py-8 text-center">
<Building2 className="mx-auto h-7 w-7 text-dim" aria-hidden />
<p className="mt-2 text-[0.95rem] font-semibold">Jeszcze bez siłowni</p>
<p className="mx-auto mt-1 max-w-md text-[0.875rem] text-dim">
Dodaj pierwszą siłownię i zaznacz rolę — albo zdecyduj później, na stronie
„Członkostwa w siłowniach” użytkownika.
</p>
<Button type="button" variant="secondary" size="sm" onClick={addRow} className="mt-4">
<UserPlus className="h-4 w-4" />
Dodaj pierwszą siłownię
</Button>
</div>
) : (
<>
<div className="space-y-4">
{rows.map((r, i) => {
const gym = gyms.find((g) => g.id === r.gymId);
return (
<div key={r.key} className="space-y-4 rounded-xl border border-line p-4">
<div className="flex justify-end">
<Button
type="button"
variant="ghost"
size="sm"
onClick={() => removeRow(r.key)}
aria-label={gym ? `Usuń ${gym.name}` : `Usuń siłownię ${i + 1}`}
>
<Trash2 className="h-4 w-4" />
</Button>
</div>
<div>
<Label>Siłownia</Label>
<GymPicker
gyms={gymsFor(r.key)}
value={r.gymId}
onChange={(gymId) => updateRow(r.key, { gymId })}
/>
</div>
<div>
<Label>Role</Label>
<GymRolePicker
value={r.roles}
onChange={(roles) => updateRow(r.key, { roles })}
labelPrefix={gym?.name ?? `siłownia ${i + 1}`}
minRoles={0}
/>
</div>
</div>
);
})}
</div>
<Button type="button" variant="secondary" size="sm" onClick={addRow} className="mt-3">
<UserPlus className="h-4 w-4" />
Dodaj kolejną siłownię
</Button>
</>
)}
</Card>
{error && <ErrorText>{error}</ErrorText>}

View File

@@ -141,7 +141,7 @@ async function submitForm() {
}
describe('admin UserForm — kreator tworzenia', () => {
it('rola startuje niezaznaczona (admin sam zaznacza), a po zaznaczeniu wyjaśnia ją', async () => {
it('startuje bez siłowni (zachęta do pierwszej), a rola domyślnie niezaznaczona', async () => {
mockRoutes([
{ match: '/admin/gyms', reply: () => ({ gyms: [GYM_A, GYM_B] }) },
{ match: '/admin/users', reply: () => ({ users: [] }) },
@@ -149,7 +149,10 @@ describe('admin UserForm — kreator tworzenia', () => {
const user = userEvent.setup();
renderForm('/users/new');
expect(await screen.findByRole('heading', { name: 'Nowy użytkownik' })).toBeInTheDocument();
// żadna rola nie jest zaznaczona domyślnie — decyzję podejmuje admin
// stan pusty: brak wierszy, żadna rola, zachęta do dodania pierwszej siłowni
expect(screen.getByText('Jeszcze bez siłowni')).toBeInTheDocument();
expect(screen.queryByRole('checkbox', { name: /siłownia 1/ })).not.toBeInTheDocument();
await user.click(screen.getByRole('button', { name: 'Dodaj pierwszą siłownię' }));
expect(screen.getByText(/wybierz rolę, aby zobaczyć/i)).toBeInTheDocument();
await user.click(screen.getByRole('checkbox', { name: 'Ćwiczący — siłownia 1' }));
expect(
@@ -175,6 +178,7 @@ describe('admin UserForm — kreator tworzenia', () => {
await user.type(container.querySelector('input[type="password"]')!, 'Password123!');
// wybierz siłownię (placeholder „Szukaj siłowni…”) i zaznacz rolę Trener
await user.click(screen.getByRole('button', { name: 'Dodaj pierwszą siłownię' }));
const gymBox = container.querySelector<HTMLInputElement>('input[role="combobox"]')!;
await user.click(gymBox);
await user.click(await screen.findByRole('option', { name: /Steel Gym/ }));
@@ -210,6 +214,7 @@ describe('admin UserForm — kreator tworzenia', () => {
const { container } = renderForm('/users/new');
await user.type(container.querySelector('input[type="email"]')!, 'nowy4@test.local');
await user.type(container.querySelector('input[type="password"]')!, 'Password123!');
await user.click(screen.getByRole('button', { name: 'Dodaj pierwszą siłownię' }));
await user.click(screen.getByRole('checkbox', { name: 'Właściciel — siłownia 1' }));
await submitForm();
@@ -231,6 +236,7 @@ describe('admin UserForm — kreator tworzenia', () => {
const { container } = renderForm('/users/new');
await user.type(container.querySelector('input[type="email"]')!, 'nowy5@test.local');
await user.type(container.querySelector('input[type="password"]')!, 'Password123!');
await user.click(screen.getByRole('button', { name: 'Dodaj pierwszą siłownię' }));
await user.click(container.querySelector('input[role="combobox"]')!);
await user.click(await screen.findByRole('option', { name: /Steel Gym/ }));

View File

@@ -264,6 +264,7 @@ test('kreator blokuje zapis niekompletnego członkostwa — modal „Nie można
await page.locator('input[type="password"]').fill('Password123!');
// rola bez wybranej siłowni → zapis wzbroniony
await page.getByRole('button', { name: 'Dodaj pierwszą siłownię' }).click();
await page.getByRole('checkbox', { name: 'Trener — siłownia 1' }).click();
await page.getByRole('button', { name: 'Zapisz' }).click();
const dialog = page.getByRole('dialog', { name: 'Nie można zapisać' });