ldc-bien-etre-animal/resources/ts/front/Components/Form/ContactForm.tsx
2026-09-09 14:23:06 +02:00

377 lines
11 KiB
TypeScript

import { useForm } from "@inertiajs/react";
import { ArrowLeft, ArrowRight } from "lucide-react";
import { type SyntheticEvent, useState } from "react";
import Checkbox from "@/front/Components/Form/Checkbox";
import FileInput from "@/front/Components/Form/FileInput";
import FormSelect from "@/front/Components/Form/FormSelect";
import SuccessModal from "@/front/Components/Form/SuccessModel";
import TextArea from "@/front/Components/Form/TextArea";
import TextInput from "@/front/Components/Form/TextInput";
import CtaButton from "@/front/Components/Layout/CtaButton";
import { useToast } from "@/hooks/use-toast";
import useTrans from "@/hooks/use-translations";
import { GetTranslation } from "@/lib/utils";
import RecaptchaForm from "../cookie/recaptcha/RecaptchaForm";
interface Option {
label: string;
value: string;
}
interface ContactFormProps {
profiles: Option[];
countries: Option[];
backHref?: string;
}
export default function ContactForm({
profiles,
countries,
backHref,
}: Readonly<ContactFormProps>) {
const { toast } = useToast();
const [showSuccess, setShowSuccess] = useState(false);
const { data, setData, errors, post, processing, reset } = useForm({
lastName: "",
firstName: "",
email: "",
phone: "",
profile: "",
company: "",
postalCode: "",
city: "",
country: "",
subject: "",
message: "",
attachment: null as File | null,
acceptsUsage: false,
});
const handleSubmit = (e: SyntheticEvent<HTMLFormElement>) => {
e.preventDefault();
post(route("front.contact.store"), {
forceFormData: true,
preserveScroll: true,
onSuccess: () => {
setShowSuccess(true);
reset();
window.grecaptcha?.reset();
},
onError: () => {
toast({
title: "Erreur",
variant: "destructive",
description:
"Une erreur est survenue. Veuillez vérifier les champs et réessayer.",
});
},
});
};
return (
<>
<RecaptchaForm
data={data}
onSubmit={handleSubmit}
classNames={{ formClassName: "flex flex-col gap-4 lg:gap-8" }}
footer={
<div className="mt-4 flex flex-col items-center gap-3 md:flex-row md:justify-center md:gap-30">
<CtaButton
href={backHref}
icon={<ArrowLeft strokeWidth={3} />}
iconPosition="left"
color="bleu"
width="w-full lg:w-auto"
size="lg"
>
{useTrans("contact_page.form.buttons.back")}
</CtaButton>
<CtaButton
type="submit"
icon={<ArrowRight strokeWidth={3} />}
iconPosition="right"
color="rouge"
width="w-full lg:w-[280px]"
size="lg"
>
{processing
? GetTranslation("contact_page.form.buttons.submitting")
: GetTranslation("contact_page.form.buttons.submit")}
</CtaButton>
</div>
}
>
<div className="grid grid-cols-1 gap-4 md:grid-cols-2 lg:gap-30">
<div>
<TextInput
name="lastName"
label={useTrans("contact_page.form.placeholders.last_name")}
value={data.lastName}
onChange={(e) => {
setData("lastName", e.target.value);
}}
required
disabled={processing}
error={errors.lastName}
/>
{errors.lastName && (
<p className="mt-1 font-poppins-regular text-[12px] text-rouge lg:text-small">
{errors.lastName}
</p>
)}
</div>
<div>
<TextInput
name="firstName"
label={useTrans("contact_page.form.placeholders.first_name")}
value={data.firstName}
onChange={(e) => {
setData("firstName", e.target.value);
}}
required
disabled={processing}
error={errors.firstName}
/>
{errors.firstName && (
<p className="mt-1 font-poppins-regular text-[12px] text-rouge lg:text-small">
{errors.firstName}
</p>
)}
</div>
</div>
<div>
<TextInput
name="email"
type="email"
label={useTrans("contact_page.form.placeholders.email")}
value={data.email}
onChange={(e) => {
setData("email", e.target.value.toLowerCase());
}}
required
disabled={processing}
error={errors.email}
/>
{errors.email && (
<p className="mt-1 font-poppins-regular text-[12px] lg:text-small text-rouge">
{errors.email}
</p>
)}
</div>
<div>
<TextInput
name="phone"
type="tel"
label={useTrans("contact_page.form.placeholders.phone")}
value={data.phone}
onChange={(e) => {
setData("phone", e.target.value);
}}
required
disabled={processing}
error={errors.phone}
/>
{errors.phone && (
<p className="mt-1 font-poppins-regular text-[12px] lg:text-small text-rouge">
{errors.phone}
</p>
)}
</div>
<div>
<FormSelect
name="profile"
label={useTrans("contact_page.form.placeholders.profile")}
options={profiles}
value={data.profile}
onChange={(e) => {
setData("profile", e.target.value);
}}
required
disabled={processing}
error={errors.profile}
/>
{errors.profile && (
<p className="mt-1 font-poppins-regular text-[12px] lg:text-small text-rouge">
{errors.profile}
</p>
)}
</div>
<div>
<TextInput
name="company"
label={useTrans("contact_page.form.placeholders.company")}
value={data.company}
onChange={(e) => {
setData("company", e.target.value);
}}
disabled={processing}
error={errors.company}
/>
{errors.company && (
<p className="mt-1 font-poppins-regular text-[12px] text-rouge lg:text-small">
{errors.company}
</p>
)}
</div>
<div>
<TextInput
name="postalCode"
label={useTrans("contact_page.form.placeholders.postal_code")}
value={data.postalCode}
onChange={(e) => {
setData("postalCode", e.target.value);
}}
disabled={processing}
error={errors.postalCode}
/>
{errors.postalCode && (
<p className="mt-1 font-poppins-regular text-[12px] text-rouge lg:text-small">
{errors.postalCode}
</p>
)}
</div>
<div>
<TextInput
name="city"
label={useTrans("contact_page.form.placeholders.city")}
value={data.city}
onChange={(e) => {
setData("city", e.target.value);
}}
disabled={processing}
error={errors.city}
/>
{errors.city && (
<p className="mt-1 font-poppins-regular text-[12px] text-rouge lg:text-small">
{errors.city}
</p>
)}
</div>
<div>
<FormSelect
name="country"
label={useTrans("contact_page.form.placeholders.country")}
options={countries}
value={data.country}
onChange={(e) => {
setData("country", e.target.value);
}}
disabled={processing}
error={errors.country}
/>
{errors.country && (
<p className="mt-1 font-poppins-regular text-[12px] text-rouge lg:text-small">
{errors.country}
</p>
)}
</div>
<div>
<TextInput
name="subject"
label={useTrans("contact_page.form.placeholders.subject")}
value={data.subject}
onChange={(e) => {
setData("subject", e.target.value);
}}
required
disabled={processing}
error={errors.subject}
/>
{errors.subject && (
<p className="mt-1 font-poppins-regular text-[12px] lg:text-small text-rouge">
{errors.subject}
</p>
)}
</div>
<div>
<TextArea
name="message"
label={useTrans("contact_page.form.placeholders.message")}
value={data.message}
onChange={(e) => {
setData("message", e.target.value);
}}
required
disabled={processing}
error={errors.message}
/>
{errors.message && (
<p className="mt-1 font-poppins-regular text-[12px] lg:text-small text-rouge">
{errors.message}
</p>
)}
</div>
<div>
<FileInput
label={useTrans("contact_page.form.placeholders.attachment")}
value={data.attachment}
onChange={(file) => {
setData("attachment", file);
}}
/>
{errors.attachment && (
<p className="mt-1 font-poppins-regular text-[12px] lg:text-small text-rouge">
{errors.attachment}
</p>
)}
</div>
<div>
<Checkbox
checked={data.acceptsUsage}
onChange={(e) => {
setData("acceptsUsage", e.target.checked);
}}
label={
<>
{useTrans("contact_page.form.consent.text_before")}{" "}
<strong>
{useTrans("contact_page.form.consent.responsible")}
</strong>{" "}
{useTrans("contact_page.form.consent.text_middle")}{" "}
<a
href={route("front.confidentialite")}
className="font-bold underline"
>
{useTrans("contact_page.form.consent.privacy_link")}
</a>
</>
}
disabled={processing}
labelStyle="text-[14px] lg:text-body"
error={errors.acceptsUsage}
/>
{errors.acceptsUsage && (
<p className="mt-1 font-poppins-regular text-[12px] lg:text-small text-rouge">
{errors.acceptsUsage}
</p>
)}
</div>
</RecaptchaForm>
<SuccessModal
isOpen={showSuccess}
onClose={() => {
setShowSuccess(false);
}}
title={useTrans("contact_page.success.title")}
message={useTrans("contact_page.success.message")}
/>
</>
);
}