🐛 Error handling on creating a customer

This commit is contained in:
Maxime Lalo 2023-10-09 15:49:45 +02:00
parent 4dcc9d1735
commit bd5364870b

View File

@ -10,13 +10,14 @@ import BackArrow from "@Front/Components/Elements/BackArrow";
import DefaultNotaryDashboard from "@Front/Components/LayoutTemplates/DefaultNotaryDashboard";
import Module from "@Front/Config/Module";
import { ECivility } from "le-coffre-resources/dist/Customer/Contact";
import { Customer, OfficeFolder } from "le-coffre-resources/dist/Notary";
import { Contact, Customer, OfficeFolder } from "le-coffre-resources/dist/Notary";
import Link from "next/link";
import { NextRouter, useRouter } from "next/router";
import BasePage from "../../Base";
import classes from "./classes.module.scss";
import TextField from "@Front/Components/DesignSystem/Form/TextField";
import { ValidationError } from "class-validator";
enum ESelectedOption {
EXISTING_CUSTOMER = "existing_customer",
@ -29,6 +30,7 @@ type IState = {
selectedCustomers: readonly IOption[];
existingCustomers: IOption[];
isLoaded: boolean;
validationError: ValidationError[];
};
type IPropsClass = IProps & {
@ -44,6 +46,7 @@ class AddClientToFolderClass extends BasePage<IPropsClass, IState> {
selectedCustomers: [],
existingCustomers: [],
isLoaded: false,
validationError: [],
};
this.onExistingClientSelected = this.onExistingClientSelected.bind(this);
this.onNewClientSelected = this.onNewClientSelected.bind(this);
@ -105,10 +108,28 @@ class AddClientToFolderClass extends BasePage<IPropsClass, IState> {
{this.state.selectedOption === "new_customer" && (
<div className={classes["new-client"]}>
<TextField name="last_name" placeholder="Nom" />
<TextField name="first_name" placeholder="Prénom" />
<TextField name="email" placeholder="E-mail" />
<TextField name="cell_phone_number" placeholder="Numéro de téléphone" />
<TextField
name="last_name"
placeholder="Nom"
validationError={this.state.validationError.find((error) => error.property === "last_name")}
/>
<TextField
name="first_name"
placeholder="Prénom"
validationError={this.state.validationError.find((error) => error.property === "first_name")}
/>
<TextField
name="email"
placeholder="E-mail"
validationError={this.state.validationError.find((error) => error.property === "email")}
/>
<TextField
name="cell_phone_number"
placeholder="Numéro de téléphone"
validationError={this.state.validationError.find(
(error) => error.property === "cell_phone_number",
)}
/>
<div className={classes["button-container"]}>
<Link href={backwardPath} className={classes["cancel-button"]}>
<Button variant={EButtonVariant.GHOST}>Annuler</Button>
@ -207,11 +228,29 @@ class AddClientToFolderClass extends BasePage<IPropsClass, IState> {
}) as Partial<Customer>[];
if (this.state.selectedOption === "new_customer") {
const customer: Customer = await Customers.getInstance().post({
contact: values,
});
if (!customer.uid) return;
customersToLink?.push({ uid: customer.uid } as Partial<Customer>);
try {
const contactToCreate = Contact.hydrate<Customer>(values);
await contactToCreate.validateOrReject?.({ groups: ["createCustomer"], forbidUnknownValues: false });
} catch (validationErrors) {
console.log(validationErrors);
this.setState({
validationError: validationErrors as ValidationError[],
});
return;
}
try {
const customer: Customer = await Customers.getInstance().post({
contact: values,
});
if (!customer.uid) return;
customersToLink?.push({ uid: customer.uid } as Partial<Customer>);
} catch (backError) {
if (!Array.isArray(backError)) return;
this.setState({
validationError: backError as ValidationError[],
});
}
}
if (customersToLink) {