Admin: detal siłowni przebudowany na wzor aplikacji (hero z okladka i chipami, dwie kolumny, godziny + wyjatek)

This commit is contained in:
Tilumca
2026-10-14 04:23:23 +02:00
parent 7c111bbf35
commit 78fa6adfea
3 changed files with 259 additions and 162 deletions

View File

@@ -1,7 +1,7 @@
import { useState } from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { ImagePlus, Star, X } from 'lucide-react';
import { Dumbbell, ImagePlus, MapPin, Star, Users, X } from 'lucide-react';
import { useTopbar } from '../../topbar';
import { api } from '../../api/client';
import { AuthImage } from '../../components/AuthImage';
@@ -9,28 +9,49 @@ import type { Equipment, Gym, GymException, GymPhoto } from '../../api/types';
import { formatEquipmentSpec, formatGymEquipment } from '../../lib/equipment';
import {
BackButton,
Badge,
Button,
Card,
Chip,
ErrorText,
Field,
Input,
ListRow,
Loading,
PageHeader,
SectionHeader,
Select,
SuccessText,
} from '../../components/ui';
import { AvailabilityEditor } from '../../components/AvailabilityEditor';
import { formatAvailability, normalizeAvailability, TIME_SLOTS } from '../../lib/availability';
import { normalizeAvailability, TIME_SLOTS } from '../../lib/availability';
function InfoItem({ label, children }: { label: string; children: React.ReactNode }) {
/* Lista godzin otwarcia: wiersz na dzień (czytelna w wąskiej kolumnie), „dziś” wyróżnione. */
function HoursList({ gym }: { gym: Gym }) {
const availability = normalizeAvailability(gym.availability);
const todayIdx = (new Date().getDay() + 6) % 7;
const DOWS = ['Poniedziałek', 'Wtorek', 'Środa', 'Czwartek', 'Piątek', 'Sobota', 'Niedziela'];
const fmt = (r: { start: string; end: string } | null) =>
r ? `${r.start}–${r.end}` : 'zamknięte';
return (
<div>
<div className="text-[11px] font-bold uppercase tracking-[0.1em] text-dim">{label}</div>
<div className="mt-0.5">{children}</div>
<div className="flex flex-col gap-1">
{availability.map((day, i) => (
<div
key={i}
className={`flex items-center justify-between gap-3 rounded-[10px] px-3 py-1.5 text-[0.85rem] ${
i === todayIdx ? 'bg-accent/10 outline outline-1 outline-accent/40' : ''
}`}
>
<span className={`flex items-center gap-2 ${day ? 'font-semibold' : 'text-dim/70'}`}>
{DOWS[i]}
{i === todayIdx && (
<span className="rounded-full bg-accent px-2 py-0.5 text-[0.62rem] font-extrabold uppercase tracking-wide text-accent-ink">
dziś
</span>
)}
</span>
<span className={`tabular-nums ${day ? 'font-semibold' : 'text-dim/60'}`}>
{fmt(day)}
</span>
</div>
))}
</div>
);
}
@@ -165,77 +186,119 @@ export default function GymDetail() {
return (
<>
<PageHeader
title={gym.name}
badge={
<>
<Badge color={gym.kind === 'VIRTUAL' ? 'blue' : 'gray'}>
{gym.kind === 'VIRTUAL' ? 'Wirtualna' : 'Realna'}
</Badge>
{gym.isPrivate && <Badge color="gray">Prywatna</Badge>}
<Badge color="gray">{gym._count?.memberships ?? 0} członk.</Badge>
</>
}
actions={<BackButton to="/gyms" />}
/>
{gym.coverPhoto && (
<div className="relative mb-4 overflow-hidden rounded-[10px] border border-line shadow-card">
{/* HERO: okładka (zdjęcie lub placeholder) + nazwa + chipy metadanych */}
<div className="relative mb-5 flex min-h-[190px] items-end overflow-hidden rounded-2xl border border-line shadow-card">
{gym.coverPhoto ? (
<AuthImage
src={gym.coverPhoto}
alt={gym.name}
className="h-44 w-full object-cover sm:h-56"
className="absolute inset-0 h-full w-full object-cover"
/>
<div
aria-hidden="true"
className="absolute inset-0 bg-[linear-gradient(180deg,transparent_45%,rgb(0_0_0/0.55))]"
/>
<div className="absolute bottom-3 left-4 right-4 flex flex-wrap items-center gap-2">
<span className="font-display text-[1.25rem] font-bold tracking-[-0.01em] text-white drop-shadow">
{gym.name}
</span>
<span className="rounded-full bg-white/15 px-2.5 py-0.5 text-[0.7rem] font-bold text-white backdrop-blur-sm">
{gym._count?.memberships ?? 0} członków
</span>
</div>
</div>
)}
{gym.kind === 'REAL' && (
<Card>
<GallerySection gym={gym} onChanged={() => {}} />
</Card>
)}
<Card>
<SectionHeader title="Dane" />
<div className="grid grid-cols-1 gap-x-4 gap-y-4 sm:grid-cols-2">
<InfoItem label="Nazwa">{gym.name}</InfoItem>
<InfoItem label="Lokalizacja">{gym.location || '—'}</InfoItem>
<InfoItem label="Dostępność">{formatAvailability(gym.availability) || '—'}</InfoItem>
<InfoItem label="Ilość miejsc">{gym.capacity ?? '—'}</InfoItem>
</div>
</Card>
<Card>
<SectionHeader title="Edycja" />
<GymEditForm
key={gym.id}
gym={gym}
onSaved={() => {
void queryClient.invalidateQueries({ queryKey: ['gyms'] });
void queryClient.invalidateQueries({ queryKey: ['gym', gym.id] });
}}
) : (
<>
<div
aria-hidden="true"
className="absolute inset-0"
style={{
background:
'linear-gradient(140deg, color-mix(in srgb, var(--accent) 26%, var(--bg-input)), color-mix(in srgb, var(--line) 60%, var(--bg-input)) 55%, var(--bg-input))',
}}
/>
<div
aria-hidden="true"
className="absolute inset-0 opacity-60"
style={{
background:
'repeating-linear-gradient(45deg, color-mix(in srgb, var(--text) 4%, transparent) 0 2px, transparent 2px 14px)',
}}
/>
<Dumbbell className="absolute left-1/2 top-1/2 h-10 w-10 -translate-x-1/2 -translate-y-1/2 text-ink/20" />
</>
)}
<div
aria-hidden="true"
className="absolute inset-0 bg-[linear-gradient(180deg,transparent_30%,rgb(0_0_0/0.6))]"
/>
</Card>
<div className="relative z-10 flex w-full flex-wrap items-end justify-between gap-3 p-5">
<div>
<h1 className="font-display text-[1.5rem] font-bold tracking-[-0.02em] text-white drop-shadow">
{gym.name}
</h1>
<div className="mt-2 flex flex-wrap gap-2">
<span className="inline-flex items-center gap-1.5 rounded-full bg-white/14 px-3 py-1 text-[0.78rem] font-semibold text-white backdrop-blur-sm">
<MapPin className="h-3 w-3" />
{gym.location || '—'}
</span>
<span className="inline-flex items-center gap-1.5 rounded-full bg-white/14 px-3 py-1 text-[0.78rem] font-semibold text-white backdrop-blur-sm">
<Users className="h-3 w-3" />
{gym._count?.memberships ?? 0} członków
</span>
<span className="inline-flex items-center gap-1.5 rounded-full bg-white/14 px-3 py-1 text-[0.78rem] font-semibold text-white backdrop-blur-sm">
<Dumbbell className="h-3 w-3" />
{gym.equipment.length} urządzeń
</span>
{gym.kind === 'VIRTUAL' && (
<span className="rounded-full bg-white/14 px-3 py-1 text-[0.78rem] font-semibold text-white backdrop-blur-sm">
Wirtualna
</span>
)}
{gym.isPrivate && (
<span className="rounded-full bg-white/14 px-3 py-1 text-[0.78rem] font-semibold text-white backdrop-blur-sm">
Prywatna
</span>
)}
</div>
</div>
<BackButton to="/gyms" />
</div>
</div>
<Card>
<EquipmentSection key={gym.id} gym={gym} />
</Card>
{/* DWIE KOLUMNY */}
<div className="grid grid-cols-1 items-start gap-4 lg:grid-cols-[1.5fr_1fr]">
<div className="flex min-w-0 flex-col gap-4">
{gym.kind === 'REAL' && (
<Card>
<GallerySection gym={gym} onChanged={() => {}} />
</Card>
)}
<Card>
<EquipmentSection key={gym.id} gym={gym} />
</Card>
<Card>
<SectionHeader title="Edycja" />
<GymEditForm
key={gym.id}
gym={gym}
onSaved={() => {
void queryClient.invalidateQueries({ queryKey: ['gyms'] });
void queryClient.invalidateQueries({ queryKey: ['gym', gym.id] });
}}
/>
</Card>
</div>
<Card>
<ExceptionsSection gymId={gym.id} />
</Card>
<div className="flex min-w-0 flex-col gap-4">
<Card>
<SectionHeader title="Godziny otwarcia" />
<HoursList gym={gym} />
<div className="mt-4 mb-2 text-[11px] font-bold uppercase tracking-[0.1em] text-dim">
Wyjątki
</div>
<ExceptionsBlock gymId={gym.id} />
</Card>
<Card>
<SectionHeader title="Dane" />
<div className="grid grid-cols-[auto_1fr] gap-x-4 gap-y-1.5 text-[0.85rem]">
<span className="text-dim">Rodzaj</span>
<b>{gym.kind === 'REAL' ? 'Realna' : 'Wirtualna'}</b>
<span className="text-dim">Prywatna</span>
<b>{gym.isPrivate ? 'tak' : 'nie'}</b>
<span className="text-dim">Pojemność</span>
<b className="font-display tabular-nums">{gym.capacity ?? '—'}</b>
</div>
</Card>
</div>
</div>
</>
);
}
@@ -377,7 +440,7 @@ function EquipmentSection({ gym }: { gym: Gym }) {
className="w-20"
aria-label="Ilość"
/>
<Button size="sm" onClick={add} disabled={!addId || busy}>
<Button size="sm" onClick={add} disabled={!addId || busy} aria-label="Dodaj urządzenie">
{busy ? 'Dodawanie…' : 'Dodaj'}
</Button>
</div>
@@ -387,7 +450,8 @@ function EquipmentSection({ gym }: { gym: Gym }) {
);
}
function ExceptionsSection({ gymId }: { gymId: string }) {
/* Wyjątki: kompaktowy komposer (data + tryb + godziny) i lista w stylu aplikacji. */
function ExceptionsBlock({ gymId }: { gymId: string }) {
const queryClient = useQueryClient();
const { data, isLoading } = useQuery({
queryKey: ['gym-exceptions', gymId],
@@ -402,8 +466,7 @@ function ExceptionsSection({ gymId }: { gymId: string }) {
const [error, setError] = useState('');
const [busy, setBusy] = useState(false);
const add = async (e: React.FormEvent) => {
e.preventDefault();
const add = async () => {
setError('');
setBusy(true);
try {
@@ -431,91 +494,121 @@ function ExceptionsSection({ gymId }: { gymId: string }) {
return (
<>
<SectionHeader
title="Wyjątki w harmonogramie"
description="Jednorazowe zmiany na konkretny dzień (zamknięcie lub inne godziny). Wyjątek znika, gdy dzień minie."
/>
<div className="rounded-xl border border-line bg-input/40 p-3.5">
<div className="mb-2.5 text-[11px] font-bold uppercase tracking-[0.1em] text-dim">
Dodaj wyjątek — data i tryb dnia
</div>
{/* wiersz 1: data + tryb; na wąskich ekranach każde przechodzi na pełną szerokość */}
<div className="flex flex-wrap gap-2.5">
<Input
type="date"
value={date}
onChange={(e) => setDate(e.target.value)}
aria-label="Data wyjątku"
className="w-full min-w-0 sm:w-44 sm:flex-none"
required
/>
<div className="flex h-10 w-full items-center gap-1 rounded-[10px] border border-line bg-card p-1 sm:w-auto">
<button
type="button"
aria-pressed={closed}
onClick={() => setClosed(true)}
className={`h-full flex-1 whitespace-nowrap rounded-[10px] px-3 text-[0.8125rem] font-semibold transition-colors sm:flex-none ${
closed ? 'bg-accent text-accent-ink' : 'text-dim hover:text-ink'
}`}
>
Zamknięte
</button>
<button
type="button"
aria-pressed={!closed}
onClick={() => setClosed(false)}
className={`h-full flex-1 whitespace-nowrap rounded-[10px] px-3 text-[0.8125rem] font-semibold transition-colors sm:flex-none ${
!closed ? 'bg-accent text-accent-ink' : 'text-dim hover:text-ink'
}`}
>
Inne godziny
</button>
</div>
</div>
{/* wiersz 2: godziny + akcja; selecty dzielą miejsce po równo, nigdy się nie łamią */}
{!closed && (
<div className="mt-2.5 flex flex-wrap items-center gap-2.5">
<Select
value={start}
onChange={(e) => setStart(e.target.value)}
aria-label="Od godziny"
className="min-w-0 flex-1 sm:flex-none sm:basis-28"
>
{TIME_SLOTS.map((s) => (
<option key={s} value={s}>
{s}
</option>
))}
</Select>
<Select
value={end}
onChange={(e) => setEnd(e.target.value)}
aria-label="Do godziny"
className="min-w-0 flex-1 sm:flex-none sm:basis-28"
>
{TIME_SLOTS.map((s) => (
<option key={s} value={s}>
{s}
</option>
))}
</Select>
<Button
type="button"
onClick={() => void add()}
disabled={busy || !date}
className="w-full sm:ml-auto sm:w-auto"
>
{busy ? 'Dodawanie…' : 'Dodaj'}
</Button>
</div>
)}
{closed && (
<div className="mt-2.5">
<Button
type="button"
onClick={() => void add()}
disabled={busy || !date}
className="w-full sm:w-auto"
>
{busy ? 'Dodawanie…' : 'Dodaj'}
</Button>
</div>
)}
{error && <ErrorText>{error}</ErrorText>}
</div>
{isLoading ? (
<Loading />
) : (
<div className="divide-y divide-line">
{exceptions.length === 0 && (
<span className="block text-[0.875rem] text-dim">Brak wyjątków.</span>
)}
{exceptions.map((ex) => (
<ListRow
key={ex.id}
title={`${new Date(ex.date).toLocaleDateString('pl-PL')} — ${
ex.availability ? `${ex.availability.start}–${ex.availability.end}` : 'zamknięte'
}`}
actions={
<Button variant="danger" size="sm" type="button" onClick={() => remove(ex.id)}>
exceptions.length > 0 && (
<div className="mt-3 flex flex-col gap-2">
{exceptions.map((ex) => (
<div
key={ex.id}
className="flex items-center gap-3 rounded-[10px] border border-dashed border-danger/40 bg-danger/7 px-3 py-2"
>
<span className="font-display text-[0.85rem] font-bold tabular-nums">
{new Date(ex.date).toLocaleDateString('pl-PL')}
</span>
<span className="min-w-0 flex-1 truncate text-dim text-[0.8125rem]">
{ex.availability
? `${ex.availability.start}–${ex.availability.end}`
: 'zamknięte'}
</span>
<Button variant="danger" size="sm" type="button" onClick={() => void remove(ex.id)}>
Usuń
</Button>
}
/>
))}
</div>
</div>
))}
</div>
)
)}
<form onSubmit={add} className="mt-4">
<div className="grid grid-cols-1 gap-x-4 sm:grid-cols-2">
<Field label="Data">
<Input type="date" value={date} onChange={(e) => setDate(e.target.value)} required />
</Field>
<Field label="Tryb">
<div className="inline-flex gap-1.5 rounded-xl border border-line bg-input p-1">
<button
type="button"
aria-pressed={closed}
onClick={() => setClosed(true)}
className={`rounded-[10px] px-3 py-1.5 text-[0.9rem] font-semibold transition-colors ${
closed ? 'bg-accent text-accent-ink' : 'text-dim hover:text-ink'
}`}
>
Zamknięte
</button>
<button
type="button"
aria-pressed={!closed}
onClick={() => setClosed(false)}
className={`rounded-[10px] px-3 py-1.5 text-[0.9rem] font-semibold transition-colors ${
!closed ? 'bg-accent text-accent-ink' : 'text-dim hover:text-ink'
}`}
>
Inne godziny
</button>
</div>
</Field>
{!closed && (
<>
<Field label="Od">
<Select value={start} onChange={(e) => setStart(e.target.value)}>
{TIME_SLOTS.map((s) => (
<option key={s} value={s}>
{s}
</option>
))}
</Select>
</Field>
<Field label="Do">
<Select value={end} onChange={(e) => setEnd(e.target.value)}>
{TIME_SLOTS.map((s) => (
<option key={s} value={s}>
{s}
</option>
))}
</Select>
</Field>
</>
)}
</div>
{error && <ErrorText>{error}</ErrorText>}
<div className="mt-1">
<Button type="submit" disabled={busy || !date}>
{busy ? 'Dodawanie…' : 'Dodaj wyjątek'}
</Button>
</div>
</form>
</>
);
}

View File

@@ -83,10 +83,12 @@ describe('admin GymDetail page', () => {
renderGymDetail();
expect(await screen.findByRole('heading', { name: 'Gym A' })).toBeInTheDocument();
expect(screen.getByText('Warszawa')).toBeInTheDocument();
expect(screen.getByText('12 członk.')).toBeInTheDocument();
expect(screen.getByText('12 członków')).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Zapisz' })).toBeInTheDocument();
// „Usuń siłownię" jest w top menu (menu kontekstowe) — sprawdzamy zarejestrowaną konfigurację
expect(useTopbarStore.getState().config?.actions?.some((a) => a.label === 'Usuń siłownię')).toBe(true);
expect(
useTopbarStore.getState().config?.actions?.some((a) => a.label === 'Usuń siłownię'),
).toBe(true);
});
it('adding already-added equipment increases its quantity via PATCH', async () => {
@@ -107,11 +109,13 @@ describe('admin GymDetail page', () => {
expect(await screen.findByRole('heading', { name: 'Gym A' })).toBeInTheDocument();
// select pokazuje cały katalog ze specyfikacją — sprzęt już dodany jest oznaczony bieżącą ilością
expect(await screen.findByRole('option', { name: 'hantle · 2.5–25 kg (2 szt.)' })).toBeInTheDocument();
expect(
await screen.findByRole('option', { name: 'hantle · 2.5–25 kg (2 szt.)' }),
).toBeInTheDocument();
fireEvent.change(screen.getByRole('combobox', { name: 'Dodaj urządzenie' }), {
target: { value: 'eq-2' },
});
fireEvent.click(screen.getByRole('button', { name: 'Dodaj' }));
fireEvent.click(screen.getByRole('button', { name: 'Dodaj urządzenie' }));
await waitFor(() => expect(patchBody).toEqual({ quantity: 3 }));
});
});

View File

@@ -28,8 +28,8 @@ test('dodawanie już dodanego sprzętu zwiększa jego ilość', async ({ page })
await expect(page.getByText('Dywan ×3')).toBeVisible();
// select pokazuje cały katalog — ponowne dodanie zwiększa ilość
await page.getByLabel('Dodaj urządzenie').selectOption('eq-2');
await page.getByRole('button', { name: 'Dodaj', exact: true }).click();
await page.getByRole('combobox', { name: 'Dodaj urządzenie' }).selectOption('eq-2');
await page.getByRole('button', { name: 'Dodaj urządzenie' }).click();
await expect(page.getByText('Dywan ×4')).toBeVisible();
});