diff --git a/ui/src/components/form/FormGenerator.tsx b/ui/src/components/form/FormGenerator.tsx index a65e937..49321ea 100644 --- a/ui/src/components/form/FormGenerator.tsx +++ b/ui/src/components/form/FormGenerator.tsx @@ -120,9 +120,23 @@ function transformData(data: any, re: RegExp) { }) } +function formatDraftDate(date: Date) { + const year = date.getFullYear() + const month = String(date.getMonth() + 1).padStart(2, '0') + const day = String(date.getDate()).padStart(2, '0') + const hours = String(date.getHours()).padStart(2, '0') + const minutes = String(date.getMinutes()).padStart(2, '0') + const seconds = String(date.getSeconds()).padStart(2, '0') + const ampm = date.toLocaleString('en-US', { timeZoneName: 'short' }).split(' ')[2] + const timeZone = date.toLocaleString('en-US', { timeZoneName: 'short' }).split(' ')[3] + + return `${year}-${month}-${day} ${hours}:${minutes}:${seconds} ${ampm} ${timeZone}` +} + export function FormGenerator({ formMetadata, root }) { // State and context variables + const [lastDraftUpdate, setLastDraftUpdate] = useState("") const [lastSubmissionUpdate, setLastSubmissionUpdate] = useState(`Submissions-${new Date().toUTCString()}`) const [draftModified, setDraftModified] = useState(false) @@ -339,10 +353,12 @@ export function FormGenerator({ formMetadata, root }) { // sets the interval for saving drafts useEffect(() => { const seconds = 10 // save the drafts every ten seconds (10 * 1000 milliseconds) - const draftSaveInterval = draftModified + const validPatient = !!patientIdentifier.submitter_donor_id && !!patientIdentifier.program_id + const draftSaveInterval = draftModified && validPatient ? setInterval(() => { saveDraft() setDraftModified(false) + setLastDraftUpdate(formatDraftDate(new Date())) }, seconds * 1000) : null @@ -407,6 +423,7 @@ export function FormGenerator({ formMetadata, root }) { onCompleted: () => { setDraftModified(false) updateDraftID(null) + setLastDraftUpdate("") } }) @@ -517,6 +534,7 @@ export function FormGenerator({ formMetadata, root }) { // final render return (