App: strukturalny adres silowni (komponent AddressFields, formularz tworzenia i edycji, mock e2e)

This commit is contained in:
Tilumca
2026-10-15 00:34:15 +02:00
parent c56d9d12e6
commit 14509f4ed1
6 changed files with 212 additions and 21 deletions

View File

@@ -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 });
}

View File

@@ -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;

View File

@@ -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<AddressFormValues>) => void;
required?: boolean;
}) {
const postalInvalid = values.postalCode.length > 0 && !PL_POSTAL_CODE.test(values.postalCode);
return (
<>
<Field label="Ulica i numer">
<Input
value={values.addressLine1}
onChange={(e) => onChange({ addressLine1: e.target.value })}
placeholder="np. Mokotowska 12A"
required={required}
/>
</Field>
<Field label="Dodatkowe adresowe (piętro, lokal)">
<Input
value={values.addressLine2}
onChange={(e) => onChange({ addressLine2: e.target.value })}
placeholder="np. piętro 2, lokal 5"
/>
</Field>
<Field label="Kod pocztowy">
<Input
value={values.postalCode}
onChange={(e) => onChange({ postalCode: formatPostalCodeInput(e.target.value) })}
placeholder="00-001"
inputMode="numeric"
autoComplete="postal-code"
aria-invalid={postalInvalid || undefined}
required={required}
/>
{postalInvalid && <p className="mt-1 text-[0.8rem] text-danger">Format kodu: 00-001</p>}
</Field>
<Field label="Miasto">
<Input
value={values.city}
onChange={(e) => onChange({ city: e.target.value })}
placeholder="np. Warszawa"
autoComplete="address-level2"
required={required}
/>
</Field>
<Field label="Kraj">
<Select value={values.countryCode} onChange={(e) => onChange({ countryCode: e.target.value })}>
{COUNTRIES.map((c) => (
<option key={c.code} value={c.code}>
{c.label}
</option>
))}
</Select>
</Field>
</>
);
}

View File

@@ -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)
</label>
)}
<Field label="Lokalizacja">
<Input
value={form.location}
onChange={(e) => setForm({ ...form, location: e.target.value })}
placeholder="np. Warszawa"
required={isVirtual}
/>
</Field>
{isVirtual ? (
<Field label="Lokalizacja">
<Input
value={form.location}
onChange={(e) => setForm({ ...form, location: e.target.value })}
placeholder="np. Warszawa"
required
/>
</Field>
) : (
<div className="grid grid-cols-1 gap-x-4 sm:grid-cols-2">
<AddressFields
values={form}
onChange={(patch) => setForm({ ...form, ...patch })}
required
/>
</div>
)}
<Field label="Dostępność (dzień tygodnia + przedział godzin)">
<AvailabilityEditor
value={form.availability}
@@ -129,7 +164,7 @@ export default function GymCreateModal({
<Button type="button" variant="secondary" onClick={onClose}>
Anuluj
</Button>
<Button type="submit" disabled={busy || !form.name.trim() || (isVirtual && !form.location.trim())}>
<Button type="submit" disabled={busy || !canSubmit}>
{busy ? 'Utwieranie…' : 'Utwórz'}
</Button>
</div>

View File

@@ -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 })
<Field label="Nazwa">
<Input value={form.name} onChange={(e) => setForm({ ...form, name: e.target.value })} required />
</Field>
<Field label="Lokalizacja">
<Input value={form.location} onChange={(e) => setForm({ ...form, location: e.target.value })} />
</Field>
{!isReal && (
<Field label="Lokalizacja">
<Input value={form.location} onChange={(e) => setForm({ ...form, location: e.target.value })} />
</Field>
)}
<Field label="Pojemność (osób)">
<Input
type="number"
@@ -66,6 +86,11 @@ export function GymEditForm({ gym, onSaved }: { gym: Gym; onSaved: () => void })
/>
</Field>
</div>
{isReal && (
<div className="grid grid-cols-1 gap-x-4 sm:grid-cols-2">
<AddressFields values={form} onChange={(patch) => setForm({ ...form, ...patch })} />
</div>
)}
{gym.kind === 'VIRTUAL' && (
<label className="mb-3.5 flex cursor-pointer items-center gap-2 text-[0.95rem]">
<input
@@ -83,7 +108,7 @@ export function GymEditForm({ gym, onSaved }: { gym: Gym; onSaved: () => void })
onChange={(availability) => setForm({ ...form, availability })}
/>
</Field>
<Button type="submit" disabled={busy}>
<Button type="submit" disabled={busy || !addressOk}>
{busy ? 'Zapisywanie…' : 'Zapisz'}
</Button>
</form>

View File

@@ -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',