From 7aa01057d6dda5e54b3871dfedbe6545f630dfd6 Mon Sep 17 00:00:00 2001 From: Marine Heckler <53305047+mariheck@users.noreply.github.com> Date: Wed, 16 Oct 2024 09:49:20 +0200 Subject: [PATCH] =?UTF-8?q?feat(site):=20Mise=20=C3=A0=20jour=20du=20formu?= =?UTF-8?q?laire=20de=20contact=20(#3339)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/site/app/contact/ContactForm.tsx | 263 ++++++++++-------- packages/site/app/contact/data.ts | 6 +- packages/site/app/contact/page.tsx | 32 +-- packages/site/components/icones/PhoneIcon.tsx | 17 -- packages/ui/src/design-system/Alert/Alert.tsx | 13 +- supabase/functions/site_send_message/index.ts | 8 +- 6 files changed, 184 insertions(+), 155 deletions(-) delete mode 100644 packages/site/components/icones/PhoneIcon.tsx 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" > -
- - -
+ - + + + -
- - -
+ + + -
- - -
+ + + -
- -