"use client"; import { useState } from "react"; import type { HouseType, RepairState, TradeInEstimateInput, } from "@/types/trade-in"; import { AddressInput } from "@/components/trade-in/AddressInput"; interface Props { onSubmit: (input: TradeInEstimateInput) => void; isPending: boolean; error: string | null; } interface FormState { address: string; area_m2: string; rooms: string; floor: string; total_floors: string; year_built: string; house_type: string; repair_state: string; has_balcony: boolean; } const INITIAL: FormState = { address: "", area_m2: "", rooms: "", floor: "", total_floors: "", year_built: "", house_type: "", repair_state: "", has_balcony: false, }; function validate(f: FormState): string | null { if (f.address.trim().length < 3) return "Введите адрес (минимум 3 символа)"; const a = Number(f.area_m2); if (!f.area_m2 || isNaN(a) || a <= 10 || a >= 500) return "Площадь: от 10 до 500 м²"; const r = Number(f.rooms); if (f.rooms === "" || isNaN(r) || r < 0 || r > 10) return "Комнат: 0 (студия) — 10"; const fl = Number(f.floor); if (!f.floor || isNaN(fl) || fl < 1 || fl > 100) return "Этаж: 1—100"; const tf = Number(f.total_floors); if (!f.total_floors || isNaN(tf) || tf < 1 || tf > 100) return "Этажей в доме: 1—100"; if (fl > tf) return "Этаж не может быть больше этажей в доме"; if (f.year_built !== "") { const y = Number(f.year_built); if (isNaN(y) || y < 1800 || y > 2100) return "Год постройки: 1800—2100"; } return null; } function toPayload(f: FormState): TradeInEstimateInput { const out: TradeInEstimateInput = { address: f.address.trim(), area_m2: Number(f.area_m2), rooms: Number(f.rooms), floor: Number(f.floor), total_floors: Number(f.total_floors), has_balcony: f.has_balcony, }; if (f.year_built !== "") out.year_built = Number(f.year_built); if (f.house_type) out.house_type = f.house_type as HouseType; if (f.repair_state) out.repair_state = f.repair_state as RepairState; return out; } export function EstimateForm({ onSubmit, isPending, error }: Props) { const [form, setForm] = useState(INITIAL); const [touched, setTouched] = useState(false); const valError = validate(form); const canSubmit = !valError && !isPending; function field(key: keyof FormState) { return (e: React.ChangeEvent) => { const value = e.target.type === "checkbox" ? (e.target as HTMLInputElement).checked : e.target.value; setForm((s) => ({ ...s, [key]: value })); }; } function handleSubmit(e: React.FormEvent) { e.preventDefault(); setTouched(true); if (canSubmit) { onSubmit(toPayload(form)); } } return (

Параметры квартиры

шаг 1/1
{/* Address with autocomplete */}
setForm((s) => ({ ...s, address: val }))} placeholder="ул. Малышева, 30 · Цвиллинга, 58 · Куйбышева, 48…" />
{/* Area + Rooms */}
м²
{/* Floor + Total floors */}
{/* Year + House type */}
{/* Repair */}
{/* Balcony */}
{touched && valError && (
{valError}
)} {error && (
{error}
)}
Кэш по адресу — 24 ч ● готов
); }