import backgroundImage from "@Assets/images/background_refonte.svg"; import Customers from "@Front/Api/LeCoffreApi/Notary/Customers/Customers"; import Button, { EButtonstyletype, EButtonVariant } from "@Front/Components/DesignSystem/Button"; import Form, { IBaseField } from "@Front/Components/DesignSystem/Form"; import TextField from "@Front/Components/DesignSystem/Form/TextField"; import Modal from "@Front/Components/DesignSystem/Modal"; import Typography, { ETypo } from "@Front/Components/DesignSystem/Typography"; import BackArrow from "@Front/Components/Elements/BackArrow"; import DefaultDoubleSidePage from "@Front/Components/LayoutTemplates/DefaultDoubleSidePage"; import Module from "@Front/Config/Module"; import useOpenable from "@Front/Hooks/useOpenable"; import { ValidationError } from "class-validator"; import { Address } from "le-coffre-resources/dist/Customer"; import { Contact, Customer } from "le-coffre-resources/dist/Notary"; import Link from "next/link"; import { useRouter } from "next/router"; import { useCallback, useEffect, useState } from "react"; import classes from "./classes.module.scss"; export default function UpdateClient() { const router = useRouter(); const { folderUid, customerUid } = router.query; const [doesInputHasChanged, setDoesInputHasChanged] = useState(false); const [customer, setCustomer] = useState(null); const [validationError, setValidationError] = useState([]); const { isOpen, open, close } = useOpenable(); const backwardPath = Module.getInstance() .get() .modules.pages.Folder.pages.FolderInformation.props.path.replace("[folderUid]", folderUid as string); useEffect(() => { const fetchCustomer = async () => { try { const customerData = await Customers.getInstance().getByUid(customerUid as string, { contact: { include: { address: true, }, }, }); if (customerData) { setCustomer(customerData); } } catch (error) { console.error("Failed to fetch customer", error); } }; fetchCustomer(); }, [customerUid]); const onFormSubmit = useCallback( async (e: React.FormEvent | null, values: { [key: string]: string }) => { if (!values["cell_phone_number"]) return; let phoneNumber = values["cell_phone_number"].replace(/\s/g, "").replace(/\./g, ""); if (phoneNumber.length === 10 && phoneNumber.startsWith("0")) { phoneNumber = "+33" + phoneNumber.substring(1); } const contact = Contact.hydrate({ first_name: values["first_name"], last_name: values["last_name"], email: values["email"], cell_phone_number: phoneNumber, birthdate: values["birthdate"] === "" ? null : new Date(values["birthdate"]!), civility: "-", address: values["address"] ? Address.hydrate
({ address: values["address"], city: "-", zip_code: 0 }) : undefined, }); try { await contact.validateOrReject?.({ groups: ["createCustomer"], forbidUnknownValues: false }); await Customers.getInstance().put(customerUid as string, { contact }); router.push(backwardPath); } catch (validationErrors) { if (Array.isArray(validationErrors)) { setValidationError(validationErrors as ValidationError[]); } } }, [backwardPath, customerUid, router], ); const leavePage = useCallback(() => { router.push(backwardPath); }, [backwardPath, router]); const onFieldChange = useCallback((name: string, field: IBaseField) => { if (field.props.value !== field.props.defaultValue) { setDoesInputHasChanged(true); } else { setDoesInputHasChanged(false); } }, []); return (
Modifier les informations du client
error.property === "first_name")} /> error.property === "last_name")} /> error.property === "email")} /> error.property === "cell_phone_number")} /> error.property === "birthdate")} />
{!doesInputHasChanged ? ( ) : ( )}
Si vous quittez, toutes les modifications que vous avez effectuées ne seront pas enregistrées.
); }