';
@@ -605,14 +607,18 @@
var btnLinkHead = document.getElementById('btnLinkThisPatient');
if (btnLinkHead) {
btnLinkHead.addEventListener('click', function() {
- if (currentPatientData) {
- openAbdmHipLinkModal(currentPatientData.id, currentPatientData.abha_address, {
- name: currentPatientData.name,
- gender: currentPatientData.gender,
- yob: currentPatientData.year_of_birth,
- phone: currentPatientData.phone,
+ var pData = window.inspCurrentPatientData || currentPatientData;
+ if (pData) {
+ openAbdmHipLinkModal(pData.id, pData.abha_address, {
+ name: pData.name,
+ gender: pData.gender,
+ yob: pData.year_of_birth,
+ phone: pData.phone,
+ uhid: pData.patient_ref || pData.p_code,
onLinked: function() {
- inspectPatient(currentPatientData.abha_address);
+ if (typeof window.inspectPatient === 'function' && pData.abha_address) {
+ window.inspectPatient(pData.abha_address);
+ }
}
});
} else {
@@ -621,35 +627,59 @@
});
}
- // Link Context Button (Row action)
- document.addEventListener('click', function(e) {
- var linkBtn = e.target.closest('.btn-table-link');
- if (linkBtn && currentPatientData) {
- var ref = linkBtn.dataset.ref;
- var type = linkBtn.dataset.type;
- openAbdmHipLinkModal(currentPatientData.id, currentPatientData.abha_address, {
- name: currentPatientData.name,
- gender: currentPatientData.gender,
- yob: currentPatientData.year_of_birth,
- phone: currentPatientData.phone,
- care_context: ref,
- task_type: type,
- onLinked: function() {
- inspectPatient(currentPatientData.abha_address);
- }
- });
- }
- });
+ window.inspOpenFhirBundleModal = openFhirBundleModal;
- // Preview FHIR Button (Row action)
- document.addEventListener('click', function(e) {
- var fhirBtn = e.target.closest('.btn-open-fhir');
- if (fhirBtn) {
- var ref = fhirBtn.dataset.ref;
- var hiType = fhirBtn.dataset.hitype || 'Record';
- openFhirBundleModal(ref, hiType);
- }
- });
+ // Singleton Document-level Click Handler to prevent multiple listener duplication on SPA reloads
+ if (!window._inspDocClickListenerAttached) {
+ window._inspDocClickListenerAttached = true;
+
+ document.addEventListener('click', function(e) {
+ var linkBtn = e.target.closest('.btn-table-link');
+ if (linkBtn && window.inspCurrentPatientData) {
+ var pData = window.inspCurrentPatientData;
+ var ref = linkBtn.dataset.ref;
+ var type = linkBtn.dataset.type;
+ var entityId = linkBtn.dataset.entityId || '';
+ var taskType = '';
+ if (type === 'HealthDocumentRecord') {
+ taskType = 'health_document_publish';
+ } else if (type === 'OPConsultRecord') {
+ taskType = 'opd_prescription_publish';
+ } else if (type === 'DiagnosticReportRecord' || type === 'DiagnosticReport') {
+ taskType = 'lab_report_publish';
+ } else if (type === 'WellnessRecord') {
+ taskType = 'wellness_record_publish';
+ } else if (type === 'InvoiceRecord') {
+ taskType = 'invoice_publish';
+ }
+
+ openAbdmHipLinkModal(pData.id, pData.abha_address, {
+ name: pData.name,
+ gender: pData.gender,
+ yob: pData.year_of_birth,
+ phone: pData.phone,
+ uhid: pData.patient_ref || pData.p_code,
+ care_context: ref,
+ task_type: taskType,
+ entity_id: entityId,
+ onLinked: function() {
+ if (typeof window.inspectPatient === 'function' && pData.abha_address) {
+ window.inspectPatient(pData.abha_address);
+ }
+ }
+ });
+ return;
+ }
+
+ var fhirBtn = e.target.closest('.btn-open-fhir');
+ if (fhirBtn && typeof window.inspOpenFhirBundleModal === 'function') {
+ var ref = fhirBtn.dataset.ref;
+ var hiType = fhirBtn.dataset.hitype || 'Record';
+ window.inspOpenFhirBundleModal(ref, hiType);
+ return;
+ }
+ });
+ }
function openFhirBundleModal(ref, hiType) {
var modalEl = document.getElementById('inspFhirModal');
diff --git a/app/Views/partials/abdm_hip_link_modal.php b/app/Views/partials/abdm_hip_link_modal.php
index 5fa6689..7dea11a 100644
--- a/app/Views/partials/abdm_hip_link_modal.php
+++ b/app/Views/partials/abdm_hip_link_modal.php
@@ -406,7 +406,11 @@
/**
* Load patient demographics and care contexts from server.
*/
+ window._hipLoadSeq = 0;
window.loadPatientCareContexts = function(patientId, abhaAddress, taskType, entityId, taskId, source) {
+ window._hipLoadSeq = (window._hipLoadSeq || 0) + 1;
+ var thisSeq = window._hipLoadSeq;
+
var loading = document.getElementById('hipCareContextsLoading');
var container = document.getElementById('hipCareContextsContainer');
if (loading) loading.classList.remove('d-none');
@@ -435,6 +439,18 @@
})
.then(function(r) { return r.json(); })
.then(function(data) {
+ // Discard stale response if a newer request was made
+ if (thisSeq !== window._hipLoadSeq) {
+ return;
+ }
+ // Discard response if patient mismatch with current context
+ if (window.currentHipTaskContext && window.currentHipTaskContext.patientId) {
+ var expectedPid = parseInt(window.currentHipTaskContext.patientId, 10);
+ if (data.patient && data.patient.id && parseInt(data.patient.id, 10) !== expectedPid) {
+ return;
+ }
+ }
+
if (loading) loading.classList.add('d-none');
updateCsrf(data);
@@ -554,6 +570,12 @@
document.getElementById('hipGender').value = prefill.gender || 'M';
document.getElementById('hipYob').value = prefill.year_of_birth || prefill.yob || '';
document.getElementById('hipSmsPhone').value = prefill.phone || prefill.mphone1 || '';
+
+ var uhidBadge = document.getElementById('hipPatientUhidBadge');
+ var initialUhid = prefill.uhid || prefill.patient_ref || prefill.p_code || prefill.uhid_no || '';
+ if (uhidBadge && initialUhid) {
+ uhidBadge.textContent = 'UHID: ' + initialUhid;
+ }
var alertBox = document.getElementById('hipLinkStatusAlert');
if (alertBox) { alertBox.classList.add('d-none'); alertBox.innerHTML = ''; }
@@ -601,15 +623,41 @@
};
function syncAndPushTaskFhirRecord(selectedContexts, alertBox, abhaAddress) {
- if (!window.currentHipTaskContext || !window.currentHipTaskContext.taskType || !window.currentHipTaskContext.entityId) {
- return;
- }
- var ctxTaskType = window.currentHipTaskContext.taskType;
- var ctxEntityId = window.currentHipTaskContext.entityId;
- var ctxPatientId = window.currentHipTaskContext.patientId || 0;
+ var ctxTaskType = (window.currentHipTaskContext && window.currentHipTaskContext.taskType) ? window.currentHipTaskContext.taskType : '';
+ var ctxEntityId = (window.currentHipTaskContext && window.currentHipTaskContext.entityId) ? window.currentHipTaskContext.entityId : '';
+ var ctxPatientId = (window.currentHipTaskContext && window.currentHipTaskContext.patientId) ? window.currentHipTaskContext.patientId : 0;
var firstRef = (selectedContexts && selectedContexts.length > 0) ? selectedContexts[0].ref : '';
var csrf = getCsrfData();
+ // Auto-infer task type and entity ID from reference if not explicitly set
+ if (!ctxTaskType && firstRef) {
+ if (/^DOC-/i.test(firstRef)) {
+ ctxTaskType = 'health_document_publish';
+ var dm = firstRef.match(/^DOC-(?:file-)?(\d+)/i);
+ if (dm && !ctxEntityId) ctxEntityId = dm[1];
+ } else if (/^(?:OPD|PRESCRIPTION)-/i.test(firstRef)) {
+ ctxTaskType = 'opd_prescription_publish';
+ var om = firstRef.match(/^(?:OPD|PRESCRIPTION)-(\d+)/i);
+ if (om && !ctxEntityId) ctxEntityId = om[1];
+ } else if (/^WELLNESS-/i.test(firstRef)) {
+ ctxTaskType = 'wellness_record_publish';
+ var wm = firstRef.match(/^WELLNESS-(?:(\d+)-W)?(\d+)/i);
+ if (wm && !ctxEntityId) ctxEntityId = wm[2] || wm[1];
+ } else if (/^INV-/i.test(firstRef)) {
+ ctxTaskType = 'invoice_publish';
+ var im = firstRef.match(/^INV-(?:[A-Z]+-)?(\d+)/i);
+ if (im && !ctxEntityId) ctxEntityId = im[1];
+ } else if (/^(?:LAB|RAD)-/i.test(firstRef)) {
+ ctxTaskType = 'lab_report_publish';
+ var lm = firstRef.match(/^(?:LAB|RAD)-(\d+)/i);
+ if (lm && !ctxEntityId) ctxEntityId = lm[1];
+ }
+ }
+
+ if (!ctxTaskType) {
+ return;
+ }
+
if (ctxTaskType === 'radiology_report_publish' || ctxTaskType === 'lab_report_publish') {
var pushData = new URLSearchParams();
pushData.append('lab_req_id', ctxEntityId);
@@ -748,6 +796,10 @@
}
}).catch(function() {});
} else if (ctxTaskType === 'health_document_publish') {
+ if (!ctxEntityId && firstRef) {
+ var dm = firstRef.match(/^DOC-(?:file-)?(\d+)/i);
+ if (dm) ctxEntityId = dm[1];
+ }
var pushData = new URLSearchParams();
pushData.append('record_id', ctxEntityId);
pushData.append('entity_id', ctxEntityId);
diff --git a/public/App/Nursing/index.html b/public/App/Nursing/index.html
index 32f6f13..1d9ddd0 100644
--- a/public/App/Nursing/index.html
+++ b/public/App/Nursing/index.html
@@ -506,6 +506,7 @@
document_date: new Date().toISOString().slice(0, 10),
remarks: ''
});
+ const [isSavingDoc, setIsSavingDoc] = useState(false);
// Vitals History Modal State
const [wellnessHistoryPatient, setWellnessHistoryPatient] = useState(null);
@@ -1142,11 +1143,12 @@
};
const handleSavePatientDocument = async () => {
- if (!activeDocPatient) return;
+ if (!activeDocPatient || isSavingDoc) return;
if (!scanImageBase64) {
alert('Please capture or upload a document photo first.');
return;
}
+ setIsSavingDoc(true);
try {
const payload = {
patient_id: activeDocPatient.id,
@@ -1171,6 +1173,8 @@
}
} catch (err) {
alert('Error uploading health document');
+ } finally {
+ setIsSavingDoc(false);
}
};
@@ -3017,9 +3021,13 @@
-
-