Trening: modal dodawania cwiczenia z wyszukiwarka (odporna na diakrytyki) i filtrem partii miesniowych
This commit is contained in:
@@ -29,6 +29,7 @@ import {
|
||||
AlertModal,
|
||||
ErrorText,
|
||||
H3,
|
||||
Input,
|
||||
Modal,
|
||||
PageHeader,
|
||||
ProgressBar,
|
||||
@@ -393,7 +394,14 @@ function ExerciseAccordion({
|
||||
);
|
||||
}
|
||||
|
||||
/* Modal z listą ćwiczeń do dodania do sesji. */
|
||||
/* Normalizacja do wyszukiwarki: małe litery + bez diakrytyków („cis" znajdzie „ciąg"). */
|
||||
const normalizeSearch = (s: string) =>
|
||||
s
|
||||
.toLocaleLowerCase('pl')
|
||||
.normalize('NFD')
|
||||
.replace(/[\u0300-\u036f]/g, '');
|
||||
|
||||
/* Modal z listą ćwiczeń do dodania do sesji: wyszukiwarka + filtr kategorii (partia mięśniowa). */
|
||||
function AddExerciseModal({
|
||||
open,
|
||||
exercises,
|
||||
@@ -405,26 +413,87 @@ function AddExerciseModal({
|
||||
onClose: () => void;
|
||||
onPick: (exerciseId: string) => void;
|
||||
}) {
|
||||
const [query, setQuery] = useState('');
|
||||
const [muscle, setMuscle] = useState<string | null>(null);
|
||||
|
||||
// świeże filtry przy każdym otwarciu
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
setQuery('');
|
||||
setMuscle(null);
|
||||
}
|
||||
}, [open]);
|
||||
|
||||
const muscles = [...new Set(exercises.map((p) => p.primaryMuscle))];
|
||||
const q = normalizeSearch(query.trim());
|
||||
const filtered = exercises.filter(
|
||||
(p) =>
|
||||
(muscle === null || p.primaryMuscle === muscle) &&
|
||||
(q === '' || normalizeSearch(p.name).includes(q)),
|
||||
);
|
||||
const chip = (active: boolean) =>
|
||||
`rounded-full px-3 py-1 text-[0.8rem] font-bold whitespace-nowrap cursor-pointer transition-colors ${
|
||||
active ? 'bg-accent/14 text-accent-text' : 'bg-dim/13 text-dim hover:text-ink'
|
||||
}`;
|
||||
|
||||
return (
|
||||
<Modal open={open} onClose={onClose} title="Dodaj ćwiczenie">
|
||||
{exercises.length === 0 ? (
|
||||
<p className="text-dim">Brak ćwiczeń w katalogu.</p>
|
||||
) : (
|
||||
<div className="flex flex-col gap-1.5">
|
||||
{exercises.map((p) => (
|
||||
<button
|
||||
key={p.id}
|
||||
type="button"
|
||||
onClick={() => onPick(p.id)}
|
||||
className="flex items-center justify-between gap-3 w-full px-3 py-2.5 rounded-[10px] text-left hover:bg-line/22 cursor-pointer transition-colors"
|
||||
>
|
||||
<span className="font-semibold">{p.name}</span>
|
||||
<span className="text-dim text-[0.8rem]">
|
||||
{MUSCLE_NAMES[p.primaryMuscle] || p.primaryMuscle}
|
||||
</span>
|
||||
<>
|
||||
<Input
|
||||
value={query}
|
||||
onChange={(e) => setQuery(e.target.value)}
|
||||
placeholder="Szukaj ćwiczenia…"
|
||||
/>
|
||||
<div className="mt-3 flex flex-wrap gap-1.5">
|
||||
<button type="button" className={chip(muscle === null)} onClick={() => setMuscle(null)}>
|
||||
Wszystkie
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
{muscles.map((m) => (
|
||||
<button
|
||||
key={m}
|
||||
type="button"
|
||||
className={chip(muscle === m)}
|
||||
onClick={() => setMuscle(muscle === m ? null : m)}
|
||||
>
|
||||
{MUSCLE_NAMES[m] || m}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
<div className="mt-3 flex max-h-[45vh] flex-col gap-1.5 overflow-y-auto">
|
||||
{filtered.length === 0 ? (
|
||||
<p className="text-dim">Brak ćwiczeń spełniających kryteria.</p>
|
||||
) : (
|
||||
muscles
|
||||
.filter((m) => muscle === null || m === muscle)
|
||||
.map((m) => {
|
||||
const items = filtered.filter((p) => p.primaryMuscle === m);
|
||||
if (items.length === 0) return null;
|
||||
return (
|
||||
<div key={m}>
|
||||
{muscle === null && (
|
||||
<p className="px-3 pt-2 pb-1 text-[0.72rem] font-bold uppercase tracking-wide text-dim">
|
||||
{MUSCLE_NAMES[m] || m}
|
||||
</p>
|
||||
)}
|
||||
{items.map((p) => (
|
||||
<button
|
||||
key={p.id}
|
||||
type="button"
|
||||
onClick={() => onPick(p.id)}
|
||||
className="flex items-center w-full px-3 py-2.5 rounded-[10px] text-left hover:bg-line/22 cursor-pointer transition-colors"
|
||||
>
|
||||
<span className="font-semibold">{p.name}</span>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
})
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</Modal>
|
||||
);
|
||||
|
||||
@@ -812,6 +812,18 @@ describe('Dodawanie ćwiczenia do sesji', () => {
|
||||
expect(within(dialog).getByText('Martwy ciąg')).toBeInTheDocument();
|
||||
expect(within(dialog).getByText('Wiosłowanie')).toBeInTheDocument();
|
||||
|
||||
// wyszukiwarka odporna na diakrytyki: „ciag" znajdzie „Martwy ciąg", ukryje „Wiosłowanie"
|
||||
const search = screen.getByPlaceholderText('Szukaj ćwiczenia…');
|
||||
await user.type(search, 'ciag');
|
||||
expect(within(dialog).getByText('Martwy ciąg')).toBeInTheDocument();
|
||||
expect(within(dialog).queryByText('Wiosłowanie')).not.toBeInTheDocument();
|
||||
|
||||
// kategoria (partia mięśniowa) po wyczyszczeniu frazy pokazuje oba ćwiczenia
|
||||
await user.clear(search);
|
||||
await user.click(within(dialog).getByRole('button', { name: 'Plecy' }));
|
||||
expect(within(dialog).getByText('Martwy ciąg')).toBeInTheDocument();
|
||||
expect(within(dialog).getByText('Wiosłowanie')).toBeInTheDocument();
|
||||
|
||||
// wybór ćwiczenia dodaje je do sesji (bez planu) i zamyka modal
|
||||
await user.click(within(dialog).getByRole('button', { name: /Martwy ciąg/ }));
|
||||
expect(await screen.findByRole('heading', { name: 'Martwy ciąg' })).toBeInTheDocument();
|
||||
|
||||
Reference in New Issue
Block a user