Admin: kreator dodawania i edycji usera z czytelnym przydzielaniem rol
- Uzytkownik: wspolny kreator (UserForm) dla /users/new i /users/:id/edit — krok Dane + krok Role i dostep - Role silowi (Cwiczacy/Trener/Wlasciciel) z widocznym wyjaśnieniem co daja (GymRolePicker), przy tworzeniu od razu tworzone czlonkostwa - Administrator: suwak zamiast Suchego Tak/Nie, z opisem uprawnien (AccountCard w UserInfo) - Wyodrebnione GymPicker i modul lib/roles.ts (opisy rol); wyczyszczone opisy w e2e (listitem/H1) Testy: 72 unit + 12 e2e admin zielone
This commit is contained in:
97
admin/src/components/GymPicker.tsx
Normal file
97
admin/src/components/GymPicker.tsx
Normal file
@@ -0,0 +1,97 @@
|
||||
import { useState } from 'react';
|
||||
import { Search, X } from 'lucide-react';
|
||||
import type { Gym } from '../api/types';
|
||||
|
||||
/**
|
||||
* Wyszukiwarka siłowni (combobox) — wybór jednej siłowni z listy.
|
||||
* Wykorzystywana w kreatorze użytkownika i na stronie członkostw.
|
||||
*/
|
||||
export function GymPicker({
|
||||
gyms,
|
||||
value,
|
||||
onChange,
|
||||
}: {
|
||||
gyms: Gym[];
|
||||
value: string;
|
||||
onChange: (id: string) => void;
|
||||
}) {
|
||||
const [query, setQuery] = useState('');
|
||||
const [open, setOpen] = useState(false);
|
||||
const selected = gyms.find((g) => g.id === value);
|
||||
const filtered = gyms.filter((g) => g.name.toLowerCase().includes(query.trim().toLowerCase()));
|
||||
|
||||
const close = () => {
|
||||
setOpen(false);
|
||||
setQuery('');
|
||||
};
|
||||
|
||||
const handleBlur = (e: React.FocusEvent<HTMLDivElement>) => {
|
||||
if (e.currentTarget.contains(e.relatedTarget as Node)) return;
|
||||
close();
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="relative" onBlur={handleBlur}>
|
||||
<div className="flex items-center gap-2 rounded-xl border border-line bg-input px-3 focus-within:border-accent">
|
||||
<Search className="h-4 w-4 shrink-0 text-dim" />
|
||||
<input
|
||||
role="combobox"
|
||||
aria-expanded={open}
|
||||
aria-label="Szukaj siłowni"
|
||||
className="w-full bg-transparent py-2.5 text-[0.95rem] focus:outline-none"
|
||||
placeholder={selected && !open ? '' : 'Szukaj siłowni…'}
|
||||
value={open ? query : (selected?.name ?? '')}
|
||||
onChange={(e) => {
|
||||
setQuery(e.target.value);
|
||||
setOpen(true);
|
||||
}}
|
||||
onFocus={() => setOpen(true)}
|
||||
/>
|
||||
{selected && !open && (
|
||||
<button
|
||||
type="button"
|
||||
aria-label="Wyczyść wybór siłowni"
|
||||
onClick={() => onChange('')}
|
||||
className="shrink-0 cursor-pointer text-dim hover:text-ink"
|
||||
>
|
||||
<X className="h-4 w-4" />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
{open && (
|
||||
<div
|
||||
role="listbox"
|
||||
aria-label="Lista siłowni"
|
||||
className="absolute z-20 mt-1.5 max-h-56 w-full overflow-y-auto rounded-xl border border-line bg-card shadow-xl"
|
||||
>
|
||||
{filtered.length === 0 ? (
|
||||
<div className="px-3.5 py-3 text-[0.875rem] text-dim">
|
||||
{query.trim() ? `Brak siłowni o nazwie „${query.trim()}”` : 'Brak dostępnych siłowni'}
|
||||
</div>
|
||||
) : (
|
||||
filtered.map((g) => (
|
||||
<button
|
||||
key={g.id}
|
||||
type="button"
|
||||
role="option"
|
||||
aria-selected={g.id === value}
|
||||
onClick={() => {
|
||||
onChange(g.id);
|
||||
close();
|
||||
}}
|
||||
className={`flex w-full cursor-pointer items-center justify-between gap-3 px-3.5 py-2.5 text-left text-[0.95rem] hover:bg-line/40 ${
|
||||
g.id === value ? 'font-semibold text-accent-text' : ''
|
||||
}`}
|
||||
>
|
||||
<span className="min-w-0 truncate">{g.name}</span>
|
||||
{g.location && (
|
||||
<span className="shrink-0 text-[0.875rem] text-dim">{g.location}</span>
|
||||
)}
|
||||
</button>
|
||||
))
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
107
admin/src/components/GymRolePicker.tsx
Normal file
107
admin/src/components/GymRolePicker.tsx
Normal file
@@ -0,0 +1,107 @@
|
||||
import { useState } from 'react';
|
||||
import { Check } from 'lucide-react';
|
||||
import type { GymRole } from '../api/types';
|
||||
import { GYM_ROLE_META, GYM_ROLE_ORDER, GYM_ROLE_PILL_ACTIVE } from '../lib/roles';
|
||||
import { cn } from '../lib/utils';
|
||||
|
||||
const PILL_INACTIVE = 'bg-transparent text-dim border-line hover:text-ink hover:border-dim';
|
||||
|
||||
interface GymRolePickerProps {
|
||||
value: GymRole[];
|
||||
onChange: (roles: GymRole[]) => void;
|
||||
/** Przedrostek aria-label pigułek (np. nazwa siłowni lub „Dodaj”), do testów i A11y. */
|
||||
labelPrefix: string;
|
||||
/** Uniemożliwia odznaczenie, gdy została tylko `minRoles` (domyślnie 1). */
|
||||
minRoles?: number;
|
||||
/** Tytuł blokady ostatniej roli (title na zablokowanej pigułce). */
|
||||
lockTitle?: string;
|
||||
/** Pod pigułkami pokazuje wyjaśnienie wskazanej/ostatnio wybranej roli. Domyślnie true. */
|
||||
showExplain?: boolean;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wybór ról siłowni (Właściciel / Trener / Ćwiczący) z widocznym wyjaśnieniem,
|
||||
* co daje każda z nich — żeby przydzielanie nie było zgadywanką.
|
||||
*/
|
||||
export function GymRolePicker({
|
||||
value,
|
||||
onChange,
|
||||
labelPrefix,
|
||||
minRoles = 1,
|
||||
lockTitle,
|
||||
showExplain = true,
|
||||
className,
|
||||
}: GymRolePickerProps) {
|
||||
const [explained, setExplained] = useState<GymRole | null>(null);
|
||||
const locked = value.length <= minRoles;
|
||||
|
||||
const toggle = (role: GymRole) => {
|
||||
setExplained(role);
|
||||
if (value.includes(role)) {
|
||||
if (locked) return;
|
||||
onChange(value.filter((r) => r !== role));
|
||||
} else {
|
||||
onChange([...value, role]);
|
||||
}
|
||||
};
|
||||
|
||||
const shown =
|
||||
explained ??
|
||||
(value.length > 0 ? (GYM_ROLE_ORDER.find((r) => value.includes(r)) ?? null) : null);
|
||||
const meta = shown ? GYM_ROLE_META[shown] : null;
|
||||
|
||||
return (
|
||||
<div className={className}>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{GYM_ROLE_ORDER.map((role) => {
|
||||
const active = value.includes(role);
|
||||
const disabled = active && locked;
|
||||
return (
|
||||
<button
|
||||
key={role}
|
||||
type="button"
|
||||
role="checkbox"
|
||||
aria-checked={active}
|
||||
aria-label={`${GYM_ROLE_META[role].label} — ${labelPrefix}`}
|
||||
aria-description={GYM_ROLE_META[role].summary}
|
||||
title={disabled ? lockTitle : GYM_ROLE_META[role].summary}
|
||||
disabled={disabled}
|
||||
onClick={() => toggle(role)}
|
||||
className={cn(
|
||||
'inline-flex flex-1 cursor-pointer items-center justify-center gap-2 rounded-xl border px-3 py-2 text-[0.95rem] font-semibold transition-colors disabled:cursor-not-allowed disabled:opacity-40',
|
||||
active ? GYM_ROLE_PILL_ACTIVE[role] : PILL_INACTIVE,
|
||||
)}
|
||||
>
|
||||
<Check className={cn('h-4 w-4', active ? '' : 'opacity-0')} />
|
||||
{GYM_ROLE_META[role].label}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
{showExplain && (
|
||||
<div className="mt-2.5 min-h-[2.5rem] rounded-xl border border-line bg-input/40 px-3 py-2.5 text-[0.875rem]">
|
||||
{meta ? (
|
||||
<>
|
||||
<span className="font-semibold text-ink">{meta.label}: </span>
|
||||
<span className="text-dim">{meta.summary}</span>
|
||||
<ul className="mt-1.5 space-y-0.5 text-dim">
|
||||
{meta.grants.map((g) => (
|
||||
<li key={g} className="flex gap-1.5">
|
||||
<span className="text-accent-text">•</span>
|
||||
<span>{g}</span>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</>
|
||||
) : (
|
||||
<span className="text-dim">
|
||||
Wybierz rolę, aby zobaczyć, co dokładnie daje. Ćwiczący = trenuje, Trener = prowadzi
|
||||
podopiecznych, Właściciel = zarządza siłownią.
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user