|
| 1 | +import { useState } from 'react' |
| 2 | +import { useTranslation } from 'react-i18next' |
| 3 | +import ModalShell from './modals/ModalShell' |
| 4 | +import { FormField, TextInput, TextArea } from './ui' |
| 5 | +import { useToast } from '../contexts/ToastContext' |
| 6 | +import { useContactModal } from '../contexts/ContactModalContext' |
| 7 | +import { INPUT_BASE_CLASSES } from './ui/inputStyles' |
| 8 | + |
| 9 | +const STATIC_FORMS_API_KEY = 'sf_h86a9a9760cijki4amg2ikb7' |
| 10 | + |
| 11 | +export default function ContactModal() { |
| 12 | + const { t } = useTranslation('common') |
| 13 | + const { isContactModalOpen, closeContactModal } = useContactModal() |
| 14 | + const { showSuccess, showError } = useToast() |
| 15 | + |
| 16 | + const [name, setName] = useState('') |
| 17 | + const [email, setEmail] = useState('') |
| 18 | + const [message, setMessage] = useState('') |
| 19 | + const [isSubmitting, setIsSubmitting] = useState(false) |
| 20 | + |
| 21 | + const resetForm = () => { |
| 22 | + setName('') |
| 23 | + setEmail('') |
| 24 | + setMessage('') |
| 25 | + } |
| 26 | + |
| 27 | + const handleClose = () => { |
| 28 | + resetForm() |
| 29 | + closeContactModal() |
| 30 | + } |
| 31 | + |
| 32 | + const handleSubmit = async () => { |
| 33 | + if (!name.trim() || !email.trim() || !message.trim()) { |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + setIsSubmitting(true) |
| 38 | + |
| 39 | + try { |
| 40 | + const response = await fetch('https://api.staticforms.dev/submit', { |
| 41 | + method: 'POST', |
| 42 | + headers: { |
| 43 | + 'Content-Type': 'application/json', |
| 44 | + }, |
| 45 | + body: JSON.stringify({ |
| 46 | + apiKey: STATIC_FORMS_API_KEY, |
| 47 | + subject: 'Contact from Folio website', |
| 48 | + name: name.trim(), |
| 49 | + email: email.trim(), |
| 50 | + message: message.trim(), |
| 51 | + replyTo: email.trim(), |
| 52 | + }), |
| 53 | + }) |
| 54 | + |
| 55 | + if (response.ok) { |
| 56 | + showSuccess(t('contactModal.success', 'Message sent! We\'ll get back to you soon.')) |
| 57 | + handleClose() |
| 58 | + } else { |
| 59 | + showError(t('contactModal.error', 'Failed to send message. Please try again.')) |
| 60 | + } |
| 61 | + } catch { |
| 62 | + showError(t('contactModal.error', 'Failed to send message. Please try again.')) |
| 63 | + } finally { |
| 64 | + setIsSubmitting(false) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + const isFormValid = name.trim() && email.trim() && message.trim() |
| 69 | + |
| 70 | + return ( |
| 71 | + <ModalShell |
| 72 | + isOpen={isContactModalOpen} |
| 73 | + onClose={handleClose} |
| 74 | + title={t('contactModal.title', 'Get in touch')} |
| 75 | + description={t('contactModal.description', 'Send us a message and we\'ll get back to you shortly.')} |
| 76 | + size="small" |
| 77 | + footer={{ |
| 78 | + secondary: { |
| 79 | + label: t('contactModal.cancel', 'Cancel'), |
| 80 | + onClick: handleClose |
| 81 | + }, |
| 82 | + primary: { |
| 83 | + label: isSubmitting |
| 84 | + ? t('contactModal.sending', 'Sending...') |
| 85 | + : t('contactModal.send', 'Send message'), |
| 86 | + onClick: handleSubmit, |
| 87 | + disabled: !isFormValid || isSubmitting, |
| 88 | + }, |
| 89 | + }} |
| 90 | + > |
| 91 | + <div className="flex flex-col gap-4 items-start w-full"> |
| 92 | + <FormField label={t('contactModal.name', 'Name')} required> |
| 93 | + <TextInput |
| 94 | + value={name} |
| 95 | + onChange={setName} |
| 96 | + placeholder={t('contactModal.namePlaceholder', 'Your name')} |
| 97 | + autoComplete="name" |
| 98 | + name="contact-name" |
| 99 | + className={INPUT_BASE_CLASSES} |
| 100 | + disabled={isSubmitting} |
| 101 | + /> |
| 102 | + </FormField> |
| 103 | + |
| 104 | + <FormField label={t('contactModal.email', 'Email')} required> |
| 105 | + <TextInput |
| 106 | + value={email} |
| 107 | + onChange={setEmail} |
| 108 | + placeholder={t('contactModal.emailPlaceholder', 'your@email.com')} |
| 109 | + autoComplete="email" |
| 110 | + name="contact-email" |
| 111 | + inputMode="email" |
| 112 | + className={INPUT_BASE_CLASSES} |
| 113 | + disabled={isSubmitting} |
| 114 | + /> |
| 115 | + </FormField> |
| 116 | + |
| 117 | + <FormField label={t('contactModal.message', 'Message')} required> |
| 118 | + <TextArea |
| 119 | + value={message} |
| 120 | + onChange={setMessage} |
| 121 | + placeholder={t('contactModal.messagePlaceholder', 'How can we help?')} |
| 122 | + name="contact-message" |
| 123 | + rows={4} |
| 124 | + disabled={isSubmitting} |
| 125 | + /> |
| 126 | + </FormField> |
| 127 | + </div> |
| 128 | + </ModalShell> |
| 129 | + ) |
| 130 | +} |
0 commit comments