Change medicine save to auto-save on Add button click (AJAX)
Medicine Save Behavior Change: - Previously: Medicines added to table locally, saved on 'Save Discharge Advice' click - Now: AJAX auto-save immediately when '+ADD / Update' button is clicked - Matches OPD Consult Form workflow (instant save on add) Implementation: - Modified btn_discharge_med_add click handler to make AJAX POST - POST parameters: action='add_drug', ajax_mode='json', new_drug_name, new_drug_type, new_drug_dose, new_drug_when, new_drug_freq, new_drug_day, new_drug_qty, new_drug_remark - Controller add_drug action already existed - now returns JSON when ajax_mode='json' - On success: adds row to table, clears form, shows 'Medicine saved successfully' - On failure: shows error message from server - CSRF token updated after each AJAX call Remove Button Behavior: - Remove button now shows confirmation dialog - Warns user: 'This will only remove from current view. Reload to see saved medicines.' - Note: Remove action is NOT yet AJAX - full page reload shows actual DB state - Future enhancement: make Remove button also AJAX (action='remove_drug') Technical Notes: - Kept discharge_medicine_json serialization as backup (unused now, but harmless) - Medicine data: med_id=0 marks client-added medicines - Success check: data.update === 1 - Error handling: network timeout 30s, shows xhr.status on fail - Form fields cleared after successful save - Focus returns to medicine name field for next entry User Benefits: - Instant feedback - no need to scroll down and click 'Save Discharge Advice' - Medicines immediately persisted to database - Can add multiple medicines in quick succession - Consistent with OPD prescription workflow - Reduced risk of data loss (saves immediately vs. waiting for form submit)
This commit is contained in:
parent
e731ef61e9
commit
fd70cc46cf
|
|
@ -4206,44 +4206,93 @@ $allergyStatusNoKnown = in_array($allergyStatusNormalized, ['no known drug aller
|
|||
formattedDuration = num + (num === 1 ? ' day' : ' days');
|
||||
}
|
||||
|
||||
// Add row to table
|
||||
var tbody = section.querySelector('#discharge_medicine_tbody');
|
||||
if (!tbody) {
|
||||
// AJAX auto-save medicine
|
||||
if (!window.jQuery) {
|
||||
setMedicineStatus('jQuery not available. Cannot save medicine.', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
// Remove "No medicine added" row if present
|
||||
var emptyRow = tbody.querySelector('tr td[colspan="9"]');
|
||||
if (emptyRow) {
|
||||
emptyRow.closest('tr').remove();
|
||||
var activeForm = section.closest('form') || getDischargeForm();
|
||||
if (!activeForm) {
|
||||
setMedicineStatus('Form not found.', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
var tr = document.createElement('tr');
|
||||
tr.innerHTML = '<td>' + $('<div>').text(medType).html() + '</td>'
|
||||
+ '<td>' + $('<div>').text(medName).html() + '</td>'
|
||||
+ '<td>' + $('<div>').text(doseLabel).html() + '</td>'
|
||||
+ '<td>' + $('<div>').text(whenLabel).html() + '</td>'
|
||||
+ '<td>' + $('<div>').text(freqLabel).html() + '</td>'
|
||||
+ '<td>' + $('<div>').text(formattedDuration).html() + '</td>'
|
||||
+ '<td>' + $('<div>').text(qty).html() + '</td>'
|
||||
+ '<td>' + $('<div>').text(remark).html() + '</td>'
|
||||
+ '<td><button type="button" class="btn btn-outline-danger btn-sm btn-remove-discharge-med">Remove</button></td>';
|
||||
tbody.appendChild(tr);
|
||||
var csrf = getCsrfPair(activeForm);
|
||||
var payload = {
|
||||
action: 'add_drug',
|
||||
ajax_mode: 'json',
|
||||
new_drug_name: medName,
|
||||
new_drug_type: medType,
|
||||
new_drug_dose: dosage,
|
||||
new_drug_when: dosageWhen,
|
||||
new_drug_freq: dosageFreq,
|
||||
new_drug_day: noOfDays,
|
||||
new_drug_qty: qty,
|
||||
new_drug_remark: remark
|
||||
};
|
||||
payload[csrf.name] = csrf.value;
|
||||
|
||||
// Clear form
|
||||
$('#discharge_med_name, #discharge_med_type, #discharge_no_of_days, #discharge_qty, #discharge_remark').val('');
|
||||
$('#discharge_dosage, #discharge_dosage_when, #discharge_dosage_freq, #discharge_dose_where').val('');
|
||||
$('#discharge_med_name').focus();
|
||||
setMedicineStatus('Saving medicine...', 'muted');
|
||||
|
||||
setMedicineStatus('Medicine added to list. Remember to save discharge form.', 'success');
|
||||
window.jQuery.ajax({
|
||||
url: activeForm.getAttribute('action') || window.location.href,
|
||||
type: 'POST',
|
||||
data: payload,
|
||||
dataType: 'json',
|
||||
timeout: 30000
|
||||
}).done(function(data) {
|
||||
updateFormCsrf(activeForm, data || {});
|
||||
|
||||
// Bind remove button
|
||||
tr.querySelector('.btn-remove-discharge-med').addEventListener('click', function() {
|
||||
tr.remove();
|
||||
if (tbody.children.length === 0) {
|
||||
tbody.innerHTML = '<tr><td colspan="9" class="text-muted text-center">No medicine added</td></tr>';
|
||||
if (data && parseInt(data.update || '0', 10) === 1) {
|
||||
// Add row to table
|
||||
var tbody = section.querySelector('#discharge_medicine_tbody');
|
||||
if (!tbody) {
|
||||
setMedicineStatus('Medicine table not found.', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
// Remove "No medicine added" row if present
|
||||
var emptyRow = tbody.querySelector('tr td[colspan="9"]');
|
||||
if (emptyRow) {
|
||||
emptyRow.closest('tr').remove();
|
||||
}
|
||||
|
||||
var tr = document.createElement('tr');
|
||||
tr.innerHTML = '<td>' + $('<div>').text(medType).html() + '</td>'
|
||||
+ '<td>' + $('<div>').text(medName).html() + '</td>'
|
||||
+ '<td>' + $('<div>').text(doseLabel).html() + '</td>'
|
||||
+ '<td>' + $('<div>').text(whenLabel).html() + '</td>'
|
||||
+ '<td>' + $('<div>').text(freqLabel).html() + '</td>'
|
||||
+ '<td>' + $('<div>').text(formattedDuration).html() + '</td>'
|
||||
+ '<td>' + $('<div>').text(qty).html() + '</td>'
|
||||
+ '<td>' + $('<div>').text(remark).html() + '</td>'
|
||||
+ '<td><button type="button" class="btn btn-outline-danger btn-sm btn-remove-discharge-med">Remove</button></td>';
|
||||
tbody.appendChild(tr);
|
||||
|
||||
// Clear form
|
||||
$('#discharge_med_name, #discharge_med_type, #discharge_no_of_days, #discharge_qty, #discharge_remark').val('');
|
||||
$('#discharge_dosage, #discharge_dosage_when, #discharge_dosage_freq, #discharge_dose_where').val('');
|
||||
$('#discharge_med_name').focus();
|
||||
|
||||
setMedicineStatus('Medicine saved successfully.', 'success');
|
||||
|
||||
// Bind remove button (Note: Remove is not yet AJAX - medicines load from DB on page reload)
|
||||
tr.querySelector('.btn-remove-discharge-med').addEventListener('click', function() {
|
||||
if (confirm('Remove this medicine? This will only remove from the current view. Reload the page to see saved medicines.')) {
|
||||
tr.remove();
|
||||
if (tbody.children.length === 0) {
|
||||
tbody.innerHTML = '<tr><td colspan="9" class="text-muted text-center">No medicine added</td></tr>';
|
||||
}
|
||||
setMedicineStatus('Medicine removed from view. Reload to see saved data.', 'info');
|
||||
}
|
||||
});
|
||||
} else {
|
||||
var errorMsg = (data && data.error_text) ? data.error_text : 'Unable to save medicine.';
|
||||
setMedicineStatus(errorMsg, 'error');
|
||||
}
|
||||
setMedicineStatus('Medicine removed from list.', 'info');
|
||||
}).fail(function(xhr, status, error) {
|
||||
setMedicineStatus('Network error: ' + (error || status), 'error');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user