diff --git a/app/src/pages/trainees/Trainees.tsx b/app/src/pages/trainees/Trainees.tsx index 25fd574..12a51c6 100644 --- a/app/src/pages/trainees/Trainees.tsx +++ b/app/src/pages/trainees/Trainees.tsx @@ -1,7 +1,7 @@ import { useEffect, useMemo, useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { useQueryClient } from '@tanstack/react-query'; -import { ArrowUpDown, Check, ChevronDown, Flame, Plus, Users } from 'lucide-react'; +import { ArrowUpDown, Building2, Check, ChevronDown, Flame, Plus, Users } from 'lucide-react'; import { sendInvitation, useIsTrainer, @@ -111,6 +111,7 @@ export default function Trainees() { const [gymId, setGymId] = useState(''); const [sortKey, setSortKey] = useState('flags'); const [sortOpen, setSortOpen] = useState(false); + const [groupByGym, setGroupByGym] = useState(false); const [firstName, setFirstName] = useState(''); const [lastName, setLastName] = useState(''); const [searching, setSearching] = useState(false); @@ -160,8 +161,20 @@ export default function Trainees() { )} , + /* grupowanie wg siłowni — opcja (domyślnie lista płaska) */ + , ], - filterActive: sortKey !== 'flags', + filterActive: sortKey !== 'flags' || groupByGym, actions: isTrainer ? [{ label: 'Dodaj podopiecznego', icon: Plus, onClick: () => setAddOpen(true) }] : undefined, @@ -216,19 +229,22 @@ export default function Trainees() { // grupowanie wg siłowni, na której trener jest trenerem podopiecznego // (wspólna siłownia: rola TRAINER trenera + członkostwo podopiecznego); + // podopieczny trenowany w wielu siłowniach pojawia się w KAŻDEJ z nich, // reszta trafia do „Inne” (ostatnia grupa) const trainerGymIds = useMemo(() => new Set(trainerGyms.map((m) => m.gymId)), [trainerGyms]); const gymGroups = useMemo(() => { const byGym = new Map(); const other: Trainee[] = []; for (const t of filtered) { - const shared = t.gyms.find((g) => trainerGymIds.has(g.id)); - if (shared) { - const grp = byGym.get(shared.id) ?? { id: shared.id, name: shared.name, trainees: [] }; - grp.trainees.push(t); - byGym.set(shared.id, grp); - } else { + const shared = t.gyms.filter((g) => trainerGymIds.has(g.id)); + if (shared.length === 0) { other.push(t); + } else { + for (const g of shared) { + const grp = byGym.get(g.id) ?? { id: g.id, name: g.name, trainees: [] }; + grp.trainees.push(t); + byGym.set(g.id, grp); + } } } const groups = [...byGym.values()].sort((a, b) => a.name.localeCompare(b.name, 'pl')); @@ -357,27 +373,37 @@ export default function Trainees() { )} {!isLoading && !error && filtered.length > 0 && ( -
- {gymGroups.map((g) => ( -
-
-

{g.name}

- {g.trainees.length} -
- -
- {g.trainees.map((t) => ( - navigate(`/trainees/${t.user.id}`)} - /> - ))} + groupByGym ? ( +
+ {gymGroups.map((g) => ( +
+
+

{g.name}

+ {g.trainees.length}
- -
- ))} -
+ +
+ {g.trainees.map((t) => ( + navigate(`/trainees/${t.user.id}`)} + /> + ))} +
+
+
+ ))} +
+ ) : ( + +
+ {filtered.map((t) => ( + navigate(`/trainees/${t.user.id}`)} /> + ))} +
+
+ ) )} ); diff --git a/app/test/e2e/trainees.spec.ts b/app/test/e2e/trainees.spec.ts index 623681d..e0116c7 100644 --- a/app/test/e2e/trainees.spec.ts +++ b/app/test/e2e/trainees.spec.ts @@ -23,11 +23,13 @@ test('trener widzi listę podopiecznych z metrykami i flagami', async ({ page }) await loginToTrainees(page); await expect(page.getByText('Anna Nowak')).toBeVisible(); - // grupowanie wg siłowni — nagłówek grupy (nazwa siłowni) - await expect(page.getByRole('heading', { name: 'Żelazna Świątynia' })).toBeVisible(); await expect(page.getByText('Zerowany streak')).toBeVisible(); await expect(page.getByText('5/20 dni')).toBeVisible(); await expect(page.getByText('Plan testowy')).toBeVisible(); + + // grupowanie wg siłowni — opcja z filtra (domyślnie lista płaska) + await page.getByRole('button', { name: 'Grupuj wg siłowni' }).click(); + await expect(page.getByRole('heading', { name: 'Żelazna Świątynia' })).toBeVisible(); }); test('wyszukiwarka w menu kontekstowym filtruje listę podopiecznych', async ({ page }) => { diff --git a/app/test/trainees.test.tsx b/app/test/trainees.test.tsx index 4471eb0..b3e3aae 100644 --- a/app/test/trainees.test.tsx +++ b/app/test/trainees.test.tsx @@ -37,6 +37,15 @@ const GYM = { equipment: [], }; +const GYM_2 = { + id: 'gym-2', + name: 'Mega Gym', + location: 'Kraków', + availability: null, + capacity: null, + equipment: [], +}; + const TRAINEE = { user: { id: 'trainee-1', firstName: 'Anna', lastName: 'Nowak' }, gyms: [GYM], @@ -78,6 +87,16 @@ const TRAINEE_3 = { flags: [], }; +// trenowany w DWÓCH siłowniach trenera — przy grupowaniu widoczny w obu grupach +const TRAINEE_4 = { + user: { id: 'trainee-4', firstName: 'Tomek', lastName: 'Kowalczyk' }, + gyms: [GYM, GYM_2], + activePlan: null, + killstreak: 2, + lastSessionAt: null, + flags: [], +}; + const CANDIDATE = { id: 'candidate-1', firstName: 'Jan', @@ -113,7 +132,10 @@ function mockApi() { if (url.includes('/me')) return jsonResponse(200, { user: TRAINER, - memberships: [{ gymId: GYM.id, roles: ['TRAINER'], gym: GYM }], + memberships: [ + { gymId: GYM.id, roles: ['TRAINER'], gym: GYM }, + { gymId: GYM_2.id, roles: ['TRAINER'], gym: GYM_2 }, + ], }); if (url.includes('/trainer/invitations/inv-1/accept')) return jsonResponse(200, { coaching: { id: 'inv-1', status: 'ACTIVE' } }); @@ -260,7 +282,7 @@ function mockApi() { if (url.includes('/trainer/trainees/trainee-1')) return jsonResponse(method === 'DELETE' ? 204 : 200, method === 'DELETE' ? {} : TRAINEE_DETAIL); if (url.includes('/trainer/trainees')) - return jsonResponse(200, { trainees: [TRAINEE, TRAINEE_2, TRAINEE_3] }); + return jsonResponse(200, { trainees: [TRAINEE, TRAINEE_2, TRAINEE_3, TRAINEE_4] }); if (url.includes('/trainer/search')) return jsonResponse(200, { users: [CANDIDATE] }); if (url.includes('/dashboard')) return jsonResponse(200, { @@ -325,10 +347,9 @@ describe('Trainees — lista i szczegóły', () => { await loginToTrainees(user); expect(await screen.findByText('Anna Nowak')).toBeInTheDocument(); - // grupowanie wg siłowni — nagłówek grupy + meta wiersza podopiecznego - expect(screen.getByRole('heading', { name: 'Żelazna Świątynia' })).toBeInTheDocument(); - expect(screen.getAllByText('Żelazna Świątynia').length).toBeGreaterThanOrEqual(2); - expect(screen.getByRole('heading', { name: 'Inne' })).toBeInTheDocument(); + // domyślnie lista płaska (bez nagłówków grup); nazwa siłowni: meta wiersza + opcja selecta + expect(screen.queryByRole('heading', { name: 'Żelazna Świątynia' })).not.toBeInTheDocument(); + expect(screen.getAllByText('Żelazna Świątynia').length).toBeGreaterThanOrEqual(1); expect(screen.getByText('Zerowany streak')).toBeInTheDocument(); expect(screen.getByText('5/20 dni')).toBeInTheDocument(); expect(screen.getByText('Plan testowy')).toBeInTheDocument(); @@ -363,7 +384,7 @@ describe('Trainees — lista i szczegóły', () => { await loginToTrainees(user); await screen.findByText('Anna Nowak'); - const NAMES = ['Anna Nowak', 'Piotr Kowal', 'Zofia Wiśniewska']; + const NAMES = ['Anna Nowak', 'Piotr Kowal', 'Tomek Kowalczyk', 'Zofia Wiśniewska']; // kolejność wierszy = kolejność przycisków-wierszy zawierających imię i nazwisko const rowOrder = () => Array.from(document.querySelectorAll('button')) @@ -375,22 +396,48 @@ describe('Trainees — lista i szczegóły', () => { await user.click(await screen.findByRole('button', { name: option })); }; - // sortowanie działa W RAMACH grup siłowni: Anna (Żelazna Świątynia) jest zawsze - // przed grupą „Inne” (Piotr, Zofia — bez wspólnej siłowni) - // domyślnie: flagi (Anna 1 > Piotr/Zofia 0; remis po nazwie) - expect(rowOrder()).toEqual(['Anna Nowak', 'Piotr Kowal', 'Zofia Wiśniewska']); + // lista płaska (grupowanie wyłączone) — sortowanie globalne + // domyślnie: flagi (Anna 1 > reszta 0; remis po nazwie) + expect(rowOrder()).toEqual(['Anna Nowak', 'Piotr Kowal', 'Tomek Kowalczyk', 'Zofia Wiśniewska']); await pickSort('Killstreak'); - expect(rowOrder()).toEqual(['Anna Nowak', 'Zofia Wiśniewska', 'Piotr Kowal']); + expect(rowOrder()).toEqual(['Zofia Wiśniewska', 'Anna Nowak', 'Tomek Kowalczyk', 'Piotr Kowal']); await pickSort('Nazwa'); - expect(rowOrder()).toEqual(['Anna Nowak', 'Piotr Kowal', 'Zofia Wiśniewska']); + expect(rowOrder()).toEqual(['Anna Nowak', 'Piotr Kowal', 'Tomek Kowalczyk', 'Zofia Wiśniewska']); await pickSort('Ostatnia sesja'); - expect(rowOrder()).toEqual(['Anna Nowak', 'Zofia Wiśniewska', 'Piotr Kowal']); + expect(rowOrder()).toEqual(['Zofia Wiśniewska', 'Anna Nowak', 'Piotr Kowal', 'Tomek Kowalczyk']); await pickSort('Flagi'); - expect(rowOrder()).toEqual(['Anna Nowak', 'Piotr Kowal', 'Zofia Wiśniewska']); + expect(rowOrder()).toEqual(['Anna Nowak', 'Piotr Kowal', 'Tomek Kowalczyk', 'Zofia Wiśniewska']); + }); + + it('grupowanie wg siłowni (toggle w filtrach): podopieczny z dwóch siłowni w obu grupach', async () => { + mockApi(); + const user = userEvent.setup(); + renderApp(); + await loginToTrainees(user); + await screen.findByText('Anna Nowak'); + + // domyślnie lista płaska — brak nagłówków grup + expect(screen.queryByRole('heading', { name: 'Żelazna Świątynia' })).not.toBeInTheDocument(); + + 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(); + 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); + + // wyłączenie wraca do listy płaskiej + await user.click(screen.getByRole('button', { name: 'Grupuj wg siłowni' })); + expect(screen.queryByRole('heading', { name: 'Mega Gym' })).not.toBeInTheDocument(); + expect(screen.getAllByText('Tomek Kowalczyk')).toHaveLength(1); }); it('trener może wyszukać kandydata i wysłać zaproszenie', async () => {