diff --git a/app/src/components/gyms/JoinGymModal.tsx b/app/src/components/gyms/JoinGymModal.tsx new file mode 100644 index 0000000..3ee0a61 --- /dev/null +++ b/app/src/components/gyms/JoinGymModal.tsx @@ -0,0 +1,88 @@ +import { useState } from 'react'; +import { useQueryClient } from '@tanstack/react-query'; +import { joinGym, useAllGyms, useGyms } from '../../api/queries'; +import type { Gym } from '../../api/types'; +import { Button, ErrorText, Input, Loading, Modal } from '../ui'; +import { useToast } from '../toast'; + +/** + * Modal „Dołącz do siłowni” — wyszukiwarka + lista REAL siłowni, do których + * zalogowany nie należy (z /gyms/all), z „Dołącz” per siłownia. + */ +export default function JoinGymModal({ open, onClose }: { open: boolean; onClose: () => void }) { + const queryClient = useQueryClient(); + const { push } = useToast(); + const [query, setQuery] = useState(''); + const [busyId, setBusyId] = useState(null); + + const { data: allData, isLoading, error } = useAllGyms(); + const { data: gymsData } = useGyms(); + const memberIds = new Set((gymsData?.gyms ?? []).map((g) => g.id)); + const joinable = (allData?.gyms ?? []).filter((g) => !memberIds.has(g.id)); + + const q = query.trim().toLowerCase(); + const filtered = joinable.filter( + (g) => !q || [g.name, g.location].filter(Boolean).some((s) => s!.toLowerCase().includes(q)), + ); + + const join = async (g: Gym) => { + setBusyId(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 { + setBusyId(null); + } + }; + + return ( + + setQuery(e.target.value)} + /> + +
+ {isLoading && } + {error && Błąd: {(error as Error).message}} + {!isLoading && !error && filtered.length === 0 && ( +

+ {q + ? 'Brak wyników dla podanego zapytania.' + : 'Brak siłowni do dołączenia — należysz już do wszystkich.'} +

+ )} + {filtered.map((g) => ( +
+
+ {g.name} +
+ {g.location || '—'} · {g.equipment.length} urządzeń +
+
+ +
+ ))} +
+
+ ); +} diff --git a/app/src/pages/gyms/Gyms.tsx b/app/src/pages/gyms/Gyms.tsx index fa197a1..ffca4a4 100644 --- a/app/src/pages/gyms/Gyms.tsx +++ b/app/src/pages/gyms/Gyms.tsx @@ -1,12 +1,20 @@ import { useEffect, useState, type ReactNode } from 'react'; import { Link, useNavigate } from 'react-router-dom'; import { useQueryClient } from '@tanstack/react-query'; -import { Building2, Dumbbell, MapPin, Plus, ThumbsDown, ThumbsUp, Users } from 'lucide-react'; +import { + Building2, + Dumbbell, + MapPin, + Plus, + ThumbsDown, + ThumbsUp, + UserPlus, + Users, +} from 'lucide-react'; import { gymPhotoSrc, joinGym, rateGym, - useAllGyms, useGyms, useGymInvitations, useMe, @@ -36,8 +44,9 @@ import { DAY_NAMES, formatDayRange, normalizeAvailability } from '../../lib/avai import GymCreateModal from '../../components/gyms/GymCreateModal'; import ClaimModal from '../../components/gyms/ClaimModal'; import InviteGymModal from '../../components/gyms/InviteGymModal'; +import JoinGymModal from '../../components/gyms/JoinGymModal'; -type Tab = 'mine' | 'join' | 'virtual'; +type Tab = 'mine' | 'virtual'; function GymKindBadges({ gym }: { gym: { kind: string; isPrivate: boolean } }) { return ( @@ -447,45 +456,24 @@ 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 [joinOpen, setJoinOpen] = 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(() => { @@ -497,7 +485,10 @@ export default function Gyms() { useTopbar({ title: 'Siłownie', search: 'gyms', - actions: [{ label: 'Stwórz siłownię', onClick: () => setCreateOpen(true), icon: Plus }], + actions: [ + { label: 'Stwórz siłownię', onClick: () => setCreateOpen(true), icon: Plus }, + { label: 'Dołącz', onClick: () => setJoinOpen(true), icon: UserPlus }, + ], }); const query = useListSearch((s) => s.queries.gyms); @@ -509,7 +500,6 @@ 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 ( @@ -518,7 +508,6 @@ export default function Gyms() { )} - {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) - - - - ))} -
- - )} - - )} - {tab === 'virtual' && ( <> {virtualLoading && } @@ -655,6 +605,7 @@ export default function Gyms() { )} setCreateOpen(false)} /> + setJoinOpen(false)} /> {inviteGym && setInviteGym(null)} gym={inviteGym} />} ); diff --git a/app/test/e2e/gyms.spec.ts b/app/test/e2e/gyms.spec.ts index c150e11..19ce0e0 100644 --- a/app/test/e2e/gyms.spec.ts +++ b/app/test/e2e/gyms.spec.ts @@ -70,7 +70,7 @@ 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 ({ +test('/gyms: modal Dołącz — wyszukiwanie i dołączanie do siłowni, do której nie należę', async ({ page, }) => { mockApi(page); @@ -83,26 +83,25 @@ test('/gyms: zakładka Dołącz — wyszukiwanie i dołączanie do siłowni, do 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); + // akcja „Dołącz” w menu kontekstowym otwiera modal z wyszukiwarką + await page.getByRole('button', { name: 'Dołącz' }).click(); + const dialog = page.getByRole('dialog'); + await expect(dialog).toBeVisible(); + // REAL siłownie, do których nie należę + await expect(dialog.getByText('Mega Gym')).toBeVisible(); + // mojej siłowni (właściciel) w liście nie ma + await expect(dialog.getByText('Żelazna Świątynia')).toHaveCount(0); - // wyszukiwarka w topbarze filtruje listę - const searchbox = page.getByRole('searchbox', { name: 'Szukaj siłowni' }); + // wyszukiwarka w modalu filtruje listę + const searchbox = dialog.getByRole('searchbox', { name: 'Szukaj siłowni do dołączenia' }); await searchbox.fill('mega'); - await expect(page.getByText('Mega Gym')).toBeVisible(); + await expect(dialog.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 expect(dialog.getByText('Brak wyników dla podanego zapytania.')).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(); + // dołączenie z modala + await dialog.getByRole('button', { name: 'Dołącz', exact: true }).click(); await expect(page.getByText('Dołączono do siłowni: Mega Gym')).toBeVisible(); });