-
Notifications
You must be signed in to change notification settings - Fork 251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix/O3-3983: Allow Same-Day Program Enrollment and Completion #2039
Draft
jwnasambu
wants to merge
13
commits into
openmrs:main
Choose a base branch
from
jwnasambu:fix/O3-3983
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+79
−34
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
89af11b
fix/O3-3983: Allow Same-Day Program Enrollment and Completion
4b4ef0a
Merge branch 'main' of https://github.com/openmrs/openmrs-esm-patient…
c6ed293
Add time component to enrollmentDate default value
2d78f5a
Add configurable option to enable time selection using a timepicker
4c2ce39
Merge branch 'main' into fix/O3-3983
jwnasambu 8424b9d
Update configSchema with allowCompletionTime
496687b
Merge branch 'main' of https://github.com/openmrs/openmrs-esm-patient…
8c4a5d4
Merge branch 'main' of https://github.com/openmrs/openmrs-esm-patient…
2c889b6
Add timePicker
a619131
Merge branch 'main' of https://github.com/openmrs/openmrs-esm-patient…
8e8a158
Code refactor
2c73704
Code refactor
aea4cce
Code refactor
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import React, { useCallback, useEffect, useMemo } from 'react'; | ||
import React, { useCallback, useEffect, useMemo, useState } from 'react'; | ||
import classNames from 'classnames'; | ||
import { type TFunction, useTranslation } from 'react-i18next'; | ||
import dayjs from 'dayjs'; | ||
|
@@ -92,6 +92,7 @@ const ProgramsForm: React.FC<ProgramsFormProps> = ({ | |
const { | ||
control, | ||
handleSubmit, | ||
setValue, | ||
watch, | ||
formState: { errors, isDirty, isSubmitting }, | ||
} = useForm<ProgramsFormData>({ | ||
|
@@ -192,46 +193,85 @@ const ProgramsForm: React.FC<ProgramsFormProps> = ({ | |
/> | ||
); | ||
|
||
const [selectedDate, setSelectedDate] = useState<Date | null>( | ||
currentEnrollment?.dateEnrolled ? parseDate(currentEnrollment.dateEnrolled) : new Date(), | ||
); | ||
const [selectedTime, setSelectedTime] = useState<string>( | ||
new Date().toLocaleTimeString('en-GB', { hour: '2-digit', minute: '2-digit' }), | ||
); | ||
|
||
const handleDateChange = ([date]: Date[]) => { | ||
setSelectedDate(date); | ||
updateDateTime(date, selectedTime); | ||
}; | ||
|
||
const handleTimeChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
const time = event.target.value; | ||
setSelectedTime(time); | ||
updateDateTime(selectedDate, time); | ||
}; | ||
|
||
const updateDateTime = (date: Date | null, time: string) => { | ||
if (date) { | ||
const [hours, minutes] = time.split(':').map(Number); | ||
const updatedDateTime = new Date(date); | ||
updatedDateTime.setHours(hours); | ||
updatedDateTime.setMinutes(minutes); | ||
setValue('enrollmentDate', updatedDateTime); | ||
} | ||
}; | ||
|
||
const enrollmentDate = ( | ||
<Controller | ||
name="enrollmentDate" | ||
control={control} | ||
render={({ field: { onChange, value } }) => ( | ||
<DatePicker | ||
aria-label="enrollment date" | ||
id="enrollmentDate" | ||
datePickerType="single" | ||
dateFormat="d/m/Y" | ||
maxDate={new Date().toISOString()} | ||
placeholder="dd/mm/yyyy" | ||
onChange={([date]) => onChange(date)} | ||
value={value} | ||
> | ||
<DatePickerInput id="enrollmentDateInput" labelText={t('dateEnrolled', 'Date enrolled')} /> | ||
</DatePicker> | ||
)} | ||
/> | ||
<DatePicker | ||
aria-label="enrollment date" | ||
id="enrollmentDate" | ||
datePickerType="single" | ||
dateFormat="d/m/Y" | ||
maxDate={new Date().toISOString()} | ||
placeholder="dd/mm/yyyy" | ||
onChange={handleDateChange} | ||
value={selectedDate} | ||
> | ||
<DatePickerInput id="enrollmentDateInput" labelText={t('dateEnrolled', 'Date enrolled')} /> | ||
</DatePicker> | ||
); | ||
|
||
const completionDate = ( | ||
<Controller | ||
name="completionDate" | ||
control={control} | ||
render={({ field: { onChange, value } }) => ( | ||
<DatePicker | ||
aria-label="completion date" | ||
id="completionDate" | ||
datePickerType="single" | ||
dateFormat="d/m/Y" | ||
minDate={new Date(watch('enrollmentDate')).toISOString()} | ||
maxDate={new Date().toISOString()} | ||
placeholder="dd/mm/yyyy" | ||
onChange={([date]) => onChange(date)} | ||
value={value} | ||
> | ||
<DatePickerInput id="completionDateInput" labelText={t('dateCompleted', 'Date completed')} /> | ||
</DatePicker> | ||
)} | ||
render={({ field: { onChange, value } }) => { | ||
const handleChange = ([date]) => { | ||
if (!date) return; | ||
|
||
const selectedDate = new Date(date); | ||
const today = new Date(); | ||
today.setHours(0, 0, 0, 0); | ||
|
||
if (selectedDate.toDateString() === today.toDateString()) { | ||
onChange(new Date()); | ||
} else { | ||
selectedDate.setUTCHours(23, 59, 59, 999); | ||
onChange(selectedDate); | ||
Comment on lines
+243
to
+254
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. handleChange should be moved outside of render to prevent it from being recreated on every render. |
||
} | ||
}; | ||
|
||
return ( | ||
<DatePicker | ||
aria-label="completion date" | ||
id="completionDate" | ||
datePickerType="single" | ||
dateFormat="d/m/Y" | ||
minDate={new Date(watch('enrollmentDate')).toISOString()} | ||
maxDate={new Date().toISOString()} | ||
placeholder="dd/mm/yyyy" | ||
onChange={handleChange} | ||
value={value} | ||
> | ||
<DatePickerInput id="completionDateInput" labelText={t('dateCompleted', 'Date completed')} /> | ||
</DatePicker> | ||
); | ||
}} | ||
/> | ||
); | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.