Skip to content

Commit f977ff9

Browse files
authored
Fix Wi-Fi network configuration in settings server and improve UI (#103)
* Fix Wi-Fi network configuration in settings server The settings UI was correctly updating Wi-Fi networks in Redis, but not properly calling wifi.save_networks() which ensures proper handling before updating wpa_supplicant configuration. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * Add additional debugging for Wi-Fi configuration issue * Simplify debugging by removing try-except blocks * Improve Wi-Fi configuration in settings UI - Remove debug code from server.py - Fix Wi-Fi network save process by using proper wifi.save_networks() function - Add unsaved changes indicator to Save button in settings UI - Disable Save button when there are no changes to save * Refactor WiFi settings saving to avoid redundant calls * Simplify WiFi network handling in settings server * Pass value directly to save_networks without JSON parsing
1 parent a50b4e3 commit f977ff9

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

gonotego/settings-server/src/App.tsx

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ const SettingsUI = () => {
5555
CUSTOM_COMMAND_PATHS: [],
5656
});
5757

58+
// Keep track of the original settings to detect changes
59+
const [originalSettings, setOriginalSettings] = useState({});
60+
5861
const [newWifiNetwork, setNewWifiNetwork] = useState({
5962
ssid: '',
6063
psk: ''
@@ -90,10 +93,14 @@ const SettingsUI = () => {
9093
}, {});
9194

9295
// Update settings state with fetched data
93-
setSettings(prev => ({
94-
...prev,
96+
const updatedSettings = {
97+
...settings,
9598
...validSettings
96-
}));
99+
};
100+
setSettings(updatedSettings);
101+
102+
// Store the original settings to detect changes
103+
setOriginalSettings(JSON.parse(JSON.stringify(updatedSettings)));
97104
} catch (error) {
98105
console.error('Error fetching settings:', error);
99106
setLoadError(true);
@@ -105,6 +112,20 @@ const SettingsUI = () => {
105112
fetchSettings();
106113
}, []);
107114

115+
// Function to detect if there are unsaved changes
116+
const hasUnsavedChanges = () => {
117+
// Check if the settings object has the same structure as originalSettings
118+
if (Object.keys(originalSettings).length === 0) return false;
119+
120+
// Compare original and current settings using deep comparison
121+
try {
122+
return JSON.stringify(settings) !== JSON.stringify(originalSettings);
123+
} catch (e) {
124+
// If comparison fails (e.g., circular references), default to true
125+
return true;
126+
}
127+
};
128+
108129
const handleChange = (key, value) => {
109130
setSettings(prev => ({
110131
...prev,
@@ -135,6 +156,10 @@ const SettingsUI = () => {
135156
}
136157

137158
setSaveStatus('saved');
159+
160+
// Update the original settings after saving
161+
setOriginalSettings(JSON.parse(JSON.stringify(settings)));
162+
138163
setTimeout(() => setSaveStatus(null), 2000);
139164
} catch (error) {
140165
console.error('Error saving settings:', error);
@@ -736,15 +761,15 @@ const SettingsUI = () => {
736761
)}
737762
<Button
738763
onClick={handleSave}
739-
className="w-32"
740-
disabled={saveStatus === 'saving'}
764+
className={`w-32 ${hasUnsavedChanges() ? 'bg-blue-600 hover:bg-blue-700' : 'bg-slate-300 hover:bg-slate-400'}`}
765+
disabled={saveStatus === 'saving' || !hasUnsavedChanges()}
741766
>
742767
{saveStatus === 'saving' ? (
743768
'Saving...'
744769
) : (
745770
<>
746771
<Save className="h-4 w-4 mr-2" />
747-
Save
772+
{hasUnsavedChanges() ? 'Save *' : 'Save'}
748773
</>
749774
)}
750775
</Button>

gonotego/settings/server.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ def do_GET(self):
155155
def do_POST(self):
156156
"""Handle POST requests."""
157157
parsed_path = urlparse(self.path)
158-
159158
if parsed_path.path == "/api/settings":
160159
try:
161160
# Read the request body
@@ -174,15 +173,14 @@ def do_POST(self):
174173
continue
175174

176175
try:
177-
settings.set(key, value)
178-
# If we're updating WiFi networks, update the wpa_supplicant.conf file
176+
# Handle WiFi networks specially
179177
if key == 'WIFI_NETWORKS':
180-
try:
181-
# Update wpa_supplicant configuration using the wifi module
182-
wifi.update_wpa_supplicant_config()
183-
wifi.reconfigure_wifi()
184-
except Exception as e:
185-
print(f"Error updating WiFi configuration: {e}")
178+
wifi.save_networks(value)
179+
wifi.update_wpa_supplicant_config()
180+
wifi.reconfigure_wifi()
181+
else:
182+
# For all other settings, just use settings.set
183+
settings.set(key, value)
186184
except Exception as e:
187185
print(f"Error setting {key}: {e}")
188186

0 commit comments

Comments
 (0)