diff --git a/admin/src/api/types.ts b/admin/src/api/types.ts index a8a4ee3..87da3ff 100644 --- a/admin/src/api/types.ts +++ b/admin/src/api/types.ts @@ -143,7 +143,14 @@ export interface GymPhoto { export interface Gym { id: string; name: string; + /** Display string derivowany z adresu (fallback dla starych, swobodnych lokalizacji). */ location: string | null; + /** Strukturalny adres (wzorzec Google/UPU). */ + addressLine1?: string | null; + addressLine2?: string | null; + postalCode?: string | null; + city?: string | null; + countryCode?: string | null; availability: Availability | null; capacity: number | null; kind: GymKind; diff --git a/admin/src/components/AddressFields.tsx b/admin/src/components/AddressFields.tsx new file mode 100644 index 0000000..31c0af3 --- /dev/null +++ b/admin/src/components/AddressFields.tsx @@ -0,0 +1,116 @@ +import { Field, Input, Select } from './ui'; + +/** + * Adres siłowni — pola strukturalne (wzorzec Google/UPU: linia adresu, + * kod pocztowy, miejscowość, kraj). Display string `location` składa serwer. + */ + +export const COUNTRIES: { code: string; label: string }[] = [ + { code: 'PL', label: 'Polska' }, + { code: 'DE', label: 'Niemcy' }, + { code: 'CZ', label: 'Czechy' }, + { code: 'SK', label: 'Słowacja' }, + { code: 'LT', label: 'Litwa' }, + { code: 'GB', label: 'Wielka Brytania' }, +]; + +/** Polskie kody pocztowe: 00-001. */ +export const PL_POSTAL_CODE = /^\d{2}-\d{3}$/; + +/** Maska kodu pocztowego: cyfry + automatyczny myślnik (00001 → 00-001). */ +export function formatPostalCodeInput(raw: string): string { + const digits = raw.replace(/\D/g, '').slice(0, 5); + return digits.length > 2 ? `${digits.slice(0, 2)}-${digits.slice(2)}` : digits; +} + +export interface AddressFormValues { + addressLine1: string; + addressLine2: string; + postalCode: string; + city: string; + countryCode: string; +} + +export function isAddressComplete(values: AddressFormValues): boolean { + return ( + values.addressLine1.trim().length > 0 && + values.city.trim().length > 0 && + PL_POSTAL_CODE.test(values.postalCode) + ); +} + +export function addressPayload(values: AddressFormValues) { + return { + addressLine1: values.addressLine1.trim() || null, + addressLine2: values.addressLine2.trim() || null, + postalCode: values.postalCode.trim() || null, + city: values.city.trim() || null, + countryCode: values.countryCode, + }; +} + +export function AddressFields({ + values, + onChange, + required = false, +}: { + values: AddressFormValues; + onChange: (patch: Partial) => void; + required?: boolean; +}) { + const postalInvalid = values.postalCode.length > 0 && !PL_POSTAL_CODE.test(values.postalCode); + return ( + <> + + onChange({ addressLine1: e.target.value })} + placeholder="np. Mokotowska 12A" + required={required} + /> + + + onChange({ addressLine2: e.target.value })} + placeholder="np. piętro 2, lokal 5" + /> + + + onChange({ postalCode: formatPostalCodeInput(e.target.value) })} + placeholder="00-001" + inputMode="numeric" + autoComplete="postal-code" + aria-invalid={postalInvalid || undefined} + required={required} + /> + {postalInvalid && ( +

Format kodu: 00-001

+ )} +
+ + onChange({ city: e.target.value })} + placeholder="np. Warszawa" + autoComplete="address-level2" + required={required} + /> + + + + + + ); +} diff --git a/admin/src/pages/gyms/GymDetail.tsx b/admin/src/pages/gyms/GymDetail.tsx index e27e390..304eb5e 100644 --- a/admin/src/pages/gyms/GymDetail.tsx +++ b/admin/src/pages/gyms/GymDetail.tsx @@ -19,6 +19,11 @@ import { useTopbar, type TopbarCustomGroup } from '../../topbar'; import { api } from '../../api/client'; import { AuthImage } from '../../components/AuthImage'; import { UserPicker } from '../../components/UserPicker'; +import { + AddressFields, + addressPayload, + isAddressComplete, +} from '../../components/AddressFields'; import { useAdminUsers } from '../users/user-queries'; import type { Equipment, Gym, GymException, GymOwner, GymPhoto } from '../../api/types'; import { formatEquipmentSpec, formatGymEquipment } from '../../lib/equipment'; @@ -30,6 +35,7 @@ import { ConfirmDialog, ErrorText, Field, + H3, Input, Loading, Modal, @@ -307,24 +313,34 @@ function ExceptionsEditor({ gymId }: { gymId: string }) { function GymEditForm({ gym, onSaved }: { gym: Gym; onSaved: () => void }) { const [form, setForm] = useState({ name: gym.name, - location: gym.location ?? '', + addressLine1: gym.addressLine1 ?? '', + addressLine2: gym.addressLine2 ?? '', + postalCode: gym.postalCode ?? '', + city: gym.city ?? '', + countryCode: gym.countryCode ?? 'PL', availability: normalizeAvailability(gym.availability), capacity: gym.capacity?.toString() ?? '', }); const [error, setError] = useState(''); const [busy, setBusy] = useState(false); const [saved, setSaved] = useState(false); + const anyAddress = !!(form.addressLine1 || form.addressLine2 || form.postalCode || form.city); + const addressOk = !anyAddress || isAddressComplete(form); const submit = async (e: React.FormEvent) => { e.preventDefault(); + if (!addressOk) return; setError(''); setBusy(true); try { + // Bez adresu w bazie i pustych pól → nie rusamy adresu (stara, swobodna lokalizacja zostaje). + const sendAddress = + anyAddress || !!(gym.addressLine1 ?? gym.postalCode ?? gym.city); await api(`/admin/gyms/${gym.id}`, { method: 'PATCH', body: { name: form.name, - location: form.location || null, + ...(sendAddress ? addressPayload(form) : {}), availability: form.availability.some((d) => d != null) ? form.availability : null, capacity: form.capacity ? parseInt(form.capacity) : null, }, @@ -348,12 +364,6 @@ function GymEditForm({ gym, onSaved }: { gym: Gym; onSaved: () => void }) { required /> - - setForm({ ...form, location: e.target.value })} - /> - void }) { /> +
+

Adres

+
+
+ setForm({ ...form, ...patch })} /> +
+ {!gym.addressLine1 && gym.location && ( +

+ Dotychczasowa lokalizacja: „{gym.location}”. Podanie adresu nadpisze ją adresem + strukturalnym. +

+ )} void }) { {error && {error}} {saved && !error && Zapisano zmiany.} - @@ -695,6 +717,25 @@ export default function GymDetail() { {gym.isPrivate ? 'tak' : 'nie'} Pojemność {gym.capacity ?? '—'} + {gym.addressLine1 || gym.postalCode || gym.city ? ( + <> + Ulica + {[gym.addressLine1, gym.addressLine2].filter(Boolean).join(', ') || '—'} + Kod i miasto + {[gym.postalCode, gym.city].filter(Boolean).join(' ') || '—'} + {gym.countryCode && gym.countryCode !== 'PL' && ( + <> + Kraj + {gym.countryCode} + + )} + + ) : ( + <> + Lokalizacja + {gym.location ?? '—'} + + )} Właściciele {ownerNames.length > 0 ? ownerNames.join(', ') : '—'} diff --git a/admin/src/pages/gyms/GymForm.tsx b/admin/src/pages/gyms/GymForm.tsx index d385956..4d90952 100644 --- a/admin/src/pages/gyms/GymForm.tsx +++ b/admin/src/pages/gyms/GymForm.tsx @@ -1,8 +1,13 @@ import { useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { api } from '../../api/client'; -import { BackButton, Button, Card, ErrorText, Field, H1, Input } from '../../components/ui'; +import { BackButton, Button, Card, ErrorText, Field, H1, H3, Input } from '../../components/ui'; import { UserPicker } from '../../components/UserPicker'; +import { + AddressFields, + addressPayload, + isAddressComplete, +} from '../../components/AddressFields'; import { AvailabilityEditor } from '../../components/AvailabilityEditor'; import { emptyAvailability } from '../../lib/availability'; import { useAdminUsers } from '../users/user-queries'; @@ -16,16 +21,21 @@ export default function GymForm() { const [form, setForm] = useState({ name: '', ownerId: '', - location: '', + addressLine1: '', + addressLine2: '', + postalCode: '', + city: '', + countryCode: 'PL', availability: emptyAvailability(), capacity: '', }); const [error, setError] = useState(''); const [busy, setBusy] = useState(false); + const addressOk = isAddressComplete(form); const submit = async (e: React.FormEvent) => { e.preventDefault(); - if (!form.ownerId) return; + if (!form.ownerId || !addressOk) return; setError(''); setBusy(true); try { @@ -37,7 +47,7 @@ export default function GymForm() { body: { name: form.name, ownerId: form.ownerId, - location: form.location || null, + ...addressPayload(form), availability, capacity: form.capacity ? parseInt(form.capacity) : null, }, @@ -73,12 +83,6 @@ export default function GymForm() { emptyLabel={usersData ? 'Brak użytkowników' : 'Wczytywanie…'} /> - - setForm({ ...form, location: e.target.value })} - /> - +
+

Adres

+
+
+ setForm({ ...form, ...patch })} + required + /> +
)} {error && {error}} - diff --git a/admin/test/GymDetail.test.tsx b/admin/test/GymDetail.test.tsx index 3f3ed0d..786ea82 100644 --- a/admin/test/GymDetail.test.tsx +++ b/admin/test/GymDetail.test.tsx @@ -13,7 +13,12 @@ const OWNER1 = { id: 'u-owner', firstName: 'Anna', lastName: 'Kowalska', email: const G1: Gym = { id: 'g1', name: 'Gym A', - location: 'Warszawa', + location: 'Polna 4, 00-001 Warszawa', + addressLine1: 'Polna 4', + addressLine2: null, + postalCode: '00-001', + city: 'Warszawa', + countryCode: 'PL', availability: [{ start: '06:00', end: '22:00' }, null, null, null, null, null, null], capacity: 50, kind: 'REAL', @@ -128,7 +133,10 @@ describe('admin GymDetail page', () => { stubGymFetch(); renderGymDetail(); expect(await screen.findByRole('heading', { name: 'Gym A' })).toBeInTheDocument(); - expect(screen.getByText('Warszawa')).toBeInTheDocument(); + // hero pokazuje location (string derivowany), karta Dane pokazuje adres strukturalny + expect(screen.getByText('Polna 4, 00-001 Warszawa')).toBeInTheDocument(); + expect(screen.getByText('Polna 4', { exact: true })).toBeInTheDocument(); + expect(screen.getByText('00-001 Warszawa')).toBeInTheDocument(); expect(screen.getByText('12 członków')).toBeInTheDocument(); expect(screen.getByText('Anna Kowalska')).toBeInTheDocument(); diff --git a/admin/test/e2e/mock-api.ts b/admin/test/e2e/mock-api.ts index 1198f94..804130e 100644 --- a/admin/test/e2e/mock-api.ts +++ b/admin/test/e2e/mock-api.ts @@ -53,7 +53,12 @@ export const TEST_GYM_PHOTOS = [ export const TEST_GYM: Gym = { id: 'gym-1', name: 'Fit Factory', - location: 'Warszawa, Mokotów', + location: 'Bobrowiecka 12, 02-847 Warszawa', + addressLine1: 'Bobrowiecka 12', + addressLine2: null, + postalCode: '02-847', + city: 'Warszawa', + countryCode: 'PL', availability: [{ start: '06:00', end: '22:00' }, null, null, null, null, null, null], capacity: 120, kind: 'REAL',