From c8dcbb180a0b5d8864272ae2e79ab14288ceda08 Mon Sep 17 00:00:00 2001 From: Linda Paiste Date: Sun, 23 Jul 2023 13:58:53 -0500 Subject: [PATCH] Delete unnecessary `return new Promise` in Redux actions. --- client/modules/IDE/actions/files.js | 64 +++++++------- client/modules/User/actions.js | 130 ++++++++++++---------------- 2 files changed, 85 insertions(+), 109 deletions(-) diff --git a/client/modules/IDE/actions/files.js b/client/modules/IDE/actions/files.js index f87f5d1c6d..9fe067323c 100644 --- a/client/modules/IDE/actions/files.js +++ b/client/modules/IDE/actions/files.js @@ -89,25 +89,22 @@ export function handleCreateFile(formProps, setSelected = true) { const { files } = state; const { parentId } = state.ide; const projectId = state.project.id; - return new Promise((resolve) => { - submitFile(formProps, files, parentId, projectId) - .then((response) => { - const { file, updatedAt } = response; - dispatch(createFile(file, parentId)); - if (updatedAt) dispatch(setProjectSavedTime(updatedAt)); - dispatch(closeNewFileModal()); - dispatch(setUnsavedChanges(true)); - if (setSelected) { - dispatch(setSelectedFile(file.id)); - } - resolve(); - }) - .catch((error) => { - const { response } = error; - dispatch(createError(response.data)); - resolve({ error }); - }); - }); + submitFile(formProps, files, parentId, projectId) + .then((response) => { + const { file, updatedAt } = response; + dispatch(createFile(file, parentId)); + if (updatedAt) dispatch(setProjectSavedTime(updatedAt)); + dispatch(closeNewFileModal()); + dispatch(setUnsavedChanges(true)); + if (setSelected) { + dispatch(setSelectedFile(file.id)); + } + }) + .catch((error) => { + const { response } = error; + dispatch(createError(response.data)); + return { error }; + }); }; } @@ -149,22 +146,19 @@ export function handleCreateFolder(formProps) { const { files } = state; const { parentId } = state.ide; const projectId = state.project.id; - return new Promise((resolve) => { - submitFolder(formProps, files, parentId, projectId) - .then((response) => { - const { file, updatedAt } = response; - dispatch(createFile(file, parentId)); - if (updatedAt) dispatch(setProjectSavedTime(updatedAt)); - dispatch(closeNewFolderModal()); - dispatch(setUnsavedChanges(true)); - resolve(); - }) - .catch((error) => { - const { response } = error; - dispatch(createError(response.data)); - resolve({ error }); - }); - }); + return submitFolder(formProps, files, parentId, projectId) + .then((response) => { + const { file, updatedAt } = response; + dispatch(createFile(file, parentId)); + if (updatedAt) dispatch(setProjectSavedTime(updatedAt)); + dispatch(closeNewFolderModal()); + dispatch(setUnsavedChanges(true)); + }) + .catch((error) => { + const { response } = error; + dispatch(createError(response.data)); + return { error }; + }); }; } diff --git a/client/modules/User/actions.js b/client/modules/User/actions.js index 826ade8676..d551da0c5b 100644 --- a/client/modules/User/actions.js +++ b/client/modules/User/actions.js @@ -46,26 +46,21 @@ export function validateAndLoginUser(formProps) { return (dispatch, getState) => { const state = getState(); const { previousPath } = state.ide; - return new Promise((resolve) => { - loginUser(formProps) - .then((response) => { - dispatch(authenticateUser(response.data)); - dispatch(setPreferences(response.data.preferences)); - dispatch( - setLanguage(response.data.preferences.language, { - persistPreference: false - }) - ); - dispatch(justOpenedProject()); - browserHistory.push(previousPath); - resolve(); - }) - .catch((error) => - resolve({ - [FORM_ERROR]: error.response.data.message + return loginUser(formProps) + .then((response) => { + dispatch(authenticateUser(response.data)); + dispatch(setPreferences(response.data.preferences)); + dispatch( + setLanguage(response.data.preferences.language, { + persistPreference: false }) ); - }); + dispatch(justOpenedProject()); + browserHistory.push(previousPath); + }) + .catch((error) => ({ + [FORM_ERROR]: error.response.data.message + })); }; } @@ -73,20 +68,17 @@ export function validateAndSignUpUser(formValues) { return (dispatch, getState) => { const state = getState(); const { previousPath } = state.ide; - return new Promise((resolve) => { - signUpUser(formValues) - .then((response) => { - dispatch(authenticateUser(response.data)); - dispatch(justOpenedProject()); - browserHistory.push(previousPath); - resolve(); - }) - .catch((error) => { - const { response } = error; - dispatch(authError(response.data.error)); - resolve({ error }); - }); - }); + return signUpUser(formValues) + .then((response) => { + dispatch(authenticateUser(response.data)); + dispatch(justOpenedProject()); + browserHistory.push(previousPath); + }) + .catch((error) => { + const { response } = error; + dispatch(authError(response.data.error)); + return { error }; + }); }; } @@ -159,23 +151,19 @@ export function logoutUser() { } export function initiateResetPassword(formValues) { - return (dispatch) => - new Promise((resolve) => { + return (dispatch) => { + dispatch({ + type: ActionTypes.RESET_PASSWORD_INITIATE + }); + return apiClient.post('/reset-password', formValues).catch((error) => { + const { response } = error; dispatch({ - type: ActionTypes.RESET_PASSWORD_INITIATE + type: ActionTypes.ERROR, + message: response.data }); - return apiClient - .post('/reset-password', formValues) - .then(() => resolve()) - .catch((error) => { - const { response } = error; - dispatch({ - type: ActionTypes.ERROR, - message: response.data - }); - resolve({ error }); - }); + return { error }; }); + }; } export function initiateVerification() { @@ -183,7 +171,7 @@ export function initiateVerification() { dispatch({ type: ActionTypes.EMAIL_VERIFICATION_INITIATE }); - apiClient + return apiClient .post('/verify/send', {}) .then(() => { // do nothing @@ -245,21 +233,18 @@ export function validateResetPasswordToken(token) { export function updatePassword(formValues, token) { return (dispatch) => - new Promise((resolve) => - apiClient - .post(`/reset-password/${token}`, formValues) - .then((response) => { - dispatch(authenticateUser(response.data)); - browserHistory.push('/'); - resolve(); - }) - .catch((error) => { - dispatch({ - type: ActionTypes.INVALID_RESET_PASSWORD_TOKEN - }); - resolve({ error }); - }) - ); + apiClient + .post(`/reset-password/${token}`, formValues) + .then((response) => { + dispatch(authenticateUser(response.data)); + browserHistory.push('/'); + }) + .catch((error) => { + dispatch({ + type: ActionTypes.INVALID_RESET_PASSWORD_TOKEN + }); + return { error }; + }); } export function updateSettingsSuccess(user) { @@ -275,16 +260,13 @@ export function submitSettings(formValues) { export function updateSettings(formValues) { return (dispatch) => - new Promise((resolve) => - submitSettings(formValues) - .then((response) => { - dispatch(updateSettingsSuccess(response.data)); - dispatch(showToast(5500)); - dispatch(setToastText('Toast.SettingsSaved')); - resolve(); - }) - .catch((error) => resolve({ error })) - ); + submitSettings(formValues) + .then((response) => { + dispatch(updateSettingsSuccess(response.data)); + dispatch(showToast(5500)); + dispatch(setToastText('Toast.SettingsSaved')); + }) + .catch((error) => ({ error })); } export function createApiKeySuccess(user) { @@ -303,7 +285,7 @@ export function createApiKey(label) { }) .catch((error) => { const { response } = error; - Promise.reject(new Error(response.data.error)); + return new Error(response.data.error); }); } @@ -319,7 +301,7 @@ export function removeApiKey(keyId) { }) .catch((error) => { const { response } = error; - Promise.reject(new Error(response.data.error)); + return new Error(response.data.error); }); }