From 672e68907a229d5c1e8ddf33d315b42d152dff93 Mon Sep 17 00:00:00 2001 From: Anand Suthar Date: Mon, 17 Jun 2024 23:55:17 +0530 Subject: [PATCH] Contact field updating with onChange and toast mesaages on error --- .../components/ProfileSettings/index.tsx | 46 +++++++++++++++---- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/app/(pages)/patient/components/ProfileSettings/index.tsx b/app/(pages)/patient/components/ProfileSettings/index.tsx index 844f7bee..c6df6c0a 100644 --- a/app/(pages)/patient/components/ProfileSettings/index.tsx +++ b/app/(pages)/patient/components/ProfileSettings/index.tsx @@ -35,6 +35,7 @@ export default function ProfileSettings({ patient }: { patient: Patient }) { const [profilePicture, setProfilePicture] = useState(patient.profile); const [dob, setDob] = useState(parseDate(patient.dob)); const [contact, setContact] = useState(patient.contact); + const [contactError, setContactError] = useState(null || String); const [gender, setGender] = useState(patient.gender); const [address, setAddress] = useState({ address_line_1: patient.address.address_line_1, @@ -53,6 +54,7 @@ export default function ProfileSettings({ patient }: { patient: Patient }) { const usernameRef = useRef(null); const emailRef = useRef(null); const passwordRef = useRef(null); + const contactRef = useRef(null); function handleFirstNameChange(e: ChangeEvent) { const firstNameRegex = /^[a-zA-Z'-]+$/; @@ -134,6 +136,30 @@ export default function ProfileSettings({ patient }: { patient: Patient }) { setPassword(e.target.value); } + const isDateInvalid = (): boolean => { + const birthDate = dob; + const today = new Date(); + let age = today.getFullYear() - birthDate.year; + const m = today.getMonth() - birthDate.month; + if (m < 0 || (m === 0 && today.getDate() < birthDate.day)) { + age--; + } + if (age < 18 || age > 100) return true; + return false; + }; + + const handleGenderChange = (e: React.ChangeEvent) => { + setGender(e.target.value); + }; + + const handleContactChange = (e: React.ChangeEvent) => { + const regex = /^\d{10}$/; + const isContactValid = regex.test(e.target.value); + + setContactError(isContactValid ? "" : "Phone must be 10"); + setContact(e.target.value); + }; + useEffect(() => { setUpdateDisabled(isUpdateDisabled()); }, [ @@ -203,18 +229,14 @@ export default function ProfileSettings({ patient }: { patient: Patient }) { }); } } - }; - const isDateInvalid = (): boolean => { - const birthDate = dob; - const today = new Date(); - let age = today.getFullYear() - birthDate.year; - const m = today.getMonth() - birthDate.month; - if (m < 0 || (m === 0 && today.getDate() < birthDate.day)) { - age--; + if (inputRef.current?.name === "contact") { + if (contactError) { + toast.error(contactError, { + position: "bottom-center", + }); + } } - if (age < 18 || age > 100) return true; - return false; }; async function handleFormSubmit(e: React.FormEvent) { @@ -426,12 +448,16 @@ export default function ProfileSettings({ patient }: { patient: Patient }) { label="Phone" value={contact} className="max-w-xs" + onChange={handleContactChange} + ref={contactRef} + onBlur={() => showToast(contactRef)} />