Admin: sekcja adresu w formularzach silowni (maska kodu 00-001, wybor kraju) + adres w karcie Dane

This commit is contained in:
Tilumca
2026-10-15 00:24:14 +02:00
parent d734a5e411
commit 5db1c7ebbc
6 changed files with 214 additions and 23 deletions

View File

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

View File

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

@@ -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
/>
</Field>
<Field label="Lokalizacja">
<Input
value={form.location}
onChange={(e) => setForm({ ...form, location: e.target.value })}
/>
</Field>
<Field label="Ilość miejsc">
<Input
type="number"
@@ -362,6 +372,18 @@ function GymEditForm({ gym, onSaved }: { gym: Gym; onSaved: () => void }) {
/>
</Field>
</div>
<div className="mb-2 mt-4 border-t border-line pt-4">
<H3>Adres</H3>
</div>
<div className="grid grid-cols-1 gap-x-4 sm:grid-cols-2">
<AddressFields values={form} onChange={(patch) => setForm({ ...form, ...patch })} />
</div>
{!gym.addressLine1 && gym.location && (
<p className="mb-3 text-[0.8rem] text-dim">
Dotychczasowa lokalizacja: „{gym.location}”. Podanie adresu nadpisze ją adresem
strukturalnym.
</p>
)}
<Field label="Dostępność (dzień tygodnia + przedział godzin)">
<AvailabilityEditor
value={form.availability}
@@ -370,7 +392,7 @@ function GymEditForm({ gym, onSaved }: { gym: Gym; onSaved: () => void }) {
</Field>
{error && <ErrorText>{error}</ErrorText>}
{saved && !error && <SuccessText>Zapisano zmiany.</SuccessText>}
<Button type="submit" disabled={busy}>
<Button type="submit" disabled={busy || !addressOk}>
{busy ? 'Zapisywanie…' : 'Zapisz'}
</Button>
</form>
@@ -695,6 +717,25 @@ export default function GymDetail() {
<b>{gym.isPrivate ? 'tak' : 'nie'}</b>
<span className="text-dim">Pojemność</span>
<b className="font-display tabular-nums">{gym.capacity ?? '—'}</b>
{gym.addressLine1 || gym.postalCode || gym.city ? (
<>
<span className="text-dim">Ulica</span>
<b>{[gym.addressLine1, gym.addressLine2].filter(Boolean).join(', ') || '—'}</b>
<span className="text-dim">Kod i miasto</span>
<b>{[gym.postalCode, gym.city].filter(Boolean).join(' ') || '—'}</b>
{gym.countryCode && gym.countryCode !== 'PL' && (
<>
<span className="text-dim">Kraj</span>
<b>{gym.countryCode}</b>
</>
)}
</>
) : (
<>
<span className="text-dim">Lokalizacja</span>
<b>{gym.location ?? '—'}</b>
</>
)}
<span className="text-dim">Właściciele</span>
<b>{ownerNames.length > 0 ? ownerNames.join(', ') : '—'}</b>
</div>

View File

@@ -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…'}
/>
</Field>
<Field label="Lokalizacja">
<Input
value={form.location}
onChange={(e) => setForm({ ...form, location: e.target.value })}
/>
</Field>
<Field label="Ilość miejsc">
<Input
type="number"
@@ -87,6 +91,16 @@ export default function GymForm() {
/>
</Field>
</div>
<div className="mb-2 mt-4 border-t border-line pt-4">
<H3>Adres</H3>
</div>
<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}
@@ -99,7 +113,7 @@ export default function GymForm() {
</p>
)}
{error && <ErrorText>{error}</ErrorText>}
<Button type="submit" disabled={busy || !form.ownerId}>
<Button type="submit" disabled={busy || !form.ownerId || !addressOk}>
{busy ? 'Zapisywanie…' : 'Utwórz siłownię'}
</Button>
</form>

View File

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

View File

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