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

@ -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', []):

View file

@ -94,26 +94,22 @@ 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);
if (config.sort !== false) {
sortOptions(list);
}
} else {
const option = createOption(newItem);
list.appendChild(option);
}
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);
const key = config.stateArray ?? `${ns}s`; // fallback to pluralization
if (Array.isArray(formState?.[key])) {
formState[key].push(newItem);
}
if (config.sort !== false) {
sortOptions(list);
}
}
@ -123,7 +119,6 @@ const ComboBoxWidget = (() => {
updateAddButtonIcon();
});
removeBtn.addEventListener('click', () => {
Array.from(list.selectedOptions).forEach(option => {
if (config.onRemove) {

View file

@ -21,19 +21,13 @@
</select>
</div>
<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 onRemove %}onRemove: function(option) { {{ onRemove | safe }} },{% endif %}
{% if onEdit %}onEdit: function(option) { {{ onEdit | safe }} }{% endif %}
<script>
document.addEventListener('DOMContentLoaded', () => {
ComboBoxWidget.initComboBox("{{ id }}", {
{% 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 %}
});
});
});
</script>
</script>
{% endmacro %}