|
| 1 | +'use client'; |
| 2 | +import {useEffect, useMemo, useState} from 'react'; |
| 3 | +import {Blockquote, Box, Button, Checkbox, Dialog, Flex, Text, TextArea} from '@radix-ui/themes'; |
| 4 | +import {useMapStore} from '@/app/store/mapStore'; |
| 5 | +import {useDraftSubmissionStore} from '@/app/store/draftSubmissionStore'; |
| 6 | +import {getDraftSubmission, updateDraftSubmission} from '@/app/utils/draftSubmissions'; |
| 7 | +import { |
| 8 | + finalizeSubmission, |
| 9 | + getFormConfig, |
| 10 | + type FormConfigPublic, |
| 11 | +} from '@/app/utils/api/apiHandlers/postSubmission'; |
| 12 | +import {FIELD_ORDER, FIELD_REGISTRY} from '@/app/components/Forms/fieldRegistry'; |
| 13 | +import {useTurnstile} from '@/app/hooks/useTurnstile'; |
| 14 | + |
| 15 | +const EMAIL_RE = /^[^@\s]+@[^@\s]+\.[^@\s]+$/; |
| 16 | + |
| 17 | +/** |
| 18 | + * The abbreviated submission form for maps started from a portal: shown when |
| 19 | + * the user flips their map to ready-to-share (useMetadataChange opens it via |
| 20 | + * draftSubmissionStore), or from the Save & Share menu while the draft |
| 21 | + * exists. Only the portal's required fields are asked — the map and the |
| 22 | + * portal tag are implicit; finalizing clones the plan server-side so the |
| 23 | + * gallery entry is frozen. |
| 24 | + */ |
| 25 | +export const SubmitToPortalModal: React.FC = () => { |
| 26 | + const promptDocumentId = useDraftSubmissionStore(state => state.promptDocumentId); |
| 27 | + const closePrompt = useDraftSubmissionStore(state => state.closePrompt); |
| 28 | + const setNotification = useMapStore(state => state.setNotification); |
| 29 | + |
| 30 | + const draft = useMemo(() => getDraftSubmission(promptDocumentId), [promptDocumentId]); |
| 31 | + const [config, setConfig] = useState<FormConfigPublic | null>(null); |
| 32 | + const [values, setValues] = useState<Record<string, string>>({}); |
| 33 | + const [acknowledged, setAcknowledged] = useState(false); |
| 34 | + const [isSubmitting, setIsSubmitting] = useState(false); |
| 35 | + const [error, setError] = useState(''); |
| 36 | + const {TurnstileComponent, captchaToken} = useTurnstile(); |
| 37 | + |
| 38 | + useEffect(() => { |
| 39 | + setConfig(null); |
| 40 | + setValues({}); |
| 41 | + setAcknowledged(false); |
| 42 | + setError(''); |
| 43 | + if (!draft) return; |
| 44 | + getFormConfig(draft.portalId).then(response => { |
| 45 | + if (response.ok) setConfig(response.response); |
| 46 | + else setError('Could not load the portal form. Please try again later.'); |
| 47 | + }); |
| 48 | + }, [draft?.portalId, promptDocumentId]); |
| 49 | + |
| 50 | + if (!promptDocumentId || !draft || draft.submitted) return null; |
| 51 | + |
| 52 | + const requiredFields = FIELD_ORDER.filter(name => config?.required_fields?.includes(name)); |
| 53 | + const isValid = |
| 54 | + !!config && |
| 55 | + acknowledged && |
| 56 | + !!captchaToken && |
| 57 | + requiredFields.every(name => { |
| 58 | + const value = (values[name] ?? '').trim(); |
| 59 | + if (!value) return false; |
| 60 | + if (name === 'email') return EMAIL_RE.test(value); |
| 61 | + const spec = FIELD_REGISTRY[name]; |
| 62 | + return !spec.validator || spec.validator(value); |
| 63 | + }); |
| 64 | + |
| 65 | + const dismiss = () => { |
| 66 | + updateDraftSubmission(promptDocumentId, {suppressed: true}); |
| 67 | + closePrompt(); |
| 68 | + }; |
| 69 | + |
| 70 | + const submit = async () => { |
| 71 | + if (!isValid || isSubmitting) return; |
| 72 | + setIsSubmitting(true); |
| 73 | + const response = await finalizeSubmission(draft.submissionId, { |
| 74 | + fields: values, |
| 75 | + tags: [], |
| 76 | + turnstile_token: captchaToken, |
| 77 | + }); |
| 78 | + setIsSubmitting(false); |
| 79 | + if (response.ok) { |
| 80 | + updateDraftSubmission(promptDocumentId, {submitted: true}); |
| 81 | + closePrompt(); |
| 82 | + setNotification({ |
| 83 | + message: |
| 84 | + 'Your map was submitted to the portal gallery. A frozen copy of the current plan was submitted — you can keep editing your own map.', |
| 85 | + importance: 2, |
| 86 | + type: 'success', |
| 87 | + }); |
| 88 | + } else { |
| 89 | + setError(response.error); |
| 90 | + } |
| 91 | + }; |
| 92 | + |
| 93 | + return ( |
| 94 | + <Dialog.Root open onOpenChange={open => !open && dismiss()}> |
| 95 | + <Dialog.Content maxWidth="480px"> |
| 96 | + <Dialog.Title>Submit your map to the portal?</Dialog.Title> |
| 97 | + <Dialog.Description size="2" color="gray"> |
| 98 | + Your map is marked ready to share. Submit a snapshot of the current plan to the{' '} |
| 99 | + {draft.portalId} gallery — you can keep editing your own map afterwards. |
| 100 | + </Dialog.Description> |
| 101 | + {error && ( |
| 102 | + <Blockquote color="red" className="my-2"> |
| 103 | + {error} |
| 104 | + </Blockquote> |
| 105 | + )} |
| 106 | + <Flex direction="column" gap="3" mt="3"> |
| 107 | + {requiredFields.map(name => { |
| 108 | + const spec = FIELD_REGISTRY[name]; |
| 109 | + const isTextArea = spec.component === TextArea; |
| 110 | + return ( |
| 111 | + <Flex key={name} direction="column" gap="1"> |
| 112 | + <Text as="label" size="2" weight="medium"> |
| 113 | + {spec.label} * |
| 114 | + </Text> |
| 115 | + {isTextArea ? ( |
| 116 | + <TextArea |
| 117 | + value={values[name] ?? ''} |
| 118 | + placeholder={spec.label} |
| 119 | + onChange={e => setValues(v => ({...v, [name]: e.target.value}))} |
| 120 | + /> |
| 121 | + ) : ( |
| 122 | + <input |
| 123 | + className="rt-TextFieldInput rt-r-size-2 border border-slate-300 rounded p-2" |
| 124 | + type={spec.type ?? 'text'} |
| 125 | + value={values[name] ?? ''} |
| 126 | + placeholder={spec.label} |
| 127 | + autoComplete={spec.autoComplete} |
| 128 | + onChange={e => setValues(v => ({...v, [name]: e.target.value}))} |
| 129 | + /> |
| 130 | + )} |
| 131 | + </Flex> |
| 132 | + ); |
| 133 | + })} |
| 134 | + <Text as="label" size="2"> |
| 135 | + <Flex gap="2" align="center"> |
| 136 | + <Checkbox |
| 137 | + checked={acknowledged} |
| 138 | + onCheckedChange={checked => setAcknowledged(checked === true)} |
| 139 | + /> |
| 140 | + I understand that my submission will be made available to the Commission and other |
| 141 | + members of the public. |
| 142 | + </Flex> |
| 143 | + </Text> |
| 144 | + {TurnstileComponent} |
| 145 | + <Flex gap="3" justify="end"> |
| 146 | + <Button variant="soft" color="gray" onClick={dismiss} disabled={isSubmitting}> |
| 147 | + Not now |
| 148 | + </Button> |
| 149 | + <Button onClick={submit} disabled={!isValid} loading={isSubmitting}> |
| 150 | + Submit to portal |
| 151 | + </Button> |
| 152 | + </Flex> |
| 153 | + </Flex> |
| 154 | + </Dialog.Content> |
| 155 | + </Dialog.Root> |
| 156 | + ); |
| 157 | +}; |
0 commit comments