Skip to content

Commit

Permalink
Fix: Year Of Birth Field Validation Condition in Patient transfer form (
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaJ2305 authored Nov 20, 2024
1 parent 41ed5be commit b076d4f
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions src/components/Facility/TransferPatientDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,38 @@ const TransferPatientDialog = (props: Props) => {
const maxYear = new Date().getFullYear();

const handleChange = (e: FieldChangeEvent<unknown>) => {
if (
e.name === "year_of_birth" &&
parseInt((e.value as string) || "0") > maxYear
) {
const value = String(e.value);

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_error",
errors: {
...state.errors,
[e.name]: `Cannot be greater than ${maxYear}`,
},
type: "set_form",
form: { ...state.form, [e.name]: e.value },
});
return;
}
};

const handleOnBlur = (e: React.FocusEvent<HTMLInputElement>) => {
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_form",
form: { ...state.form, [e.name]: e.value },
type: "set_error",
errors: {
...state.errors,
[e.target.name]: errorMessage,
},
});
};

Expand All @@ -115,6 +131,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;
Expand Down Expand Up @@ -193,9 +214,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}
/>
Expand Down

0 comments on commit b076d4f

Please sign in to comment.