Spaces:
Runtime error
Runtime error
Commit
·
cd342b7
1
Parent(s):
fe5e62a
Upd patient.js PATCH route
Browse files- static/js/patient.js +80 -15
static/js/patient.js
CHANGED
|
@@ -7,6 +7,52 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 7 |
const successReturn = document.getElementById('patientSuccessReturn');
|
| 8 |
const successEdit = document.getElementById('patientSuccessEdit');
|
| 9 |
const createdIdEl = document.getElementById('createdPatientId');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
cancelBtn.addEventListener('click', () => {
|
| 12 |
window.location.href = '/';
|
|
@@ -14,6 +60,8 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 14 |
|
| 15 |
form.addEventListener('submit', async (e) => {
|
| 16 |
e.preventDefault();
|
|
|
|
|
|
|
| 17 |
const payload = {
|
| 18 |
name: document.getElementById('name').value.trim(),
|
| 19 |
age: parseInt(document.getElementById('age').value, 10),
|
|
@@ -25,23 +73,34 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 25 |
past_assessment_summary: document.getElementById('summary').value.trim() || null
|
| 26 |
};
|
| 27 |
try {
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
throw new Error(`HTTP ${resp.status}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
}
|
| 36 |
-
const data = await resp.json();
|
| 37 |
-
const pid = data.patient_id;
|
| 38 |
-
localStorage.setItem('medicalChatbotPatientId', pid);
|
| 39 |
-
// Show success modal
|
| 40 |
-
if (createdIdEl) createdIdEl.textContent = pid;
|
| 41 |
-
successModal.classList.add('show');
|
| 42 |
} catch (err) {
|
| 43 |
console.error(err);
|
| 44 |
-
result.textContent = 'Failed to create patient. Please try again.';
|
| 45 |
result.style.color = 'crimson';
|
| 46 |
}
|
| 47 |
});
|
|
@@ -49,5 +108,11 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 49 |
// Success modal wiring
|
| 50 |
if (successClose) successClose.addEventListener('click', () => successModal.classList.remove('show'));
|
| 51 |
if (successReturn) successReturn.addEventListener('click', () => { window.location.href = '/'; });
|
| 52 |
-
if (successEdit) successEdit.addEventListener('click', () => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
});
|
|
|
|
| 7 |
const successReturn = document.getElementById('patientSuccessReturn');
|
| 8 |
const successEdit = document.getElementById('patientSuccessEdit');
|
| 9 |
const createdIdEl = document.getElementById('createdPatientId');
|
| 10 |
+
const submitBtn = form?.querySelector('button[type="submit"]');
|
| 11 |
+
const titleEl = document.querySelector('h1');
|
| 12 |
+
|
| 13 |
+
let isEditMode = false;
|
| 14 |
+
let currentPatientId = null;
|
| 15 |
+
|
| 16 |
+
function getPatientIdFromContext() {
|
| 17 |
+
const urlParams = new URLSearchParams(window.location.search);
|
| 18 |
+
const pidFromUrl = urlParams.get('patient_id');
|
| 19 |
+
if (pidFromUrl && /^\d{8}$/.test(pidFromUrl)) return pidFromUrl;
|
| 20 |
+
const pidFromStorage = localStorage.getItem('medicalChatbotPatientId');
|
| 21 |
+
if (pidFromStorage && /^\d{8}$/.test(pidFromStorage)) return pidFromStorage;
|
| 22 |
+
return null;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
async function loadPatientIntoForm(patientId) {
|
| 26 |
+
try {
|
| 27 |
+
const resp = await fetch(`/patients/${patientId}`);
|
| 28 |
+
if (!resp.ok) return;
|
| 29 |
+
const data = await resp.json();
|
| 30 |
+
document.getElementById('name').value = data.name || '';
|
| 31 |
+
document.getElementById('age').value = data.age ?? '';
|
| 32 |
+
document.getElementById('sex').value = data.sex || 'Other';
|
| 33 |
+
document.getElementById('address').value = data.address || '';
|
| 34 |
+
document.getElementById('phone').value = data.phone || '';
|
| 35 |
+
document.getElementById('email').value = data.email || '';
|
| 36 |
+
document.getElementById('medications').value = Array.isArray(data.medications) ? data.medications.join('\n') : '';
|
| 37 |
+
document.getElementById('summary').value = data.past_assessment_summary || '';
|
| 38 |
+
} catch (e) {
|
| 39 |
+
console.warn('Failed to load patient profile for editing', e);
|
| 40 |
+
}
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
function enableEditMode(patientId) {
|
| 44 |
+
isEditMode = true;
|
| 45 |
+
currentPatientId = patientId;
|
| 46 |
+
if (submitBtn) submitBtn.textContent = 'Update';
|
| 47 |
+
if (titleEl) titleEl.textContent = 'Edit Patient';
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
// Initialize edit mode if patient_id is available
|
| 51 |
+
const detectedPid = getPatientIdFromContext();
|
| 52 |
+
if (detectedPid) {
|
| 53 |
+
enableEditMode(detectedPid);
|
| 54 |
+
loadPatientIntoForm(detectedPid);
|
| 55 |
+
}
|
| 56 |
|
| 57 |
cancelBtn.addEventListener('click', () => {
|
| 58 |
window.location.href = '/';
|
|
|
|
| 60 |
|
| 61 |
form.addEventListener('submit', async (e) => {
|
| 62 |
e.preventDefault();
|
| 63 |
+
result.textContent = '';
|
| 64 |
+
result.style.color = '';
|
| 65 |
const payload = {
|
| 66 |
name: document.getElementById('name').value.trim(),
|
| 67 |
age: parseInt(document.getElementById('age').value, 10),
|
|
|
|
| 73 |
past_assessment_summary: document.getElementById('summary').value.trim() || null
|
| 74 |
};
|
| 75 |
try {
|
| 76 |
+
if (isEditMode && currentPatientId) {
|
| 77 |
+
const resp = await fetch(`/patients/${currentPatientId}`, {
|
| 78 |
+
method: 'PATCH',
|
| 79 |
+
headers: { 'Content-Type': 'application/json' },
|
| 80 |
+
body: JSON.stringify(payload)
|
| 81 |
+
});
|
| 82 |
+
if (!resp.ok) throw new Error(`HTTP ${resp.status}`);
|
| 83 |
+
result.textContent = 'Patient updated successfully.';
|
| 84 |
+
result.style.color = 'green';
|
| 85 |
+
} else {
|
| 86 |
+
const resp = await fetch('/patients', {
|
| 87 |
+
method: 'POST',
|
| 88 |
+
headers: { 'Content-Type': 'application/json' },
|
| 89 |
+
body: JSON.stringify(payload)
|
| 90 |
+
});
|
| 91 |
+
if (!resp.ok) throw new Error(`HTTP ${resp.status}`);
|
| 92 |
+
const data = await resp.json();
|
| 93 |
+
const pid = data.patient_id;
|
| 94 |
+
localStorage.setItem('medicalChatbotPatientId', pid);
|
| 95 |
+
// Show success modal
|
| 96 |
+
if (createdIdEl) createdIdEl.textContent = pid;
|
| 97 |
+
successModal.classList.add('show');
|
| 98 |
+
// Switch to edit mode automatically after creation
|
| 99 |
+
enableEditMode(pid);
|
| 100 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
} catch (err) {
|
| 102 |
console.error(err);
|
| 103 |
+
result.textContent = isEditMode ? 'Failed to update patient. Please try again.' : 'Failed to create patient. Please try again.';
|
| 104 |
result.style.color = 'crimson';
|
| 105 |
}
|
| 106 |
});
|
|
|
|
| 108 |
// Success modal wiring
|
| 109 |
if (successClose) successClose.addEventListener('click', () => successModal.classList.remove('show'));
|
| 110 |
if (successReturn) successReturn.addEventListener('click', () => { window.location.href = '/'; });
|
| 111 |
+
if (successEdit) successEdit.addEventListener('click', () => {
|
| 112 |
+
successModal.classList.remove('show');
|
| 113 |
+
const pid = createdIdEl?.textContent?.trim() || localStorage.getItem('medicalChatbotPatientId');
|
| 114 |
+
if (pid && /^\d{8}$/.test(pid)) {
|
| 115 |
+
enableEditMode(pid);
|
| 116 |
+
}
|
| 117 |
+
});
|
| 118 |
});
|