Refactor settings logic; streamline brand and item type creation, enhance ComboBoxWidget integration for improved usability

This commit is contained in:
Yaro Kasear 2025-06-23 10:13:22 -05:00
parent c6fc1a4795
commit 347baaa12e
3 changed files with 40 additions and 35 deletions

View file

@ -411,6 +411,22 @@ def settings():
flash("Invalid form state submitted. JSON decode failed.", "danger") flash("Invalid form state submitted. JSON decode failed.", "danger")
traceback.print_exc() traceback.print_exc()
return redirect(url_for('main.settings')) return redirect(url_for('main.settings'))
# === 0. Create new Brands ===
for name in state.get('brands', []):
clean = name.strip()
if clean:
print(f"🧪 Creating new brand: {clean}")
new_brand = Brand(name=clean) # type: ignore
db.session.add(new_brand)
# === 0.5 Create new Types ===
for name in state.get('types', []):
clean = name.strip()
if clean:
print(f"🧪 Creating new item type: {clean}")
new_type = Item(description=clean) # type: ignore
db.session.add(new_type)
# === 1. Create new Sections === # === 1. Create new Sections ===
section_map = {} section_map = {}

View file

@ -94,26 +94,22 @@ const ComboBoxWidget = (() => {
} }
currentlyEditing = null; currentlyEditing = null;
} else { } else {
if (config.onAdd) { const exists = Array.from(list.options).some(opt => opt.textContent === newItem);
config.onAdd(newItem, list, createOption); if (exists) {
} else { alert(`"${newItem}" already exists.`);
// Default fallback here return;
if (config.stateArray && formState && formState[config.stateArray]) { }
const exists = Array.from(list.options).some(opt => opt.textContent === newItem);
if (exists) { const option = createOption(newItem);
alert(`"${newItem}" already exists.`); list.appendChild(option);
return;
} const key = config.stateArray ?? `${ns}s`; // fallback to pluralization
const option = createOption(newItem); if (Array.isArray(formState?.[key])) {
list.appendChild(option); formState[key].push(newItem);
formState[config.stateArray].push(newItem); }
if (config.sort !== false) {
sortOptions(list); if (config.sort !== false) {
} sortOptions(list);
} else {
const option = createOption(newItem);
list.appendChild(option);
}
} }
} }
@ -123,7 +119,6 @@ const ComboBoxWidget = (() => {
updateAddButtonIcon(); updateAddButtonIcon();
}); });
removeBtn.addEventListener('click', () => { removeBtn.addEventListener('click', () => {
Array.from(list.selectedOptions).forEach(option => { Array.from(list.selectedOptions).forEach(option => {
if (config.onRemove) { if (config.onRemove) {

View file

@ -21,19 +21,13 @@
</select> </select>
</div> </div>
<script> <script>
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
ComboBoxWidget.initComboBox("{{ id }}", { ComboBoxWidget.initComboBox("{{ id }}", {
onAdd: function(newItem, list, createOption) { {% if onAdd %}onAdd: function(newItem, list, createOption) { {{ onAdd | safe }} },{% endif %}
{% if onAdd %} {% if onRemove %}onRemove: function(option) { {{ onRemove | safe }} },{% endif %}
{{ onAdd | safe }} {% if onEdit %}onEdit: function(option) { {{ onEdit | safe }} }{% endif %}
{% else %} });
ComboBoxWidget.handleComboAdd('{{ id }}-input', '{{ id }}-list', '{{ id }}s', '{{ label or id }}');
{% endif %}
},
{% if onRemove %}onRemove: function(option) { {{ onRemove | safe }} },{% endif %}
{% if onEdit %}onEdit: function(option) { {{ onEdit | safe }} }{% endif %}
}); });
}); </script>
</script>
{% endmacro %} {% endmacro %}