From d77d86087d8aa20cd59d4a3456f048cfce7d8d49 Mon Sep 17 00:00:00 2001 From: MarekWo Date: Fri, 24 Apr 2026 21:39:17 +0200 Subject: [PATCH] fix(regions): allow clearing the default region in Region Registry Click the already-selected radio to clear the default; new DELETE /api/regions/default endpoint also pushes an empty CMD 63 to the firmware so its persistent default is wiped too. Co-Authored-By: Claude Opus 4.7 --- app/routes/api.py | 37 +++++++++++++++++++++++++++++++++++++ app/static/js/app.js | 34 +++++++++++++++++++++++++++++++++- app/templates/base.html | 2 +- 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/app/routes/api.py b/app/routes/api.py index 9c51d0d..d7746df 100644 --- a/app/routes/api.py +++ b/app/routes/api.py @@ -4070,6 +4070,43 @@ def set_channel_scope_api(index): return jsonify({'success': False, 'error': str(e)}), 500 +@api_bp.route('/regions/default', methods=['DELETE']) +def clear_default_region_api(): + """Clear the default-region flag in the DB and the firmware default (CMD 63). + + If the firmware push fails the DB flag is still cleared; we return 200 with + a non-blocking `warning` so the UI can toast it. + """ + try: + db = _get_db() + if not db: + return jsonify({'success': False, 'error': 'Database not available'}), 500 + + had_default = db.get_default_region() is not None + db.set_default_region(None) + + warning = None + if had_default: + dm = _get_dm() + if dm and dm.is_connected: + try: + res = dm.set_default_flood_scope('', '') + if not res.get('success'): + warning = f"{res.get('error')} Your choice is saved locally." + except Exception as e: + warning = f"Could not clear firmware default: {e}. Your choice is saved locally." + else: + warning = 'Device disconnected — choice saved locally; firmware default not updated.' + + out = {'success': True} + if warning: + out['warning'] = warning + return jsonify(out), 200 + except Exception as e: + logger.error(f"Error clearing default region: {e}") + return jsonify({'success': False, 'error': str(e)}), 500 + + @api_bp.route('/regions//default', methods=['POST']) def set_default_region_api(region_id): """Mark a region as default in the DB AND push it to the firmware (CMD 63). diff --git a/app/static/js/app.js b/app/static/js/app.js index c9d2840..a082d8c 100644 --- a/app/static/js/app.js +++ b/app/static/js/app.js @@ -2871,7 +2871,8 @@ function renderRegionsList() {
+ title="Click again to clear the default" + onclick="handleRegionRadioClick(${r.id}, this)">
${escapeHtml(r.name)}
@@ -2927,6 +2928,17 @@ async function deleteRegion(id, name) { } } +function handleRegionRadioClick(id, inputEl) { + // Click on the already-selected default clears the default; otherwise sets it. + const wasDefault = (window.regionRegistry || []).some(r => r.id === id && r.is_default); + if (wasDefault) { + inputEl.checked = false; + clearDefaultRegion(); + } else { + setDefaultRegion(id); + } +} + async function setDefaultRegion(id) { try { const resp = await fetch(`/api/regions/${id}/default`, { method: 'POST' }); @@ -2948,6 +2960,26 @@ async function setDefaultRegion(id) { } } +async function clearDefaultRegion() { + try { + const resp = await fetch('/api/regions/default', { method: 'DELETE' }); + const data = await resp.json().catch(() => ({})); + if (!resp.ok || !data.success) { + showNotification(data.error || 'Failed to clear default region', 'danger'); + await loadRegions(); // snap UI back to server truth + return; + } + if (data.warning) { + showNotification(data.warning, 'warning'); + } + (window.regionRegistry || []).forEach(r => { r.is_default = 0; }); + } catch (e) { + console.error('Error clearing default region:', e); + showNotification('Network error clearing default', 'danger'); + await loadRegions(); + } +} + // ================================================================ // Per-channel region picker (Manage Channels > row > pin icon) // ================================================================ diff --git a/app/templates/base.html b/app/templates/base.html index 6037873..a71a71e 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -792,7 +792,7 @@
- Tip: pick the default region via the radio button. The default is also pushed to the firmware so any untagged channel uses it. + Tip: pick the default region via the radio button. Click the selected radio again to clear it. The default is also pushed to the firmware so any untagged channel uses it.