From e90fc1599e7887d8412be790fa8f505e7582523a Mon Sep 17 00:00:00 2001 From: Luceium Date: Mon, 6 Nov 2023 23:51:21 -0800 Subject: [PATCH 1/2] catches errors when saving data and ends loading --- src/store/form/actions.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/store/form/actions.ts b/src/store/form/actions.ts index bcb9fe09..5c06df5d 100755 --- a/src/store/form/actions.ts +++ b/src/store/form/actions.ts @@ -51,6 +51,10 @@ export const saveData = () => (dispatch, getState) => { dispatch(setData(e, false)); dispatch(loadingEnd()); dispatch(setUserEdited(false)); + }).catch(e => { // catches 401 and other errors + console.error(e); + dispatch(loadingEnd()); + alert("Error saving data " + e); }); } From af55147e4eec7f2282d8ba816781c4d2b337979f Mon Sep 17 00:00:00 2001 From: Luceium Date: Tue, 7 Nov 2023 00:01:48 -0800 Subject: [PATCH 2/2] removes redundant loadingEnd() calls and moves them too .finall() --- src/store/form/actions.ts | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/store/form/actions.ts b/src/store/form/actions.ts index 5c06df5d..338b0f7e 100755 --- a/src/store/form/actions.ts +++ b/src/store/form/actions.ts @@ -34,11 +34,11 @@ export const getUserProfile = () => (dispatch, getState) => { dispatch(loadingStart()); return API.get("treehacks", `/users/${userId}`, {}).then(e => { dispatch(setUserProfile(e)); - dispatch(loadingEnd()); }).catch(e => { console.error(e); - dispatch(loadingEnd()); alert("Error getting user data " + e); + }).finally(() => { + dispatch(loadingEnd()); }); }; @@ -49,12 +49,12 @@ export const saveData = () => (dispatch, getState) => { dispatch(loadingStart()); return API.put("treehacks", `/users/${userId}/forms/${formName}`, { "body": formData }).then(e => { dispatch(setData(e, false)); - dispatch(loadingEnd()); dispatch(setUserEdited(false)); }).catch(e => { // catches 401 and other errors console.error(e); - dispatch(loadingEnd()); alert("Error saving data " + e); + }).finally(() => { + dispatch(loadingEnd()); }); } @@ -63,11 +63,11 @@ export const submitForm = () => (dispatch, getState) => { const formName = (getState().form as IFormState).formName; dispatch(loadingStart()); return API.post("treehacks", `/users/${userId}/forms/${formName}/submit`, {}).then(e => { - dispatch(loadingEnd()); }).catch(e => { console.error(e); - dispatch(loadingEnd()); alert("Error submitting form " + e); + }).finally(() => { + dispatch(loadingEnd()); }); } @@ -76,12 +76,12 @@ export const loadData = (userId = null) => (dispatch, getState) => { const formName = (getState().form as IFormState).formName; dispatch(loadingStart()); return API.get("treehacks", `/users/${userId}/forms/${formName}`, {}).then(e => { - dispatch(loadingEnd()); dispatch(setData(e)); }).catch(e => { console.error(e); - dispatch(loadingEnd()); alert("Error getting data " + e); + }).finally(() => { + dispatch(loadingEnd()); }); } @@ -92,8 +92,9 @@ export const confirmAdmission = () => (dispatch, getState) => { dispatch(getUserProfile()); }).catch(e => { console.error(e); - dispatch(loadingEnd()); alert("Error " + e); + }).finally(() => { + dispatch(loadingEnd()); }); } @@ -104,7 +105,8 @@ export const declineAdmission = () => (dispatch, getState) => { dispatch(getUserProfile()); }).catch(e => { console.error(e); - dispatch(loadingEnd()); alert("Error " + e); + }).finally(() => { + dispatch(loadingEnd()); }); } \ No newline at end of file