Spaces:
Sleeping
Sleeping
File size: 5,543 Bytes
e9b7eb0 c4fa4ca cd342b7 94fccea cd342b7 1faabaf cd342b7 1faabaf cd342b7 84e8d07 cd342b7 540bab9 cd342b7 94fccea cd342b7 e9b7eb0 cd342b7 e9b7eb0 540bab9 e9b7eb0 cd342b7 84e8d07 cd342b7 84e8d07 cd342b7 1faabaf cd342b7 cc7ea57 d3e0bda 1faabaf d3e0bda 1faabaf d3e0bda cc7ea57 94fccea cd342b7 e9b7eb0 cd342b7 e9b7eb0 c4fa4ca cd342b7 1faabaf cd342b7 94fccea cd342b7 e9b7eb0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
document.addEventListener('DOMContentLoaded', () => {
const form = document.getElementById('patientForm');
const result = document.getElementById('result');
const cancelBtn = document.getElementById('cancelBtn');
const successModal = document.getElementById('patientSuccessModal');
const successClose = document.getElementById('patientSuccessClose');
const successReturn = document.getElementById('patientSuccessReturn');
const successEdit = document.getElementById('patientSuccessEdit');
const createdIdEl = document.getElementById('createdPatientId');
const submitBtn = form?.querySelector('button[type="submit"]');
const titleEl = document.querySelector('h1');
let isEditMode = false;
let currentPatientId = null;
function getPatientIdFromUrl() {
const urlParams = new URLSearchParams(window.location.search);
// The API uses a generic string 'id', but we'll keep the client-side validation for now.
const pidFromUrl = urlParams.get('patient_id');
return pidFromUrl;
}
async function loadPatientIntoForm(patientId) {
try {
const resp = await fetch(`/patient/${patientId}`);
if (!resp.ok) return;
const data = await resp.json();
document.getElementById('name').value = data.name || '';
document.getElementById('age').value = data.age ?? '';
document.getElementById('sex').value = data.sex || 'Other';
document.getElementById('ethnicity').value = data.ethnicity || '';
document.getElementById('address').value = data.address || '';
document.getElementById('phone').value = data.phone || '';
document.getElementById('email').value = data.email || '';
document.getElementById('medications').value = Array.isArray(data.medications) ? data.medications.join('\n') : '';
document.getElementById('summary').value = data.past_assessment_summary || '';
} catch (e) {
console.warn('Failed to load patient profile for editing', e);
}
}
function enableEditMode(patientId) {
isEditMode = true;
currentPatientId = patientId;
if (submitBtn) submitBtn.textContent = 'Update';
if (titleEl) titleEl.textContent = 'Edit Patient';
}
// Initialize: only enter edit mode if patient_id is explicitly in URL
const pidFromUrl = getPatientIdFromUrl();
if (pidFromUrl) {
enableEditMode(pidFromUrl);
loadPatientIntoForm(pidFromUrl);
}
cancelBtn.addEventListener('click', () => {
window.location.href = '/';
});
form.addEventListener('submit', async (e) => {
e.preventDefault();
result.textContent = '';
result.style.color = '';
const payload = {
name: document.getElementById('name').value.trim(),
age: parseInt(document.getElementById('age').value, 10),
sex: document.getElementById('sex').value,
ethnicity: document.getElementById('ethnicity').value,
address: document.getElementById('address').value.trim() || null,
phone: document.getElementById('phone').value.trim() || null,
email: document.getElementById('email').value.trim() || null,
medications: document.getElementById('medications').value.split('\n').map(s => s.trim()).filter(Boolean),
past_assessment_summary: document.getElementById('summary').value.trim() || null
};
try {
if (isEditMode && currentPatientId) {
const resp = await fetch(`/patient/${currentPatientId}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
if (!resp.ok) throw new Error(`HTTP ${resp.status}`);
result.textContent = 'Patient updated successfully.';
result.style.color = 'green';
} else {
const resp = await fetch('/patient', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
if (!resp.ok) throw new Error(`HTTP ${resp.status}`);
const data = await resp.json();
const pid = data.id;
localStorage.setItem('medicalChatbotPatientId', pid);
// Add to localStorage for future suggestions
const existingPatients = JSON.parse(localStorage.getItem('medicalChatbotPatients') || '[]');
const newPatient = {
id: pid,
name: payload.name,
age: payload.age,
sex: payload.sex
};
// Check if patient already exists to avoid duplicates
const exists = existingPatients.some(p => p.id === pid);
if (!exists) {
existingPatients.push(newPatient);
localStorage.setItem('medicalChatbotPatients', JSON.stringify(existingPatients));
}
// Show success modal (stay in create view until user opts to edit)
if (createdIdEl) createdIdEl.textContent = pid;
successModal.classList.add('show');
}
} catch (err) {
console.error(err);
result.textContent = isEditMode ? 'Failed to update patient. Please try again.' : 'Failed to create patient. Please try again.';
result.style.color = 'crimson';
}
});
// Success modal wiring
if (successClose) successClose.addEventListener('click', () => successModal.classList.remove('show'));
if (successReturn) successReturn.addEventListener('click', () => { window.location.href = '/'; });
if (successEdit) successEdit.addEventListener('click', () => {
successModal.classList.remove('show');
const pid = createdIdEl?.textContent?.trim() || localStorage.getItem('medicalChatbotPatientId');
if (pid) {
// Update URL to reflect edit mode for clarity and reload safety
const newUrl = new URL(window.location.href);
newUrl.searchParams.set('patient_id', pid);
window.history.pushState({ path: newUrl.href }, '', newUrl.href);
enableEditMode(pid);
loadPatientIntoForm(pid);
}
});
});
|