Refactor entity synchronization logic in Area, Brand, Item, RoomFunction, and Room models; improve ID handling and streamline foreign key resolution in settings

This commit is contained in:
Yaro Kasear 2025-06-25 11:23:33 -05:00
parent 7833c4828b
commit 543494120c
9 changed files with 170 additions and 96 deletions

View file

@ -1,6 +1,6 @@
{% import "fragments/_icon_fragment.html" as icons %}
{% macro render_combobox(id, options, label=none, placeholder=none, onAdd=none, onRemove=none, onEdit=none) %}
{% macro render_combobox(id, options, label=none, placeholder=none, onAdd=none, onRemove=none, onEdit=none, data_attributes=none) %}
<!-- Combobox Widget Fragment -->
{% if label %}
@ -18,7 +18,16 @@
</div>
<select class="form-select border-top-0 rounded-top-0" id="{{ id }}-list" name="{{ id }}" size="10" multiple>
{% for option in options %}
<option value="{{ option.id }}">{{ option.name }}</option>
<option value="{{ option.id }}"
{% if data_attributes %}
{% for key, data_attr in data_attributes.items() %}
{% if option[key] is defined %}
data-{{ data_attr }}="{{ option[key] }}"
{% endif %}
{% endfor %}
{% endif %}>
{{ option.name }}
</option>
{% endfor %}
</select>
</div>

View file

@ -84,7 +84,8 @@ submit_button=True
label='Rooms',
placeholder='Add a new room',
onAdd=room_editor,
onEdit=room_editor
onEdit=room_editor,
data_attributes={'area_id': 'section-id', 'function_id': 'function-id'}
) }}
</div>
</div>
@ -154,17 +155,23 @@ submit_button=True
}));
}
function sanitizeFk(val) {
return val && val !== "null" && val !== "" ? val : null;
}
const roomOptions = Array.from(document.getElementById('room-list').options);
const rooms = roomOptions.map(opt => {
const data = opt.dataset;
return {
id: opt.value || undefined,
name: opt.textContent.trim(),
section_id: data.sectionId ?? null,
function_id: data.functionId ?? null,
section_id: sanitizeFk(data.sectionId),
function_id: sanitizeFk(data.functionId),
};
});
return {
brands: extractOptions("brand"),
types: extractOptions("type"),
@ -181,8 +188,16 @@ submit_button=True
const form = document.getElementById('settingsForm');
// Replace the whole submission logic with just JSON
form.addEventListener('submit', () => {
document.getElementById('formStateField').value = JSON.stringify(buildFormState());
form.addEventListener('submit', (event) => {
event.preventDefault(); // 🚨 Stop form from leaving the building
try {
const state = buildFormState();
document.getElementById('formStateField').value = JSON.stringify(state);
form.submit(); // 🟢 Now it can go
} catch (err) {
alert("Form submission failed: " + err.message);
console.error("Failed to build form state:", err);
}
});
// Modal populates dropdowns fresh from the page every time it opens
@ -232,8 +247,8 @@ submit_button=True
existingOption.textContent = name;
existingOption.value = idRaw;
existingOption.dataset.sectionId = sectionVal;
existingOption.dataset.functionId = funcVal;
existingOption.dataset.sectionId = sectionVal || "";
existingOption.dataset.functionId = funcVal || "";
ComboBoxWidget.sortOptions(roomList);