Silownie: /gyms - Dołącz przeniesione z zakładek do menu kontekstowego + modal z wyszukiwarką siłowni, do których nie należę
This commit is contained in:
88
app/src/components/gyms/JoinGymModal.tsx
Normal file
88
app/src/components/gyms/JoinGymModal.tsx
Normal file
@@ -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<string | null>(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 (
|
||||
<Modal open={open} onClose={onClose} title="Dołącz do siłowni" handle>
|
||||
<Input
|
||||
type="search"
|
||||
placeholder="Szukaj siłowni…"
|
||||
aria-label="Szukaj siłowni do dołączenia"
|
||||
value={query}
|
||||
onChange={(e) => setQuery(e.target.value)}
|
||||
/>
|
||||
|
||||
<div className="mt-3 max-h-[50vh] space-y-2 overflow-y-auto pr-1">
|
||||
{isLoading && <Loading />}
|
||||
{error && <ErrorText>Błąd: {(error as Error).message}</ErrorText>}
|
||||
{!isLoading && !error && filtered.length === 0 && (
|
||||
<p className="py-6 text-center text-dim text-[0.9rem]">
|
||||
{q
|
||||
? 'Brak wyników dla podanego zapytania.'
|
||||
: 'Brak siłowni do dołączenia — należysz już do wszystkich.'}
|
||||
</p>
|
||||
)}
|
||||
{filtered.map((g) => (
|
||||
<div
|
||||
key={g.id}
|
||||
className="flex items-center justify-between gap-3 bg-input border border-line rounded-xl px-3 py-2"
|
||||
>
|
||||
<div className="min-w-0">
|
||||
<strong className="text-[0.95rem]">{g.name}</strong>
|
||||
<div className="text-dim text-[0.8rem]">
|
||||
{g.location || '—'} · {g.equipment.length} urządzeń
|
||||
</div>
|
||||
</div>
|
||||
<Button
|
||||
size="sm"
|
||||
className="shrink-0"
|
||||
disabled={busyId === g.id}
|
||||
onClick={() => void join(g)}
|
||||
>
|
||||
{busyId === g.id ? 'Dołączanie…' : 'Dołącz'}
|
||||
</Button>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
@@ -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<string | null>(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<Tab | null>(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() {
|
||||
<Tabs
|
||||
tabs={[
|
||||
{ id: 'mine', label: 'Moje siłownie' },
|
||||
{ id: 'join', label: 'Dołącz' },
|
||||
{ id: 'virtual', label: 'Wirtualne' },
|
||||
]}
|
||||
active={tab ?? 'virtual'}
|
||||
@@ -574,45 +563,6 @@ export default function Gyms() {
|
||||
</>
|
||||
)}
|
||||
|
||||
{tab === 'join' && (
|
||||
<>
|
||||
{allLoading && <Loading />}
|
||||
{allError && <ErrorText>Błąd: {(allError as Error).message}</ErrorText>}
|
||||
{!allLoading && !allError && filteredJoinable.length === 0 && (
|
||||
<Card>
|
||||
<EmptyState
|
||||
icon={Building2}
|
||||
title={q ? 'Brak wyników' : 'Brak siłowni do dołączenia'}
|
||||
description={
|
||||
q
|
||||
? 'Brak wyników dla podanego zapytania.'
|
||||
: 'Należysz już do wszystkich dostępnych siłowni.'
|
||||
}
|
||||
/>
|
||||
</Card>
|
||||
)}
|
||||
{filteredJoinable.length > 0 && (
|
||||
<>
|
||||
<div className="mb-3 flex items-center gap-2.5">
|
||||
<H3 className="mb-0">Dołącz do siłowni</H3>
|
||||
<Badge color="gray">{filteredJoinable.length}</Badge>
|
||||
</div>
|
||||
<div className="grid grid-cols-1 gap-4 lg:grid-cols-2">
|
||||
{filteredJoinable.map((g) => (
|
||||
// bez onClick na karcie — tap w „Dołącz” nie może jednocześnie otworzyć detalu
|
||||
// (nazwa pozostaje linkiem do /gyms/:id)
|
||||
<GymCard key={g.id} gym={g} cover={g.coverPhoto}>
|
||||
<Button size="sm" onClick={() => void join(g)} disabled={joinBusyId === g.id}>
|
||||
Dołącz
|
||||
</Button>
|
||||
</GymCard>
|
||||
))}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
|
||||
{tab === 'virtual' && (
|
||||
<>
|
||||
{virtualLoading && <Loading />}
|
||||
@@ -655,6 +605,7 @@ export default function Gyms() {
|
||||
)}
|
||||
|
||||
<GymCreateModal open={createOpen} onClose={() => setCreateOpen(false)} />
|
||||
<JoinGymModal open={joinOpen} onClose={() => setJoinOpen(false)} />
|
||||
{inviteGym && <InviteGymModal open onClose={() => setInviteGym(null)} gym={inviteGym} />}
|
||||
</>
|
||||
);
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user