'use client' import dynamic from 'next/dynamic' import { useCallback, useEffect, useState } from 'react' import { texts } from '@/texts' import type { EventWizardEditLocks, EventWizardFormData } from '@/components/events/create/consumer/types' import ConsumerInput from '@/components/consumer/ConsumerInput' import InlineNotice from '@/components/feedback/InlineNotice' import MapLocationPicker from '@/components/events/create/MapLocationPicker' import { useEventScheduleConstraints } from '@/components/events/schedule/eventScheduleConstraints' import { parseConsumerEventWizardStep2, type ConsumerEventWizardStep2FieldErrors } from '@/validation/consumerEventWizardStep2' import { addToast } from '@/lib/toast' const EventScheduleFieldControl = dynamic(() => import('@/components/events/schedule/EventScheduleFieldControl'), { ssr: false, }) interface Step2ScheduleLocationProps { data: EventWizardFormData locks?: EventWizardEditLocks onBindSubmit: (submit: (() => void) | null) => void onChange: (patch: Partial) => void onNext: (patch: Partial) => void } export default function Step2ScheduleLocation({ data, locks, onBindSubmit, onChange, onNext }: Step2ScheduleLocationProps) { const [errors, setErrors] = useState({}) const { todayIso, startMinimumTime, endMinimumTime } = useEventScheduleConstraints(data.startDate, data.startTime, data.endDate) const scheduleLocked = Boolean(locks?.schedule) const locationLocked = Boolean(locks?.location) const showLockNotice = Boolean(locks?.notice && (scheduleLocked || locationLocked)) const patch = (next: Partial) => { setErrors({}) onChange(next) } const handleSubmit = useCallback(() => { const result = parseConsumerEventWizardStep2({ startDate: data.startDate, startTime: data.startTime, endDate: data.endDate, endTime: data.endTime, address: data.address, addressVisibility: data.addressVisibility, generalArea: data.addressVisibility === 'attendees_only' ? data.generalArea : '', }) if (!result.success) { setErrors(result.fieldErrors) addToast({ title: texts.common.formIncomplete, description: Object.values(result.fieldErrors).find(Boolean) ?? texts.common.formValidationDefault, color: 'danger', }) return } setErrors({}) onNext({ ...result.data, lat: data.lat, lng: data.lng, generalArea: result.data.addressVisibility === 'attendees_only' ? (result.data.generalArea?.trim() ?? '') : '', }) }, [data, onNext]) useEffect(() => { onBindSubmit(handleSubmit) return () => { onBindSubmit(null) } }, [handleSubmit, onBindSubmit]) return (
{ event.preventDefault() handleSubmit() }} > {showLockNotice ? {locks?.notice} : null}
{texts.events.scheduleSectionTitle} {texts.events.scheduleSectionDescription}
{ if (scheduleLocked) return patch({ startDate: String(value) }) }} /> { if (scheduleLocked) return patch({ startTime: Number(value) }) }} /> { if (scheduleLocked) return patch({ endDate: String(value) }) }} /> { if (scheduleLocked) return patch({ endTime: Number(value) }) }} />
{ if (locationLocked) return patch({ address: typeof value === 'string' ? value : '' }) }} /> {errors.address ?

{errors.address}

: null}
{ if (locationLocked) return patch({ address }) }} onChange={({ lat, lng }) => { if (locationLocked) return patch({ lat, lng }) }} /> { if (locationLocked) return patch({ addressVisibility: checked ? 'attendees_only' : 'public', generalArea: checked ? data.generalArea : '', }) }} /> {data.addressVisibility === 'attendees_only' ? (
{ if (locationLocked) return patch({ generalArea: typeof value === 'string' ? value : '' }) }} /> {errors.generalArea ?

{errors.generalArea}

: null}
) : null} ) }