Fix indicator toggle functionality and add loading feedback
- Fix JSON parsing bug: JavaScript FormData sends indicator list as JSON string inside a list, now properly parsed on backend - Move form event listener setup to _setupIndicatorForm() called from addToCharts() so it's always attached regardless of indicator data - Add immediate visual feedback: popup hides instantly on submit and button shows "Updating..." text Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
24fcb56c38
commit
7b796c51c8
|
|
@ -1382,6 +1382,20 @@ class BrighterTrades:
|
||||||
# Get indicator names - can be a list (from form checkboxes) or JSON string
|
# Get indicator names - can be a list (from form checkboxes) or JSON string
|
||||||
indicator_param = params.get('indicator', [])
|
indicator_param = params.get('indicator', [])
|
||||||
if isinstance(indicator_param, list):
|
if isinstance(indicator_param, list):
|
||||||
|
# Check if it's a list with a single JSON string element (from JavaScript FormData)
|
||||||
|
if len(indicator_param) == 1 and isinstance(indicator_param[0], str):
|
||||||
|
try:
|
||||||
|
# Try to parse as JSON array
|
||||||
|
parsed = json.loads(indicator_param[0])
|
||||||
|
if isinstance(parsed, list):
|
||||||
|
indicators_to_toggle = parsed
|
||||||
|
else:
|
||||||
|
indicators_to_toggle = indicator_param
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
# Not JSON, use as-is (single indicator name)
|
||||||
|
indicators_to_toggle = indicator_param
|
||||||
|
else:
|
||||||
|
# Multiple checkbox values from standard form submission
|
||||||
indicators_to_toggle = indicator_param
|
indicators_to_toggle = indicator_param
|
||||||
elif isinstance(indicator_param, str):
|
elif isinstance(indicator_param, str):
|
||||||
# Try to parse as JSON for backwards compatibility
|
# Try to parse as JSON for backwards compatibility
|
||||||
|
|
|
||||||
|
|
@ -597,6 +597,9 @@ class Indicators {
|
||||||
Receives indicator data, creates and stores the indicator
|
Receives indicator data, creates and stores the indicator
|
||||||
objects, then inserts the data into the charts.
|
objects, then inserts the data into the charts.
|
||||||
*/
|
*/
|
||||||
|
// Always set up the visibility form event handler
|
||||||
|
this._setupIndicatorForm();
|
||||||
|
|
||||||
if (idata.indicators && Object.keys(idata.indicators).length > 0) {
|
if (idata.indicators && Object.keys(idata.indicators).length > 0) {
|
||||||
this.create_indicators(idata.indicators, charts);
|
this.create_indicators(idata.indicators, charts);
|
||||||
// Initialize each indicator with the data directly
|
// Initialize each indicator with the data directly
|
||||||
|
|
@ -610,6 +613,15 @@ class Indicators {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_setupIndicatorForm(){
|
||||||
|
// Set up the visibility form event handler
|
||||||
|
const form = document.getElementById('indicator-form');
|
||||||
|
if (form && !form._hasSubmitListener) {
|
||||||
|
form.addEventListener('submit', this.handleFormSubmit.bind(this));
|
||||||
|
form._hasSubmitListener = true; // Prevent duplicate listeners
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
init_indicators(data){
|
init_indicators(data){
|
||||||
// Loop through all the indicators.
|
// Loop through all the indicators.
|
||||||
|
|
@ -621,11 +633,6 @@ class Indicators {
|
||||||
}
|
}
|
||||||
this.i_objs[name].init(data[name]);
|
this.i_objs[name].init(data[name]);
|
||||||
}
|
}
|
||||||
// Set up the visibility form event handler
|
|
||||||
const form = document.getElementById('indicator-form');
|
|
||||||
|
|
||||||
// Add a submit event listener to the form
|
|
||||||
form.addEventListener('submit', this.handleFormSubmit.bind(this));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This updates all the indicator data (re-initializes with full dataset)
|
// This updates all the indicator data (re-initializes with full dataset)
|
||||||
|
|
@ -899,6 +906,18 @@ class Indicators {
|
||||||
// Append all selected indicators as a single array-like structure
|
// Append all selected indicators as a single array-like structure
|
||||||
formData.append('indicator', JSON.stringify(selectedIndicators));
|
formData.append('indicator', JSON.stringify(selectedIndicators));
|
||||||
|
|
||||||
|
// Show loading feedback immediately
|
||||||
|
const submitBtn = form.querySelector('input[type="submit"]');
|
||||||
|
const originalValue = submitBtn.value;
|
||||||
|
submitBtn.value = 'Updating...';
|
||||||
|
submitBtn.disabled = true;
|
||||||
|
|
||||||
|
// Hide the popup and show a brief loading indicator
|
||||||
|
const popup = document.getElementById('indicators');
|
||||||
|
if (popup) {
|
||||||
|
popup.style.display = 'none';
|
||||||
|
}
|
||||||
|
|
||||||
// Send form data via AJAX (fetch)
|
// Send form data via AJAX (fetch)
|
||||||
fetch(form.action, {
|
fetch(form.action, {
|
||||||
method: form.method,
|
method: form.method,
|
||||||
|
|
@ -908,10 +927,15 @@ class Indicators {
|
||||||
// Handle success (you can reload the page or update the UI)
|
// Handle success (you can reload the page or update the UI)
|
||||||
window.location.reload();
|
window.location.reload();
|
||||||
} else {
|
} else {
|
||||||
|
// Restore button on error
|
||||||
|
submitBtn.value = originalValue;
|
||||||
|
submitBtn.disabled = false;
|
||||||
alert('Failed to update indicators.');
|
alert('Failed to update indicators.');
|
||||||
}
|
}
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
console.error('Error:', error);
|
console.error('Error:', error);
|
||||||
|
submitBtn.value = originalValue;
|
||||||
|
submitBtn.disabled = false;
|
||||||
alert('An error occurred while updating the indicators.');
|
alert('An error occurred while updating the indicators.');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue