From 14509f4ed1dbbad52ad91be1b6496a3dbcb4c6c2 Mon Sep 17 00:00:00 2001 From: Tilumca Date: Thu, 15 Oct 2026 00:34:15 +0200 Subject: [PATCH] App: strukturalny adres silowni (komponent AddressFields, formularz tworzenia i edycji, mock e2e) --- app/src/api/queries.ts | 12 ++- app/src/api/types.ts | 7 ++ app/src/components/AddressFields.tsx | 111 +++++++++++++++++++++ app/src/components/gyms/GymCreateModal.tsx | 61 ++++++++--- app/src/pages/gyms/GymSections.tsx | 37 +++++-- app/test/e2e/mock-api.ts | 5 + 6 files changed, 212 insertions(+), 21 deletions(-) create mode 100644 app/src/components/AddressFields.tsx diff --git a/app/src/api/queries.ts b/app/src/api/queries.ts index 39568a7..c6f6371 100644 --- a/app/src/api/queries.ts +++ b/app/src/api/queries.ts @@ -153,6 +153,14 @@ export function joinGym(gymId: string) { ); } +export interface GymAddressBody { + addressLine1?: string | null; + addressLine2?: string | null; + postalCode?: string | null; + city?: string | null; + countryCode?: string | null; +} + export function createGym(body: { name: string; location?: string | null; @@ -160,7 +168,7 @@ export function createGym(body: { capacity?: number | null; kind?: GymKind; isPrivate?: boolean; -}) { +} & GymAddressBody) { return api<{ gym: UserGym }>('/gyms', { method: 'POST', body }); } @@ -206,7 +214,7 @@ export function updateGym( availability?: Availability | null; capacity?: number | null; isPrivate?: boolean; - }, + } & GymAddressBody, ) { return api<{ gym: Gym }>(`/gyms/${gymId}`, { method: 'PATCH', body }); } diff --git a/app/src/api/types.ts b/app/src/api/types.ts index 05eeb6c..7c87317 100644 --- a/app/src/api/types.ts +++ b/app/src/api/types.ts @@ -106,7 +106,14 @@ export interface GymPhoto { export interface Gym { id: string; name: string; + /** Display string; dla REAL derivowany z adresu strukturalnego. */ location: string | null; + /** Adres strukturalny (tylko REAL). */ + 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/app/src/components/AddressFields.tsx b/app/src/components/AddressFields.tsx new file mode 100644 index 0000000..b08e374 --- /dev/null +++ b/app/src/components/AddressFields.tsx @@ -0,0 +1,111 @@ +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/app/src/components/gyms/GymCreateModal.tsx b/app/src/components/gyms/GymCreateModal.tsx index f31ab86..c153645 100644 --- a/app/src/components/gyms/GymCreateModal.tsx +++ b/app/src/components/gyms/GymCreateModal.tsx @@ -3,6 +3,11 @@ import { useQueryClient } from '@tanstack/react-query'; import { createGym, useMe } from '../../api/queries'; import type { Availability, GymKind } from '../../api/types'; import { Button, Field, Input, Modal, Select } from '../ui'; +import { + AddressFields, + addressPayload, + isAddressComplete, +} from '../AddressFields'; import { useToast } from '../toast'; import { AvailabilityEditor } from '../AvailabilityEditor'; import { emptyAvailability } from '../../lib/availability'; @@ -29,6 +34,11 @@ export default function GymCreateModal({ kind: 'VIRTUAL' as GymKind, isPrivate: false, location: '', + addressLine1: '', + addressLine2: '', + postalCode: '', + city: '', + countryCode: 'PL', availability: emptyAvailability(), capacity: '', }); @@ -36,17 +46,31 @@ export default function GymCreateModal({ useEffect(() => { if (open) { - setForm({ name: '', kind: 'VIRTUAL', isPrivate: false, location: '', availability: emptyAvailability(), capacity: '' }); + setForm({ + name: '', + kind: 'VIRTUAL', + isPrivate: false, + location: '', + addressLine1: '', + addressLine2: '', + postalCode: '', + city: '', + countryCode: 'PL', + availability: emptyAvailability(), + capacity: '', + }); setBusy(false); } }, [open]); const isVirtual = form.kind === 'VIRTUAL'; + const canSubmit = + !!form.name.trim() && + (isVirtual ? !!form.location.trim() : isAddressComplete(form)); const handleCreate = async (e: React.FormEvent) => { e.preventDefault(); - if (!form.name.trim()) return; - if (isVirtual && !form.location.trim()) return; + if (!canSubmit) return; setBusy(true); try { const availability: Availability | null = form.availability.some((d) => d != null) @@ -56,7 +80,8 @@ export default function GymCreateModal({ name: form.name.trim(), kind: form.kind, isPrivate: isVirtual ? form.isPrivate : undefined, - location: form.location.trim() || null, + location: isVirtual ? form.location.trim() || null : undefined, + ...(isVirtual ? {} : addressPayload(form)), availability, capacity: form.capacity ? parseInt(form.capacity) : null, }); @@ -103,14 +128,24 @@ export default function GymCreateModal({ Prywatna (ukryta przed innymi) )} - - setForm({ ...form, location: e.target.value })} - placeholder="np. Warszawa" - required={isVirtual} - /> - + {isVirtual ? ( + + setForm({ ...form, location: e.target.value })} + placeholder="np. Warszawa" + required + /> + + ) : ( +
+ setForm({ ...form, ...patch })} + required + /> +
+ )} Anuluj - diff --git a/app/src/pages/gyms/GymSections.tsx b/app/src/pages/gyms/GymSections.tsx index 39ad6eb..84639bc 100644 --- a/app/src/pages/gyms/GymSections.tsx +++ b/app/src/pages/gyms/GymSections.tsx @@ -12,30 +12,48 @@ import { formatEquipmentSpec, formatGymEquipment } from '../../lib/equipment'; import { Button, Chip, Field, Input, Loading, SectionHeader, Select } from '../../components/ui'; import { useToast } from '../../components/toast'; import { AvailabilityEditor } from '../../components/AvailabilityEditor'; +import { + AddressFields, + addressPayload, + isAddressComplete, +} from '../../components/AddressFields'; import { normalizeAvailability } from '../../lib/availability'; -/** Formularz edycji danych siłowni (nazwa/lokalizacja/pojemność/prywatność/dostępność). */ +/** Formularz edycji danych siłowni (nazwa/adres|lokalizacja/pojemność/prywatność/dostępność). */ export function GymEditForm({ gym, onSaved }: { gym: Gym; onSaved: () => void }) { const { push } = useToast(); + const isReal = gym.kind === 'REAL'; 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() ?? '', isPrivate: gym.isPrivate, }); const [busy, setBusy] = useState(false); + const anyAddress = !!(form.addressLine1 || form.addressLine2 || form.postalCode || form.city); + const addressOk = !isReal || !anyAddress || isAddressComplete(form); const submit = async (e: React.FormEvent) => { e.preventDefault(); + if (!addressOk) return; setBusy(true); try { const availability: Availability | null = form.availability.some((d) => d != null) ? form.availability : null; + // Bez adresu w bazie i pustych pól → nie rusamy adresu (stara, swobodna lokalizacja zostaje). + const sendAddress = + isReal && (anyAddress || !!(gym.addressLine1 ?? gym.postalCode ?? gym.city)); await updateGym(gym.id, { name: form.name, - location: form.location || null, + location: isReal ? undefined : form.location || null, + ...(sendAddress ? addressPayload(form) : {}), availability, capacity: form.capacity ? parseInt(form.capacity) : null, isPrivate: gym.kind === 'VIRTUAL' ? form.isPrivate : undefined, @@ -55,9 +73,11 @@ export function GymEditForm({ gym, onSaved }: { gym: Gym; onSaved: () => void }) setForm({ ...form, name: e.target.value })} required /> - - setForm({ ...form, location: e.target.value })} /> - + {!isReal && ( + + setForm({ ...form, location: e.target.value })} /> + + )} void }) /> + {isReal && ( +
+ setForm({ ...form, ...patch })} /> +
+ )} {gym.kind === 'VIRTUAL' && (
- diff --git a/app/test/e2e/mock-api.ts b/app/test/e2e/mock-api.ts index 3526852..6fe2b94 100644 --- a/app/test/e2e/mock-api.ts +++ b/app/test/e2e/mock-api.ts @@ -94,6 +94,11 @@ export const TEST_GYM_OWNED = { id: 'gym-1', name: 'Żelazna Świątynia', location: 'Warszawa', + addressLine1: 'Mokotowska 12A', + addressLine2: null, + postalCode: '00-900', + city: 'Warszawa', + countryCode: 'PL', availability: [{ start: '06:00', end: '22:00' }, null, null, null, null, null, null], capacity: 50, kind: 'REAL',