117 lines
3.4 KiB
TypeScript
117 lines
3.4 KiB
TypeScript
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="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>
|
|
</>
|
|
);
|
|
}
|