import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import { Button, Card, Field } from '@barev/ui'; import { errorText, haptic, useI18n, useToast, useTRPC } from '@barev/web'; import { useState } from 'react'; const WHEN = ['month', 'quarter', 'later', 'date'] as const; const PACKAGES = ['CONSULT', 'TURNKEY', 'CUSTOM'] as const; const YES_NO = ['flat', 'registration', 'socialCard', 'bankCard', 'residence'] as const; type When = (typeof WHEN)[number]; type Pkg = (typeof PACKAGES)[number]; /** Конструктор переезда в приложении: когда, какой пакет, что нужно. После отправки — план по шагам */ export function MoveForm() { const { t, amd } = useI18n(); const trpc = useTRPC(); const qc = useQueryClient(); const toast = useToast(); const me = useQuery(trpc.client.me.queryOptions()); const [when, setWhen] = useState(null); const [date, setDate] = useState(''); const [pkg, setPkg] = useState(null); const [airport, setAirport] = useState<'none' | 'standard' | 'premium'>('standard'); const [stay, setStay] = useState<'none' | 'flat' | 'hotel'>('hotel'); const [budgetFrom, setBudgetFrom] = useState(''); const [budgetTo, setBudgetTo] = useState(''); const [yes, setYes] = useState>({ flat: true, registration: true, socialCard: true, bankCard: false, residence: false }); const [comment, setComment] = useState(''); const [error, setError] = useState(null); const create = useMutation( trpc.client.relocation.create.mutationOptions({ onSuccess: () => { haptic.ok(); toast(t('client.move.created')); void qc.invalidateQueries(); window.scrollTo({ top: 0 }); }, onError: (e) => toast(errorText(t, e), 'error'), }), ); function submit() { if (!when || (when === 'date' && !date)) return setError(t('client.move.errWhen')); if (!pkg) return setError(t('client.move.errPackage')); const any = airport !== 'none' || stay !== 'none' || YES_NO.some((k) => yes[k]); if (pkg === 'CUSTOM' && !any) return setError(t('client.move.errEmpty')); setError(null); const num = (v: string) => (v ? Number(v) : undefined); create.mutate({ moveWhen: when, moveDate: when === 'date' ? date : undefined, package: pkg, options: pkg === 'CUSTOM' ? { airport, stay, stayBudgetFrom: stay === 'none' ? undefined : num(budgetFrom), stayBudgetTo: stay === 'none' ? undefined : num(budgetTo), ...yes } : undefined, comment: comment.trim() || undefined, }); } const seg = (value: V, options: readonly V[], label: (v: V) => string, onChange: (v: V) => void) => (
{options.map((o) => ( ))}
); return (
{me.data?.firstName ? t('client.move.formHello', { name: me.data.firstName }) : t('client.move.title')}
{t('client.move.formTitle')}
{t('client.move.formSub')}

{t('client.move.whenTitle')}

{WHEN.map((w) => ( ))}
{when === 'date' && ( setDate(e.target.value)} /> )}

{t('client.move.packageTitle')}

{PACKAGES.map((p) => ( ))}
{pkg === 'CUSTOM' && (

{t('client.move.customTitle')}

{t('landing.quiz.airport')} {seg(airport, ['none', 'standard', 'premium'] as const, (v) => t(`landing.quiz.airport_${v}`), setAirport)}
{t('landing.quiz.stay')} {seg(stay, ['none', 'flat', 'hotel'] as const, (v) => t(`landing.quiz.stay_${v}`), setStay)}
{stay !== 'none' && (
setBudgetFrom(e.target.value.replace(/\D/g, '').slice(0, 7))} /> setBudgetTo(e.target.value.replace(/\D/g, '').slice(0, 7))} />
)} {YES_NO.map((k) => (
{t(`landing.quiz.${k}`)} {seg(yes[k] ? 'yes' : 'no', ['no', 'yes'] as const, (v) => t(`landing.quiz.${v}`), (v) => setYes({ ...yes, [k]: v === 'yes' }))}
))}
)}