diff --git a/packages/site/app/contact/ContactForm.tsx b/packages/site/app/contact/ContactForm.tsx index 5baac9bd20..aa7f5850b6 100644 --- a/packages/site/app/contact/ContactForm.tsx +++ b/packages/site/app/contact/ContactForm.tsx @@ -5,7 +5,16 @@ import classNames from 'classnames'; import { useEffect, useState } from 'react'; import { supabase } from '../initSupabase'; import { options } from './data'; -import { Button, useEventTracker } from '@tet/ui'; +import { + Button, + Field, + FormSectionGrid, + Input, + OptionValue, + Select, + Textarea, + useEventTracker, +} from '@tet/ui'; import { useRouter, useSearchParams } from 'next/navigation'; type FormData = { @@ -14,6 +23,7 @@ type FormData = { prenom: string; nom: string; email: string; + tel: string; message: string; }; @@ -23,12 +33,14 @@ const initFormData: FormData = { prenom: '', nom: '', email: '', + tel: '', message: '', }; const ContactForm = () => { const [formData, setFormData] = useState(initFormData); const [status, setStatus] = useState<'success' | 'error' | null>(null); + const [isError, setIsError] = useState(false); const searchParams = useSearchParams(); const router = useRouter(); @@ -47,44 +59,54 @@ const ContactForm = () => { }); }; - const handleChangeSelect = (event: React.ChangeEvent) => { - const option = event.target.options[event.target.selectedIndex]; - const categorie = option.closest('optgroup')?.label ?? ''; + const handleChangeSelect = (value: OptionValue) => { + const categorie = options.find((opt) => + opt.options.some((o) => o.value === value) + ); + const option = categorie?.options.find((opt) => opt.value === value); setFormData((prevState) => ({ ...prevState, - categorie, - objet: { value: event.target.value, label: option.innerText }, + categorie: categorie?.title ?? '', + objet: option ?? { value: '', label: '' }, })); }; const handleSubmit = async (event: React.FormEvent) => { event.preventDefault(); - const sentData = { - ...formData, - objet: formData.objet.label, - }; - - const { data, error } = await supabase.functions.invoke( - 'site_send_message', - { - body: sentData, - } - ); - - if (data) { - setStatus('success'); - setFormData(initFormData); - if (objet !== null) { - router.push('/contact'); - } - } else if (error) { - console.error(error); - setStatus('error'); + if ( + !formData.objet || + !formData.nom || + !formData.prenom || + !formData.email + ) { + setIsError(true); } else { - console.error('site_send_message : aucune donnée reçue'); - setStatus('error'); + setIsError(false); + const sentData = { + ...formData, + objet: formData.objet.label, + }; + const { data, error } = await supabase.functions.invoke( + 'site_send_message', + { + body: sentData, + } + ); + if (data) { + setStatus('success'); + setFormData(initFormData); + if (objet !== null) { + router.push('/contact'); + } + } else if (error) { + console.error(error); + setStatus('error'); + } else { + console.error('site_send_message : aucune donnée reçue'); + setStatus('error'); + } } }; @@ -105,7 +127,7 @@ const ContactForm = () => { if (optionGroup && option) { setFormData((prevState) => ({ ...prevState, - categorie: optionGroup.group, + categorie: optionGroup.title, objet: option, message: objet === 'panier' @@ -124,97 +146,110 @@ const ContactForm = () => { handleSubmit(event); tracker('envoyer_message', {}); }} + className="border border-grey-4 rounded-lg max-md:p-4 md:max-lg:p-10 p-20" > -
- - -
+ - + + + -
- - -
+ + + -
- - -
+ + + -
- -