🐛 Error handling on creating a customer
This commit is contained in:
parent
4dcc9d1735
commit
bd5364870b
@ -10,13 +10,14 @@ import BackArrow from "@Front/Components/Elements/BackArrow";
|
|||||||
import DefaultNotaryDashboard from "@Front/Components/LayoutTemplates/DefaultNotaryDashboard";
|
import DefaultNotaryDashboard from "@Front/Components/LayoutTemplates/DefaultNotaryDashboard";
|
||||||
import Module from "@Front/Config/Module";
|
import Module from "@Front/Config/Module";
|
||||||
import { ECivility } from "le-coffre-resources/dist/Customer/Contact";
|
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 Link from "next/link";
|
||||||
import { NextRouter, useRouter } from "next/router";
|
import { NextRouter, useRouter } from "next/router";
|
||||||
|
|
||||||
import BasePage from "../../Base";
|
import BasePage from "../../Base";
|
||||||
import classes from "./classes.module.scss";
|
import classes from "./classes.module.scss";
|
||||||
import TextField from "@Front/Components/DesignSystem/Form/TextField";
|
import TextField from "@Front/Components/DesignSystem/Form/TextField";
|
||||||
|
import { ValidationError } from "class-validator";
|
||||||
|
|
||||||
enum ESelectedOption {
|
enum ESelectedOption {
|
||||||
EXISTING_CUSTOMER = "existing_customer",
|
EXISTING_CUSTOMER = "existing_customer",
|
||||||
@ -29,6 +30,7 @@ type IState = {
|
|||||||
selectedCustomers: readonly IOption[];
|
selectedCustomers: readonly IOption[];
|
||||||
existingCustomers: IOption[];
|
existingCustomers: IOption[];
|
||||||
isLoaded: boolean;
|
isLoaded: boolean;
|
||||||
|
validationError: ValidationError[];
|
||||||
};
|
};
|
||||||
|
|
||||||
type IPropsClass = IProps & {
|
type IPropsClass = IProps & {
|
||||||
@ -44,6 +46,7 @@ class AddClientToFolderClass extends BasePage<IPropsClass, IState> {
|
|||||||
selectedCustomers: [],
|
selectedCustomers: [],
|
||||||
existingCustomers: [],
|
existingCustomers: [],
|
||||||
isLoaded: false,
|
isLoaded: false,
|
||||||
|
validationError: [],
|
||||||
};
|
};
|
||||||
this.onExistingClientSelected = this.onExistingClientSelected.bind(this);
|
this.onExistingClientSelected = this.onExistingClientSelected.bind(this);
|
||||||
this.onNewClientSelected = this.onNewClientSelected.bind(this);
|
this.onNewClientSelected = this.onNewClientSelected.bind(this);
|
||||||
@ -105,10 +108,28 @@ class AddClientToFolderClass extends BasePage<IPropsClass, IState> {
|
|||||||
|
|
||||||
{this.state.selectedOption === "new_customer" && (
|
{this.state.selectedOption === "new_customer" && (
|
||||||
<div className={classes["new-client"]}>
|
<div className={classes["new-client"]}>
|
||||||
<TextField name="last_name" placeholder="Nom" />
|
<TextField
|
||||||
<TextField name="first_name" placeholder="Prénom" />
|
name="last_name"
|
||||||
<TextField name="email" placeholder="E-mail" />
|
placeholder="Nom"
|
||||||
<TextField name="cell_phone_number" placeholder="Numéro de téléphone" />
|
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"]}>
|
<div className={classes["button-container"]}>
|
||||||
<Link href={backwardPath} className={classes["cancel-button"]}>
|
<Link href={backwardPath} className={classes["cancel-button"]}>
|
||||||
<Button variant={EButtonVariant.GHOST}>Annuler</Button>
|
<Button variant={EButtonVariant.GHOST}>Annuler</Button>
|
||||||
@ -207,11 +228,29 @@ class AddClientToFolderClass extends BasePage<IPropsClass, IState> {
|
|||||||
}) as Partial<Customer>[];
|
}) as Partial<Customer>[];
|
||||||
|
|
||||||
if (this.state.selectedOption === "new_customer") {
|
if (this.state.selectedOption === "new_customer") {
|
||||||
const customer: Customer = await Customers.getInstance().post({
|
try {
|
||||||
contact: values,
|
const contactToCreate = Contact.hydrate<Customer>(values);
|
||||||
});
|
await contactToCreate.validateOrReject?.({ groups: ["createCustomer"], forbidUnknownValues: false });
|
||||||
if (!customer.uid) return;
|
} catch (validationErrors) {
|
||||||
customersToLink?.push({ uid: customer.uid } as Partial<Customer>);
|
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) {
|
if (customersToLink) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user