From f5deff869264013619da04b2704cc56ac77bb676 Mon Sep 17 00:00:00 2001 From: tisachi Date: Sun, 13 Sep 2026 18:00:26 +0200 Subject: [PATCH] =?UTF-8?q?Silownie:=20/gyms=20-=20zakladka=20Do=C5=82?= =?UTF-8?q?=C4=85cz=20z=20wyszukiwaniem=20i=20do=C5=82=C4=85czaniem=20do?= =?UTF-8?q?=20si=C5=82owni,=20do=20kt=C3=B3rych=20nie=20nale=C5=BC=C4=99?= =?UTF-8?q?=20(lista=20REAL=20z=20/gyms/all,=20filtr=20w=20topbarze)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/pages/gyms/Gyms.tsx | 155 ++++++++++++++++++++++++++---- app/test/e2e/gyms.spec.ts | 40 +++++++- app/test/e2e/virtual-gyms.spec.ts | 6 +- 3 files changed, 181 insertions(+), 20 deletions(-) diff --git a/app/src/pages/gyms/Gyms.tsx b/app/src/pages/gyms/Gyms.tsx index cd7928c..fa197a1 100644 --- a/app/src/pages/gyms/Gyms.tsx +++ b/app/src/pages/gyms/Gyms.tsx @@ -6,6 +6,7 @@ import { gymPhotoSrc, joinGym, rateGym, + useAllGyms, useGyms, useGymInvitations, useMe, @@ -16,7 +17,18 @@ import { withdrawGymInvitation, } from '../../api/queries'; import type { Gym, GymClaim, GymInvitation, VirtualGym } from '../../api/types'; -import { Badge, Button, Card, EmptyState, ErrorText, H3, ListRow, Loading, MetaItem, Tabs } from '../../components/ui'; +import { + Badge, + Button, + Card, + EmptyState, + ErrorText, + H3, + ListRow, + Loading, + MetaItem, + Tabs, +} from '../../components/ui'; import { useToast } from '../../components/toast'; import { useListSearch } from '../../store/list-search'; import { useTopbar } from '../../topbar'; @@ -25,7 +37,7 @@ import GymCreateModal from '../../components/gyms/GymCreateModal'; import ClaimModal from '../../components/gyms/ClaimModal'; import InviteGymModal from '../../components/gyms/InviteGymModal'; -type Tab = 'mine' | 'virtual'; +type Tab = 'mine' | 'join' | 'virtual'; function GymKindBadges({ gym }: { gym: { kind: string; isPrivate: boolean } }) { return ( @@ -62,15 +74,22 @@ function GymCard({ return (
{/* okładka: zdjęcie albo gradientowy placeholder z piktogramem */}
{cover ? ( - {gym.name} + {gym.name} ) : ( <>
{gym.location || '—'} - {memberCount != null ? `${memberCount} członków` : gym.capacity != null ? `${gym.capacity} miejsc` : '—'} + {memberCount != null + ? `${memberCount} członków` + : gym.capacity != null + ? `${gym.capacity} miejsc` + : '—'} {gym.equipment.length} urządzeń
@@ -154,7 +177,9 @@ function GymCard({ style={{ width: `${rating}%` }} />
- {rating}% + + {rating}% +
)} {children &&
{children}
} @@ -166,7 +191,10 @@ function GymCard({ /* Głosy „Polecam/Nie polecam” z licznikami (aria-pressed). */ function VoteButtons({ gym, onRate }: { gym: VirtualGym; onRate: (vote: 'UP' | 'DOWN') => void }) { return ( -
+
)} {isOwner && !gym.isPrivate && gym.claimStatus === 'REJECTED' && ( <> - Odrzucono{gym.rejectReason ? `: ${gym.rejectReason}` : ''} + + Odrzucono{gym.rejectReason ? `: ${gym.rejectReason}` : ''} + @@ -319,22 +354,36 @@ function VirtualGymCard({ )} {gym.isPrivate && isOwner && ( - setClaimOpen(true)} onWithdraw={() => void withdrawClaim()} busy={busy} /> + setClaimOpen(true)} + onWithdraw={() => void withdrawClaim()} + busy={busy} + /> )}
{gym.isPrivate && isOwner && sentInvitations.length > 0 && (
-
Wysłane zaproszenia
+
+ Wysłane zaproszenia +
{sentInvitations.map((inv) => { - const name = [inv.user?.firstName, inv.user?.lastName].filter(Boolean).join(' ') || inv.user?.email || '—'; + const name = + [inv.user?.firstName, inv.user?.lastName].filter(Boolean).join(' ') || + inv.user?.email || + '—'; return ( void withdrawInvitation(inv.id)}> + } @@ -398,23 +447,45 @@ function ClaimStatusInline({ export default function Gyms() { const navigate = useNavigate(); + const queryClient = useQueryClient(); const { data: ownedData, isLoading: ownedLoading, error: ownedError } = useOwnedGyms(); const { data: gymsData } = useGyms(); + const { data: allData, isLoading: allLoading, error: allError } = useAllGyms(); const { data: virtualData, isLoading: virtualLoading, error: virtualError } = useVirtualGyms(); const { data: invData } = useGymInvitations(); const { data: claimsData } = useMyGymClaims(); const { data: me } = useMe(); const isAdmin = me?.user.isAdmin ?? false; + const { push } = useToast(); const ownedGyms = ownedData?.gyms ?? []; const memberIds = new Set((gymsData?.gyms ?? []).map((g) => g.id)); const ownerIds = new Set(ownedGyms.map((g) => g.id)); + // REAL siłownie, do których nie należę (zakładka „Dołącz”) + const joinableGyms = (allData?.gyms ?? []).filter((g) => !memberIds.has(g.id)); const virtualGyms = virtualData?.gyms ?? []; const sentGymInvitations = (invData?.invitations ?? []).filter((i) => i.direction === 'sent'); const claimsByGym = new Map((claimsData?.claims ?? []).map((c) => [c.gymId, c])); const [createOpen, setCreateOpen] = useState(false); const [inviteGym, setInviteGym] = useState<{ id: string; name: string } | null>(null); + const [joinBusyId, setJoinBusyId] = useState(null); + + const join = async (g: Gym) => { + setJoinBusyId(g.id); + try { + await joinGym(g.id); + await Promise.all([ + queryClient.invalidateQueries({ queryKey: ['gyms'] }), + queryClient.invalidateQueries({ queryKey: ['gyms', 'all'] }), + ]); + push(`Dołączono do siłowni: ${g.name}`, 'success'); + } catch (e) { + push((e as Error).message, 'error'); + } finally { + setJoinBusyId(null); + } + }; const [tab, setTab] = useState(null); useEffect(() => { @@ -438,6 +509,7 @@ export default function Gyms() { !q || [g.name, g.location].filter(Boolean).some((s) => s!.toLowerCase().includes(q)); const filteredMine = ownedGyms.filter(matches); + const filteredJoinable = joinableGyms.filter(matches); const filteredVirtual = virtualGyms.filter(matches); return ( @@ -446,6 +518,7 @@ export default function Gyms() {
{filteredMine.map((g) => ( - navigate(`/gyms/${g.id}`)} /> + navigate(`/gyms/${g.id}`)} + /> + ))} +
+ + )} + + )} + + {tab === 'join' && ( + <> + {allLoading && } + {allError && Błąd: {(allError as Error).message}} + {!allLoading && !allError && filteredJoinable.length === 0 && ( + + + + )} + {filteredJoinable.length > 0 && ( + <> +
+

Dołącz do siłowni

+ {filteredJoinable.length} +
+
+ {filteredJoinable.map((g) => ( + // bez onClick na karcie — tap w „Dołącz” nie może jednocześnie otworzyć detalu + // (nazwa pozostaje linkiem do /gyms/:id) + + + ))}
@@ -505,7 +622,11 @@ export default function Gyms() { )} @@ -534,9 +655,7 @@ export default function Gyms() { )} setCreateOpen(false)} /> - {inviteGym && ( - setInviteGym(null)} gym={inviteGym} /> - )} + {inviteGym && setInviteGym(null)} gym={inviteGym} />} ); } diff --git a/app/test/e2e/gyms.spec.ts b/app/test/e2e/gyms.spec.ts index a090690..c150e11 100644 --- a/app/test/e2e/gyms.spec.ts +++ b/app/test/e2e/gyms.spec.ts @@ -16,7 +16,9 @@ async function loginToProfile(page: import('@playwright/test').Page) { await expect(page.getByRole('heading', { name: 'Profil — siłownie', exact: true })).toBeVisible(); } -test('profil pokazuje siłownie użytkownika z rolą i sprzętem oraz listę do dołączenia', async ({ page }) => { +test('profil pokazuje siłownie użytkownika z rolą i sprzętem oraz listę do dołączenia', async ({ + page, +}) => { mockApi(page); await loginToProfile(page); @@ -68,6 +70,42 @@ test('admin może stworzyć siłownię i dodać do niej sprzęt', async ({ page await expect(page.getByText('Sprzęt dodany')).toBeVisible(); }); +test('/gyms: zakładka Dołącz — wyszukiwanie i dołączanie do siłowni, do której nie należę', async ({ + page, +}) => { + mockApi(page); + await page.goto('/login'); + await page.locator('input[type="email"]').fill('test@fitos.pl'); + await page.locator('input[type="password"]').fill('haslo123'); + await page.getByRole('button', { name: 'Zaloguj' }).click(); + await expect(page.getByRole('heading', { name: 'Dashboard' })).toBeVisible(); + + await page.getByRole('link', { name: 'Siłownie' }).click(); + await expect(page.getByRole('heading', { name: 'Siłownie', exact: true })).toBeVisible(); + + // zakładka „Dołącz”: REAL siłownie, do których nie należę (zakładka ≠ przycisk karty) + await page.locator('[data-slot="tabs"]').getByRole('button', { name: 'Dołącz' }).click(); + await expect(page.getByText('Mega Gym')).toBeVisible(); + // moją siłownię (właściciel) w liście nie ma + await expect(page.getByText('Żelazna Świątynia')).toHaveCount(0); + + // wyszukiwarka w topbarze filtruje listę + const searchbox = page.getByRole('searchbox', { name: 'Szukaj siłowni' }); + await searchbox.fill('mega'); + await expect(page.getByText('Mega Gym')).toBeVisible(); + await searchbox.fill('nieistniejąca'); + // exact: opis „Brak wyników dla podanego zapytania” zawiera ten sam podłańcuch + await expect(page.getByRole('heading', { name: 'Brak wyników' })).toBeVisible(); + await searchbox.fill(''); + + // dołączenie (przycisk w karcie, nie zakładka) + await page + .locator('[data-slot="gym-card"]', { has: page.getByRole('link', { name: 'Mega Gym' }) }) + .getByRole('button', { name: 'Dołącz', exact: true }) + .click(); + await expect(page.getByText('Dołączono do siłowni: Mega Gym')).toBeVisible(); +}); + test('właściciel ma menu „Siłownie" i zarządza swoją siłownią', async ({ page }) => { mockApi(page); await page.goto('/login'); diff --git a/app/test/e2e/virtual-gyms.spec.ts b/app/test/e2e/virtual-gyms.spec.ts index 8f267d0..c0db3e4 100644 --- a/app/test/e2e/virtual-gyms.spec.ts +++ b/app/test/e2e/virtual-gyms.spec.ts @@ -62,7 +62,11 @@ test('wirtualne siłownie: dołączenie do publicznej', async ({ page }) => { await loginToGyms(page); await openVirtualTab(page); - await page.getByRole('button', { name: 'Dołącz' }).click(); + // przycisk w karcie (zakładka „Dołącz” ma tę samą nazwę) + await page + .locator('[data-slot="gym-card"]', { has: page.getByRole('link', { name: 'Wirtualna Alfa' }) }) + .getByRole('button', { name: 'Dołącz', exact: true }) + .click(); await expect(page.getByText('Dołączono do siłowni: Wirtualna Alfa')).toBeVisible(); });