Podopieczni: przy grupowaniu lista silowni (z podopiecznymi) jako zakladki, widoczna aktywna grupa

This commit is contained in:
tisachi
2026-09-11 16:59:56 +02:00
parent 5aaa1d539a
commit d6cee6dcb8
2 changed files with 54 additions and 29 deletions

View File

@@ -25,6 +25,7 @@ import {
ProgressBar,
Select,
Skeleton,
Tabs,
} from '../../components/ui';
import { Avatar } from '../../components/Avatar';
import { useToast } from '../../components/toast';
@@ -113,6 +114,7 @@ export default function Trainees() {
const [sortKey, setSortKey] = useState<SortKey>('none');
const [sortOpen, setSortOpen] = useState(false);
const [groupByGym, setGroupByGym] = useState(false);
const [activeGymTab, setActiveGymTab] = useState<string | null>(null);
const [firstName, setFirstName] = useState('');
const [lastName, setLastName] = useState('');
const [searching, setSearching] = useState(false);
@@ -256,6 +258,8 @@ export default function Trainees() {
if (other.length > 0) groups.push({ id: 'other', name: 'Inne', trainees: other });
return groups;
}, [filtered, trainerGymIds]);
// aktywna zakładka siłowni (domyślnie pierwsza grupa; fallback gdy grupa zniknie np. po wyszukiwarce)
const activeGroup = gymGroups.find((g) => g.id === activeGymTab) ?? gymGroups[0];
const invite = async (candidate: UserSearchResult) => {
try {
@@ -378,28 +382,36 @@ export default function Trainees() {
</Card>
)}
{!isLoading && !error && filtered.length > 0 && (
groupByGym ? (
<div className="space-y-5">
{gymGroups.map((g) => (
<section key={g.id}>
<div className="mb-3 flex items-center gap-2.5">
<H3 className="mb-0">{g.name}</H3>
<Badge color="gray">{g.trainees.length}</Badge>
groupByGym && activeGroup ? (
<>
{/* lista siłowni (tylko te z podopiecznymi) jako zakładki — łatwiej się odnaleźć */}
{gymGroups.length > 1 && (
<div className="mb-4">
<Tabs
tabs={gymGroups.map((g) => ({ id: g.id, label: g.name }))}
active={activeGroup.id}
onChange={setActiveGymTab}
/>
</div>
)}
<section>
<div className="mb-3 flex items-center gap-2.5">
<H3 className="mb-0">{activeGroup.name}</H3>
<Badge color="gray">{activeGroup.trainees.length}</Badge>
</div>
<Card>
<div className="divide-y divide-line">
{activeGroup.trainees.map((t) => (
<TraineeCard
key={t.user.id}
trainee={t}
onOpen={() => navigate(`/trainees/${t.user.id}`)}
/>
))}
</div>
<Card>
<div className="divide-y divide-line">
{g.trainees.map((t) => (
<TraineeCard
key={t.user.id}
trainee={t}
onOpen={() => navigate(`/trainees/${t.user.id}`)}
/>
))}
</div>
</Card>
</section>
))}
</div>
</Card>
</section>
</>
) : (
<Card>
<div className="divide-y divide-line">

View File

@@ -429,18 +429,31 @@ describe('Trainees — lista i szczegóły', () => {
await user.click(screen.getByRole('button', { name: 'Grupuj wg siłowni' }));
// grupy: Żelazna Świątynia (Anna, Tomek), Mega Gym (Tomek), Inne (Piotr, Zofia)
expect(screen.getByRole('heading', { name: 'Żelazna Świątynia' })).toBeInTheDocument();
// zakładki = lista siłowni z podopiecznymi (siłownie alfabetycznie, „Inne” na końcu)
expect(screen.getByRole('button', { name: 'Inne' })).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Mega Gym' })).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Żelazna Świątynia' })).toBeInTheDocument();
// domyślnie pierwsza zakładka (Mega Gym) — tylko jej wiersze
expect(screen.getByRole('heading', { name: 'Mega Gym' })).toBeInTheDocument();
expect(screen.getByRole('heading', { name: 'Inne' })).toBeInTheDocument();
// Tomek trenowany w obu siłowniach — w obu grupach; reszta po jednej
expect(screen.getAllByText('Tomek Kowalczyk')).toHaveLength(2);
expect(screen.getAllByText('Anna Nowak')).toHaveLength(1);
expect(screen.getAllByText('Piotr Kowal')).toHaveLength(1);
expect(screen.getByText('Tomek Kowalczyk')).toBeInTheDocument();
expect(screen.queryByText('Anna Nowak')).not.toBeInTheDocument();
// wyłączenie wraca do listy płaskiej
// przełączenie na siłownię — Anna + Tomek (Tomek trenowany w obu siłowniach)
await user.click(screen.getByRole('button', { name: 'Żelazna Świątynia' }));
expect(screen.getByRole('heading', { name: 'Żelazna Świątynia' })).toBeInTheDocument();
expect(screen.getByText('Anna Nowak')).toBeInTheDocument();
expect(screen.getAllByText('Tomek Kowalczyk')).toHaveLength(1);
// „Inne” — Piotr i Zofia
await user.click(screen.getByRole('button', { name: 'Inne' }));
expect(screen.getByRole('heading', { name: 'Inne' })).toBeInTheDocument();
expect(screen.getByText('Piotr Kowal')).toBeInTheDocument();
expect(screen.queryByText('Anna Nowak')).not.toBeInTheDocument();
// wyłączenie wraca do listy płaskiej (wszyscy, Tomek raz)
await user.click(screen.getByRole('button', { name: 'Grupuj wg siłowni' }));
expect(screen.queryByRole('heading', { name: 'Mega Gym' })).not.toBeInTheDocument();
expect(screen.getByText('Anna Nowak')).toBeInTheDocument();
expect(screen.getAllByText('Tomek Kowalczyk')).toHaveLength(1);
});