From 23fefc296fedf257ea063174008befa616f097c0 Mon Sep 17 00:00:00 2001 From: Poh Anson <50758680+PohAnson@users.noreply.github.com> Date: Thu, 12 Dec 2024 15:11:19 +0800 Subject: [PATCH 1/2] Factorise out the popup when recent visit. created new function `checkRecency` --- pages/registration/index.js | 46 +++++++++----------------------- utils/check.js | 52 +++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 34 deletions(-) create mode 100644 utils/check.js diff --git a/pages/registration/index.js b/pages/registration/index.js index bc33e7d..cead781 100644 --- a/pages/registration/index.js +++ b/pages/registration/index.js @@ -17,6 +17,7 @@ import CustomModal from '@/components/CustomModal'; import { PageTitle } from '@/components/TextComponents'; import { REGISTRATION_FORM_FIELDS } from '@/utils/constants'; import { VillageContext } from '@/context/VillageContext'; +import { checkRecency } from '@/utils/check'; export const submitNewVisit = async patient => { try { @@ -207,41 +208,18 @@ const Registration = () => { }); const handleNewVisit = useWithLoading(async patient => { - try { - const { data: patient_visits } = await axiosInstance.get( - `/visits?patient=${patient.pk}` - ); - if (patient_visits.length > 0) { - const mostRecentVisit = patient_visits.reduce((latest, visit) => { - return new Date(visit.date) > new Date(latest.date) ? visit : latest; - }, patient_visits[0]); - - const currentDate = new Date(); - const mostRecentVisitDate = new Date(mostRecentVisit.date); - const timeDiffInMin = - Math.abs(currentDate.getTime() - mostRecentVisitDate.getTime()) / - (1000 * 60); - - if (timeDiffInMin < 60) { - const isNotDuplicateVisit = confirm( - `It's only been ${Math.floor(timeDiffInMin)} min since a visit for this patient was created. - Are you sure you want to create a new visit? - (Note: duplicate visits will cause unnecessary complications) - Press CANCEL to NOT CREATE a new visit. - Press OK to CREATE a new visit.` - ); - - if (!isNotDuplicateVisit) { - return; - } - } - } - } catch (error) { - toast.error(`Error fetching patient visits: ${error.message}`); - console.error('Error fetching patient vists:', error); + const isNotRecent = await checkRecency( + patient.pk, + 60, + true, + `Are you sure you want to create a new visit? + (Note: duplicate visits will cause unnecessary complications) + Press CANCEL to NOT CREATE a new visit. + Press OK to CREATE a new visit.` + ); + if (isNotRecent) { + submitNewVisit(patient); } - - submitNewVisit(patient); }); return ( diff --git a/utils/check.js b/utils/check.js new file mode 100644 index 0000000..0b21c23 --- /dev/null +++ b/utils/check.js @@ -0,0 +1,52 @@ +import toast from 'react-hot-toast'; +import axiosInstance from '@/pages/api/_axiosInstance'; +/** + * + * @param {Number} patientPk patient primary key to check + * @param {Number} timeDiffCheck how much time difference between visit and current time in minute + * @param {boolean} confirmOnRecent whether to confirm on recent; true: current time diff is less than timeDiffCheck; + * @param {string} confirmMessage the message to confirm + * @returns {Promise} if it is not recent. true: proceed OR error OR no visit; false: is recent; + */ +export async function checkRecency( + patientPk, + timeDiffCheck, + confirmOnRecent, + confirmMessage = '' +) { + try { + const { data: patient_visits } = await axiosInstance.get( + `/visits?patient=${patientPk}` + ); + if (patient_visits.length <= 0) { + return true; + } else { + const mostRecentVisit = patient_visits.reduce((latest, visit) => { + return new Date(visit.date) > new Date(latest.date) ? visit : latest; + }, patient_visits[0]); + + const currentDate = new Date(); + const mostRecentVisitDate = new Date(mostRecentVisit.date); + const timeDiffInMin = + Math.abs(currentDate.getTime() - mostRecentVisitDate.getTime()) / + (1000 * 60); + + if ( + confirmOnRecent + ? timeDiffInMin < timeDiffCheck + : timeDiffInMin > timeDiffCheck + ) { + return confirm( + `It's been ${timeDiffInMin < 1440 ? `${Math.floor(timeDiffInMin)} min` : 'days'} since a visit for this patient was created. + ${confirmMessage} + ` + ); + } + } + } catch (error) { + toast.error(`Error fetching patient visits: ${error.message}`); + console.error('Error fetching patient vists:', error); + return true; + } + return true; +} From 2aa99f570f5cb88a13d25ed049eb8c74a74690bc Mon Sep 17 00:00:00 2001 From: Poh Anson <50758680+PohAnson@users.noreply.github.com> Date: Thu, 12 Dec 2024 15:12:56 +0800 Subject: [PATCH 2/2] Alert when editing vitals of old visits. --- pages/records/patient-vitals/index.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pages/records/patient-vitals/index.js b/pages/records/patient-vitals/index.js index 95610f6..b32bdd1 100644 --- a/pages/records/patient-vitals/index.js +++ b/pages/records/patient-vitals/index.js @@ -15,6 +15,7 @@ import axiosInstance from '@/pages/api/_axiosInstance'; import CustomModal from '@/components/CustomModal'; import useWithLoading from '@/utils/loading'; import NoVisitPlaceholder from '@/components/records/NoVisitPlaceholder'; +import { checkRecency } from '@/utils/check'; const PatientVitals = () => { const [mounted, setMounted] = useState(false); @@ -118,6 +119,19 @@ const PatientVitals = () => { }); const submitVitalsForm = useWithLoading(async () => { + const THREE_DAY_IN_MIN = 4320; // 3 * 60 * 24 + + const toSubmit = await checkRecency( + patient.pk, + THREE_DAY_IN_MIN, + false, + `Are you sure you want to edit this vital? + Press OK to SUBMIT. + Press CANCEL to NOT SUBMIT.` + ); + if (!toSubmit) { + return; + } const formPayload = { ...vitalsFormDetails, visit: selectedVisitID,