(fix) O3-5657: Various fixes to the procedures form#3297
Conversation
|
@denniskigen @NethmiRodrigo Could you please review this PR? |
|
On
Could we add it here? estimatedStartDate?: string | null; |
|
On
Example in Could we build the comparison date in local time? const [year, month] = data.estimatedStartDate.split('-').map(Number);
const start = new Date(year, Number.isFinite(month) ? month - 1 : 0, 1);
return data.endDateTime >= start; |
| setValue('estimatedStartDate', ''); | ||
| } | ||
| }, [isStartDateKnown, estimatedYear, estimatedMonth, setValue]); | ||
|
|
There was a problem hiding this comment.
Nice work on the toggle clearing logic, @dilankavishka. While testing locally I hit a small validation issue that I think originates here. Wanted to share what I found and a fix that worked end to end.
Repro:
- Toggle "Is start date known?" to No. "Start date is required" appears immediately, before the user has touched the year combo.
- Pick 2023 for Year. The error stays visible.
- Click Save & close. Only then does the error clear.
There are two things going on. First, the error doesn't clear after picking a year because setValue('estimatedStartDate', '2023', { shouldValidate: true }) runs validation, but shouldValidate only updates errors on the path it was called with. The refine's error lives at path: ['startDateTime'], so the stale error there is left untouched. We need an explicit trigger('startDateTime') to refresh that path. Second, the error fires immediately on toggle because setValue('startDateTime', null, { shouldValidate: true }) in the toggle handler revalidates on the spot, before the user has had a chance to fill the year.
Just those two changes still leave the error appearing on toggle, because this useEffect itself runs whenever isStartDateKnown changes and re-triggers validation. The RHF idiom for progressive validation is to gate the manual trigger on isSubmitted, so we only push back on the user after they've actually tried to submit:
const {
formState: { errors, isSubmitted },
setValue,
trigger,
...
} = useFormContext<ProceduresFormSchema>();
useEffect(() => {
const value =
!isStartDateKnown && estimatedYear
? estimatedMonth ? `${estimatedYear}-${estimatedMonth}` : estimatedYear
: '';
setValue('estimatedStartDate', value);
if (isSubmitted) {
trigger('startDateTime');
}
}, [isStartDateKnown, estimatedYear, estimatedMonth, isSubmitted, setValue, trigger]);And in the toggle handler at line 260:
if (!isKnown) {
setValue('startDateTime', null);
}I verified locally that this gives:
- Toggle pre-submit: no premature error.
- Submit with year empty: error appears.
- Pick year post-submit: error clears.
- Clear year post-submit: error reappears.
Could we also add a test for the "clears after picking a year" path? That's the actual regression the fix addresses, and the current test at procedures-form.workspace.test.tsx:464 only checks the error after submit:
// After the existing submit assertion:
await user.click(screen.getByRole('combobox', { name: /year/i }));
await user.click(screen.getByRole('option', { name: '2023' }));
expect(screen.queryByText(/start date is required/i)).not.toBeInTheDocument();Side note while we're here: the original three-branch form collapses to two since the if (isStartDateKnown) and final else both write ''. The snippet above already incorporates that.
There was a problem hiding this comment.
Done.
Screen.Recording.2026-05-12.at.10.16.27.mov
@denniskigen To keep the consistency and the better understanding shall we rename the title to so as like this, the length of procedure is 2 hours. |
Would "Procedure duration" be a more accurate legend label here? @VeronicaMuthee |
All other keys in en.json are camelCase. The key with a space was an outlier and harder to grep for and reference consistently.
denniskigen
left a comment
There was a problem hiding this comment.
Thanks for the careful iteration on this one, @dilankavishka. Tracking down the stale startDateTime after the toggle and wiring up the validation re-trigger so the error clears as soon as the user picks a year both took some thought, and the new tests lock both behaviours in nicely. Good catch on the procedure-type null guard too, and tightening the RawProcedure type closes a contract gap that had been hiding in plain sight.
Pushed one small follow-up to switch the duration translation key to camelCase for consistency with the rest of en.json. Approving.
Thank you Dennis! |
bd88d2b
into
openmrs:feat/procedure-history



Requirements
Summary
Selects withComboBoxes and fix horizontal alignment of the duration fieldsstartDateTimewas left in form state and silently passed validation)selectedItemestimatedStartDateto theRawProceduretype so the payload contract matches what is actually sent (and the backend persists)startDateTimevalidation after submit so the "Start date is required" error clears as soon as the user picks a yearScreenshots
Before:

After:

Other
Related Issue
https://issues.openmrs.org/browse/O3-5657