Skip to content

Commit

Permalink
added timeout when save sites hangs
Browse files Browse the repository at this point in the history
  • Loading branch information
Codebmk committed Oct 25, 2024
1 parent ccd0cbf commit 3f961fd
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions netmanager/src/views/pages/Preferences/Preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ const useStyles = makeStyles((theme) => ({
}
}));

const SAVE_TIMEOUT = 30000; // 30 seconds timeout

const Preferences = () => {
const classes = useStyles();
const dispatch = useDispatch();
Expand Down Expand Up @@ -242,16 +244,23 @@ const Preferences = () => {
const handleSaveSites = async () => {
setIsSaving(true);
setError(null);

const saveOperation = setUserPreferencesApi([...selectedSites, ...newSelectedSites]);
const timeoutPromise = new Promise((_, reject) =>
setTimeout(() => reject(new Error('Operation timed out')), SAVE_TIMEOUT)
);

try {
const updatedSites = [...selectedSites, ...newSelectedSites];
await setUserPreferencesApi(updatedSites);
setSelectedSites(updatedSites);
await Promise.race([saveOperation, timeoutPromise]);
setSelectedSites([...selectedSites, ...newSelectedSites]);
setModalOpen(false);
showSnackbar('Sites updated successfully');
} catch (error) {
console.error('Error updating sites:', error);
const errorMessage =
error.response?.data?.message || error.message || 'An unknown error occurred';
error.message === 'Operation timed out'
? 'The operation timed out. Please try again.'
: error.response?.data?.message || error.message || 'An unknown error occurred';
setError(`Failed to update sites: ${errorMessage}`);
showSnackbar(`Error updating sites: ${errorMessage}`, 'error');
} finally {
Expand Down

0 comments on commit 3f961fd

Please sign in to comment.