From 10639ca35d3c894fbc492b85fba0bed052737faa Mon Sep 17 00:00:00 2001 From: Maxime Lalo Date: Mon, 4 Dec 2023 15:49:38 +0100 Subject: [PATCH] :sparkles: Modifying cell phone number validation --- .../UserFolder/UserFolderHeader/index.tsx | 24 +++++++++++++++++++ .../CollaboratorInformations/index.tsx | 2 +- .../Folder/AddClientToFolder/index.tsx | 10 ++++++++ .../Layouts/Folder/UpdateClient/index.tsx | 11 +++++++++ .../Components/Layouts/MyAccount/index.tsx | 2 +- .../Layouts/Users/UserInformations/index.tsx | 4 ++-- 6 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/front/Components/DesignSystem/UserFolder/UserFolderHeader/index.tsx b/src/front/Components/DesignSystem/UserFolder/UserFolderHeader/index.tsx index e6060bfe..b0d3b5e8 100644 --- a/src/front/Components/DesignSystem/UserFolder/UserFolderHeader/index.tsx +++ b/src/front/Components/DesignSystem/UserFolder/UserFolderHeader/index.tsx @@ -76,6 +76,30 @@ export default class UserFolderHeader extends React.Component { private formatPhoneNumber(phoneNumber: string): string { if (!phoneNumber) return ""; phoneNumber = phoneNumber.replace(/ /g, ""); + phoneNumber = phoneNumber.replace("+33", "0"); + + if (phoneNumber.length !== 10) { + // split the last 9 digits + const lastNineDigits = phoneNumber.slice(-9); + + // get the country code + const countryCode = phoneNumber.slice(0, -9); + + // isolate the first digit + const firstDigit = lastNineDigits.slice(0, 1); + + // isolate the 8 other ones + const lastEightDigits = lastNineDigits.slice(1); + + // make a space every two digits on the last eights + const output = lastEightDigits.split("").map((char, index) => { + if (index % 2) return char + " "; + return char; + }); + // format the phone number + phoneNumber = countryCode + " " + firstDigit + " " + output.join(""); + return phoneNumber; + } const output = phoneNumber.split("").map((char, index) => { if (index % 2) return char + " "; return char; diff --git a/src/front/Components/Layouts/Collaborators/CollaboratorInformations/index.tsx b/src/front/Components/Layouts/Collaborators/CollaboratorInformations/index.tsx index 639617fd..8cde5107 100644 --- a/src/front/Components/Layouts/Collaborators/CollaboratorInformations/index.tsx +++ b/src/front/Components/Layouts/Collaborators/CollaboratorInformations/index.tsx @@ -163,7 +163,7 @@ export default function CollaboratorInformations(props: IProps) { Numéro de téléphone - {userSelected?.contact?.phone_number} + {userSelected?.contact?.cell_phone_number}
diff --git a/src/front/Components/Layouts/Folder/AddClientToFolder/index.tsx b/src/front/Components/Layouts/Folder/AddClientToFolder/index.tsx index 56c98c0d..5da5c5bc 100644 --- a/src/front/Components/Layouts/Folder/AddClientToFolder/index.tsx +++ b/src/front/Components/Layouts/Folder/AddClientToFolder/index.tsx @@ -229,6 +229,16 @@ class AddClientToFolderClass extends BasePage { if (this.state.selectedOption === "new_customer") { try { + // remove every space from the phone number + values["cell_phone_number"] = values["cell_phone_number"].replace(/\s/g, ""); + if (values["cell_phone_number"] && values["cell_phone_number"].length === 10) { + // get the first digit of the phone number + const firstDigit = values["cell_phone_number"].charAt(0); + // if the first digit is a 0 replace it by +33 + if (firstDigit === "0") { + values["cell_phone_number"] = "+33" + values["cell_phone_number"].substring(1); + } + } const contactToCreate = Contact.hydrate(values); await contactToCreate.validateOrReject?.({ groups: ["createCustomer"], forbidUnknownValues: false }); } catch (validationErrors) { diff --git a/src/front/Components/Layouts/Folder/UpdateClient/index.tsx b/src/front/Components/Layouts/Folder/UpdateClient/index.tsx index b8ba3045..7cc6b833 100644 --- a/src/front/Components/Layouts/Folder/UpdateClient/index.tsx +++ b/src/front/Components/Layouts/Folder/UpdateClient/index.tsx @@ -176,6 +176,17 @@ class UpdateClientClass extends BasePage { [key: string]: string; }, ) { + if (!values["cell_phone_number"]) return; + // remove every space from the phone number + values["cell_phone_number"] = values["cell_phone_number"].replace(/\s/g, ""); + if (values["cell_phone_number"] && values["cell_phone_number"].length === 10) { + // get the first digit of the phone number + const firstDigit = values["cell_phone_number"].charAt(0); + // if the first digit is a 0 replace it by +33 + if (firstDigit === "0") { + values["cell_phone_number"] = "+33" + values["cell_phone_number"].substring(1); + } + } const contact = Contact.hydrate({ first_name: values["first_name"], last_name: values["last_name"], diff --git a/src/front/Components/Layouts/MyAccount/index.tsx b/src/front/Components/Layouts/MyAccount/index.tsx index 4ac8be58..7510f09d 100644 --- a/src/front/Components/Layouts/MyAccount/index.tsx +++ b/src/front/Components/Layouts/MyAccount/index.tsx @@ -61,7 +61,7 @@ export default class MyAccount extends Base { diff --git a/src/front/Components/Layouts/Users/UserInformations/index.tsx b/src/front/Components/Layouts/Users/UserInformations/index.tsx index f93d0fcb..50a71fd3 100644 --- a/src/front/Components/Layouts/Users/UserInformations/index.tsx +++ b/src/front/Components/Layouts/Users/UserInformations/index.tsx @@ -173,7 +173,7 @@ export default function UserInformations(props: IProps) { const liveVote = await LiveVotes.getInstance().post(vote); if (liveVote.appointment.votes?.length === 3) { - if(superAdminModalType === "add") { + if (superAdminModalType === "add") { Toasts.getInstance().open({ title: `Le titre de super-administrateur a été attribué à ${userSelected.contact?.first_name} ${userSelected.contact?.last_name} `, }); @@ -238,7 +238,7 @@ export default function UserInformations(props: IProps) { Numéro de téléphone - {userSelected?.contact?.phone_number} + {userSelected?.contact?.cell_phone_number}