import { createFileRoute } from '@tanstack/react-router' import { useState } from 'react' import { m, mKey, mExists } from '@/i18n/messages' import { useLocale } from '@/i18n/config' import { asI18n } from '@pikku/react' import { ActionIcon, Alert, Box, Button, Checkbox, Container, Divider, Group, NumberInput, Paper, Select, SimpleGrid, Stack, Text, Textarea, TextInput, Tooltip, } from '@pikku/mantine/core' import { Info, Plus, Trash2 } from 'lucide-react' import { usePikkuMutation } from '@project/functions-sdk/pikku/api.gen' import { MarketingShell } from '../components/MarketingShell' import { PageTitle } from '../components/AppLayout' type DateOption = { startDate: string; endDate: string } const MAX_DATE_OPTIONS = 3 type State = { eventName: string eventOutline: string description: string // 0–3 prioritized date options, in priority order (first = preferred). dateOptions: DateOption[] expectedPersons: string halfHouse: boolean clientName: string contactEmail: string contactPhone: string website: string notes: string agbAccepted: boolean privacyAccepted: boolean } const empty = (search?: { date?: string; from?: string; to?: string }): State => { const seedStart = search?.from ?? search?.date ?? '' const seedEnd = search?.to ?? search?.date ?? '' return { eventName: '', eventOutline: '', description: '', dateOptions: [{ startDate: seedStart, endDate: seedEnd }], expectedPersons: '', halfHouse: false, clientName: '', contactEmail: '', contactPhone: '', website: '', notes: '', agbAccepted: false, privacyAccepted: false, } } function EnquiryPage() { useLocale() const search = Route.useSearch() const [form, setForm] = useState(() => empty(search)) const [success, setSuccess] = useState(null) const mutation = usePikkuMutation('submitEnquiry', { onSuccess: (data) => setSuccess(data.enquiryId), }) const set = (key: K, value: State[K]) => setForm((f) => ({ ...f, [key]: value })) const setOption = (i: number, patch: Partial) => setForm((f) => ({ ...f, dateOptions: f.dateOptions.map((o, idx) => (idx === i ? { ...o, ...patch } : o)), })) const addOption = () => setForm((f) => f.dateOptions.length >= MAX_DATE_OPTIONS ? f : { ...f, dateOptions: [...f.dateOptions, { startDate: '', endDate: '' }] }, ) const removeOption = (i: number) => setForm((f) => ({ ...f, dateOptions: f.dateOptions.filter((_, idx) => idx !== i) })) // A row is "complete" only when both dates are set and ordered. Partially // filled or reversed rows block submission; fully empty form = no dates (ok). const rowState = (o: DateOption): 'empty' | 'complete' | 'invalid' => { if (!o.startDate && !o.endDate) return 'empty' if (o.startDate && o.endDate && o.startDate <= o.endDate) return 'complete' return 'invalid' } const optionsValid = form.dateOptions.every((o) => rowState(o) !== 'invalid') const submit = () => { const dateOptions = form.dateOptions .filter((o) => rowState(o) === 'complete') .map((o) => ({ startDate: o.startDate, endDate: o.endDate })) mutation.mutate({ venueSlug: 'drawehn', contact: { name: form.clientName.trim() || undefined, contactEmail: form.contactEmail, contactPhone: form.contactPhone || undefined, website: form.website || undefined, }, event: { eventName: form.eventName, eventOutline: form.eventOutline || undefined, description: form.description || undefined, expectedPersons: form.expectedPersons ? Number(form.expectedPersons) : undefined, halfHouse: form.halfHouse, }, dateOptions: dateOptions.length ? dateOptions : undefined, notes: form.notes || undefined, privacyAccepted: form.privacyAccepted as true, }) } if (success) { return ( {m.common_pages_enquiry_success_body({ enquiryId: success })} ) } const canSubmit = form.eventName.trim() && form.contactEmail.trim() && form.agbAccepted && form.privacyAccepted && optionsValid return (
} required value={form.eventName} onChange={(e) => set('eventName', e.currentTarget.value)} /> } value={form.eventOutline} onChange={(e) => set('eventOutline', e.currentTarget.value)} />