From f1d8ed4f08e09fa546020b92628213ef7f8fc22b Mon Sep 17 00:00:00 2001 From: AdityaJ2305 Date: Fri, 15 Nov 2024 23:57:59 +0530 Subject: [PATCH 1/4] Fix: Year Of Birth Field Validation Condition in Patient transfer form --- .../Facility/TransferPatientDialog.tsx | 43 +++++++++++++------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/src/components/Facility/TransferPatientDialog.tsx b/src/components/Facility/TransferPatientDialog.tsx index 530bc513f0d..40a0f76a200 100644 --- a/src/components/Facility/TransferPatientDialog.tsx +++ b/src/components/Facility/TransferPatientDialog.tsx @@ -75,23 +75,38 @@ const TransferPatientDialog = (props: Props) => { const maxYear = new Date().getFullYear(); const handleChange = (e: FieldChangeEvent) => { - if ( - e.name === "year_of_birth" && - parseInt((e.value as string) || "0") > maxYear - ) { + dispatch({ + type: "set_form", + form: { ...state.form, [e.name]: e.value }, + }); + }; + + const handleOnBlur = (e: any) => { + if (state.form.year_of_birth > maxYear) { dispatch({ type: "set_error", errors: { ...state.errors, - [e.name]: `Cannot be greater than ${maxYear}`, + [e.target.name]: `Cannot be greater than ${maxYear}`, + }, + }); + } else if (state.form.year_of_birth < 1900) { + dispatch({ + type: "set_error", + errors: { + ...state.errors, + [e.target.name]: `Cannot be smaller than 1900`, + }, + }); + } else { + dispatch({ + type: "set_error", + errors: { + ...state.errors, + [e.target.name]: "", }, }); - return; } - dispatch({ - type: "set_form", - form: { ...state.form, [e.name]: e.value }, - }); }; const validateForm = () => { @@ -115,6 +130,11 @@ const TransferPatientDialog = (props: Props) => { errors[field] = `Cannot be greater than ${maxYear}`; invalidForm = true; } + + if (parseInt(state.form[field] || "0") < 1900) { + errors[field] = `Cannot be smaller than 1900`; + invalidForm = true; + } return; default: return; @@ -193,9 +213,8 @@ const TransferPatientDialog = (props: Props) => { label="Year of birth" labelClassName="text-sm" value={state.form.year_of_birth} - min="1900" - max={maxYear} onChange={handleChange} + onBlur={handleOnBlur} placeholder="Enter year of birth" error={state.errors.year_of_birth} /> From fc7267968d2693161496b87858009289f738c45c Mon Sep 17 00:00:00 2001 From: AdityaJ2305 Date: Sat, 16 Nov 2024 00:14:12 +0530 Subject: [PATCH 2/4] changes asked by codeRbbit --- .../Facility/TransferPatientDialog.tsx | 40 +++++++------------ 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/src/components/Facility/TransferPatientDialog.tsx b/src/components/Facility/TransferPatientDialog.tsx index 40a0f76a200..27470a585b4 100644 --- a/src/components/Facility/TransferPatientDialog.tsx +++ b/src/components/Facility/TransferPatientDialog.tsx @@ -81,32 +81,22 @@ const TransferPatientDialog = (props: Props) => { }); }; - const handleOnBlur = (e: any) => { - if (state.form.year_of_birth > maxYear) { - dispatch({ - type: "set_error", - errors: { - ...state.errors, - [e.target.name]: `Cannot be greater than ${maxYear}`, - }, - }); - } else if (state.form.year_of_birth < 1900) { - dispatch({ - type: "set_error", - errors: { - ...state.errors, - [e.target.name]: `Cannot be smaller than 1900`, - }, - }); - } else { - dispatch({ - type: "set_error", - errors: { - ...state.errors, - [e.target.name]: "", - }, - }); + const handleOnBlur = (e: React.FocusEvent) => { + const yearValue = Number(state.form.year_of_birth); + if (!state.form.year_of_birth) return; + let errorMessage = ""; + if (yearValue > maxYear) { + errorMessage = `Cannot be greater than ${maxYear}`; + } else if (yearValue < 1900) { + errorMessage = `Cannot be smaller than 1900`; } + dispatch({ + type: "set_error", + errors: { + ...state.errors, + [e.target.name]: errorMessage, + }, + }); }; const validateForm = () => { From b7984811afab36516bdbbd73c977600fe43ca837 Mon Sep 17 00:00:00 2001 From: AdityaJ2305 Date: Mon, 18 Nov 2024 19:34:37 +0530 Subject: [PATCH 3/4] Fixed limit to 4 --- src/components/Facility/TransferPatientDialog.tsx | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/components/Facility/TransferPatientDialog.tsx b/src/components/Facility/TransferPatientDialog.tsx index 27470a585b4..32f42283544 100644 --- a/src/components/Facility/TransferPatientDialog.tsx +++ b/src/components/Facility/TransferPatientDialog.tsx @@ -75,10 +75,14 @@ const TransferPatientDialog = (props: Props) => { const maxYear = new Date().getFullYear(); const handleChange = (e: FieldChangeEvent) => { - dispatch({ - type: "set_form", - form: { ...state.form, [e.name]: e.value }, - }); + const value = String(e.value); + + if (value.length <= 4) { + dispatch({ + type: "set_form", + form: { ...state.form, [e.name]: e.value }, + }); + } }; const handleOnBlur = (e: React.FocusEvent) => { From 54f6a31cb6258a9cb948d85ee22e33577be97941 Mon Sep 17 00:00:00 2001 From: AdityaJ2305 Date: Mon, 18 Nov 2024 20:51:42 +0530 Subject: [PATCH 4/4] handleChange function fixed --- src/components/Facility/TransferPatientDialog.tsx | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/components/Facility/TransferPatientDialog.tsx b/src/components/Facility/TransferPatientDialog.tsx index 32f42283544..153ea17fa92 100644 --- a/src/components/Facility/TransferPatientDialog.tsx +++ b/src/components/Facility/TransferPatientDialog.tsx @@ -77,7 +77,14 @@ const TransferPatientDialog = (props: Props) => { const handleChange = (e: FieldChangeEvent) => { const value = String(e.value); - if (value.length <= 4) { + if (e.name === "year_of_birth") { + if (value.length <= 4) { + dispatch({ + type: "set_form", + form: { ...state.form, [e.name]: e.value }, + }); + } + } else { dispatch({ type: "set_form", form: { ...state.form, [e.name]: e.value },