Refactor settings logic; streamline brand and item type creation, enhance ComboBoxWidget integration for improved usability
This commit is contained in:
parent
c6fc1a4795
commit
347baaa12e
3 changed files with 40 additions and 35 deletions
16
routes.py
16
routes.py
|
@ -412,6 +412,22 @@ def settings():
|
|||
traceback.print_exc()
|
||||
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 ===
|
||||
section_map = {}
|
||||
for name in state.get('sections', []):
|
||||
|
|
|
@ -94,27 +94,23 @@ const ComboBoxWidget = (() => {
|
|||
}
|
||||
currentlyEditing = null;
|
||||
} else {
|
||||
if (config.onAdd) {
|
||||
config.onAdd(newItem, list, createOption);
|
||||
} else {
|
||||
// Default fallback here
|
||||
if (config.stateArray && formState && formState[config.stateArray]) {
|
||||
const exists = Array.from(list.options).some(opt => opt.textContent === newItem);
|
||||
if (exists) {
|
||||
alert(`"${newItem}" already exists.`);
|
||||
return;
|
||||
}
|
||||
|
||||
const option = createOption(newItem);
|
||||
list.appendChild(option);
|
||||
formState[config.stateArray].push(newItem);
|
||||
|
||||
const key = config.stateArray ?? `${ns}s`; // fallback to pluralization
|
||||
if (Array.isArray(formState?.[key])) {
|
||||
formState[key].push(newItem);
|
||||
}
|
||||
|
||||
if (config.sort !== false) {
|
||||
sortOptions(list);
|
||||
}
|
||||
} else {
|
||||
const option = createOption(newItem);
|
||||
list.appendChild(option);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
input.value = '';
|
||||
|
@ -123,7 +119,6 @@ const ComboBoxWidget = (() => {
|
|||
updateAddButtonIcon();
|
||||
});
|
||||
|
||||
|
||||
removeBtn.addEventListener('click', () => {
|
||||
Array.from(list.selectedOptions).forEach(option => {
|
||||
if (config.onRemove) {
|
||||
|
|
|
@ -24,13 +24,7 @@
|
|||
<script>
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
ComboBoxWidget.initComboBox("{{ id }}", {
|
||||
onAdd: function(newItem, list, createOption) {
|
||||
{% if onAdd %}
|
||||
{{ onAdd | safe }}
|
||||
{% else %}
|
||||
ComboBoxWidget.handleComboAdd('{{ id }}-input', '{{ id }}-list', '{{ id }}s', '{{ label or id }}');
|
||||
{% endif %}
|
||||
},
|
||||
{% if onAdd %}onAdd: function(newItem, list, createOption) { {{ onAdd | safe }} },{% endif %}
|
||||
{% if onRemove %}onRemove: function(option) { {{ onRemove | safe }} },{% endif %}
|
||||
{% if onEdit %}onEdit: function(option) { {{ onEdit | safe }} }{% endif %}
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue