Skip to content

Commit fd227fa

Browse files
committed
Refactor save functionality in SettingsForms to utilize a dedicated save button for each app type. Implement error handling and success notifications for saving settings, enhancing user experience and feedback during the save process.
1 parent 99ac4f1 commit fd227fa

File tree

1 file changed

+55
-24
lines changed

1 file changed

+55
-24
lines changed

frontend/static/js/settings_forms.js

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const SettingsForms = {
5858
// Add save button at the top
5959
let sonarrSaveButtonHtml = `
6060
<div style="margin-bottom: 20px;">
61-
<button type="button" id="saveSonarrButton" disabled style="
61+
<button type="button" id="sonarr-save-button" disabled style="
6262
background: #6b7280;
6363
color: #9ca3af;
6464
border: 1px solid #4b5563;
@@ -577,7 +577,7 @@ const SettingsForms = {
577577
// Add save button at the top
578578
let radarrSaveButtonHtml = `
579579
<div style="margin-bottom: 20px;">
580-
<button type="button" id="saveRadarrButton" disabled style="
580+
<button type="button" id="radarr-save-button" disabled style="
581581
background: #6b7280;
582582
color: #9ca3af;
583583
border: 1px solid #4b5563;
@@ -933,7 +933,7 @@ const SettingsForms = {
933933
// Add save button at the top
934934
let lidarrSaveButtonHtml = `
935935
<div style="margin-bottom: 20px;">
936-
<button type="button" id="saveLidarrButton" disabled style="
936+
<button type="button" id="lidarr-save-button" disabled style="
937937
background: #6b7280;
938938
color: #9ca3af;
939939
border: 1px solid #4b5563;
@@ -2957,30 +2957,61 @@ const SettingsForms = {
29572957
saveButton.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Saving...';
29582958
saveButton.disabled = true;
29592959

2960-
// Use the apps module save functionality
2961-
if (window.appsModule && window.appsModule.saveAppSettings) {
2962-
const appPanel = container.closest('.app-apps-panel') || document.getElementById(`${appType}Apps`);
2963-
if (appPanel) {
2964-
window.appsModule.saveAppSettings(appType, appPanel);
2965-
2966-
// Wait a bit then reset button state (the apps module will handle success/error)
2967-
setTimeout(() => {
2968-
saveButton.innerHTML = '<i class="fas fa-save"></i> Save Changes';
2969-
updateSaveButtonState(false);
2970-
2971-
// Update baseline after successful save
2972-
captureFormBaseline();
2973-
}, 1000);
2974-
} else {
2975-
console.error(`[SettingsForms] Could not find app panel for ${appType}`);
2976-
saveButton.innerHTML = '<i class="fas fa-save"></i> Save Changes';
2977-
updateSaveButtonState(hasChanges);
2978-
}
2979-
} else {
2980-
console.error('[SettingsForms] Apps module save function not available');
2960+
// Collect form settings
2961+
let settings;
2962+
try {
2963+
settings = SettingsForms.getFormSettings(container, appType);
2964+
console.log(`[SettingsForms] Collected ${appType} settings:`, settings);
2965+
} catch (error) {
2966+
console.error(`[SettingsForms] Error collecting ${appType} settings:`, error);
29812967
saveButton.innerHTML = '<i class="fas fa-save"></i> Save Changes';
29822968
updateSaveButtonState(hasChanges);
2969+
return;
29832970
}
2971+
2972+
// Save settings to server
2973+
HuntarrUtils.fetchWithTimeout(`./api/settings/${appType}`, {
2974+
method: 'POST',
2975+
headers: {
2976+
'Content-Type': 'application/json'
2977+
},
2978+
body: JSON.stringify(settings)
2979+
})
2980+
.then(response => {
2981+
if (!response.ok) {
2982+
throw new Error(`HTTP error ${response.status}: ${response.statusText}`);
2983+
}
2984+
return response.json();
2985+
})
2986+
.then(data => {
2987+
console.log(`[SettingsForms] ${appType} settings saved successfully:`, data);
2988+
2989+
// Reset button state and clear unsaved changes warning
2990+
saveButton.innerHTML = '<i class="fas fa-save"></i> Save Changes';
2991+
updateSaveButtonState(false);
2992+
2993+
// Update baseline after successful save
2994+
captureFormBaseline();
2995+
2996+
// Show success notification
2997+
if (typeof huntarrUI !== 'undefined' && typeof huntarrUI.showNotification === 'function') {
2998+
huntarrUI.showNotification(`${appType.charAt(0).toUpperCase() + appType.slice(1)} settings saved successfully`, 'success');
2999+
}
3000+
})
3001+
.catch(error => {
3002+
console.error(`[SettingsForms] Error saving ${appType} settings:`, error);
3003+
3004+
// Reset button state but keep changes detected
3005+
saveButton.innerHTML = '<i class="fas fa-save"></i> Save Changes';
3006+
updateSaveButtonState(hasChanges);
3007+
3008+
// Show error notification
3009+
if (typeof huntarrUI !== 'undefined' && typeof huntarrUI.showNotification === 'function') {
3010+
huntarrUI.showNotification(`Error saving ${appType} settings: ${error.message}`, 'error');
3011+
} else {
3012+
alert(`Error saving ${appType} settings: ${error.message}`);
3013+
}
3014+
});
29843015
});
29853016

29863017
// Function to capture current form state as baseline

0 commit comments

Comments
 (0)