Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete unnecessary return new Promise in Redux actions. #2321

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 29 additions & 35 deletions client/modules/IDE/actions/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think return is missing here, otherwise all the changes looks good to me. Nicely Done.

.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 };
});
};
}

Expand Down Expand Up @@ -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 };
});
};
}

Expand Down
130 changes: 56 additions & 74 deletions client/modules/User/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,47 +46,39 @@ 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
}));
};
}

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 };
});
};
}

Expand Down Expand Up @@ -159,31 +151,27 @@ 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() {
return (dispatch) => {
dispatch({
type: ActionTypes.EMAIL_VERIFICATION_INITIATE
});
apiClient
return apiClient
.post('/verify/send', {})
.then(() => {
// do nothing
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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);
});
}

Expand All @@ -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);
});
}

Expand Down
Loading