From 3804c1c1434ced50c840385b3d96a66156db62c1 Mon Sep 17 00:00:00 2001 From: SleepWalker Date: Thu, 28 Jul 2016 08:03:30 +0300 Subject: [PATCH] Decouple the rest of an api calls --- src/components/auth/actions.js | 61 ++++++++++++------------------ src/services/api/authentication.js | 20 ++++++++++ src/services/api/signup.js | 31 +++++++++++++++ 3 files changed, 76 insertions(+), 36 deletions(-) create mode 100644 src/services/api/signup.js diff --git a/src/components/auth/actions.js b/src/components/auth/actions.js index 3db5ecf..6f4cb84 100644 --- a/src/components/auth/actions.js +++ b/src/components/auth/actions.js @@ -1,9 +1,9 @@ import { routeActions } from 'react-router-redux'; import { updateUser, logout as logoutUser, changePassword as changeUserPassword, authenticate } from 'components/user/actions'; -import request from 'services/request'; import authentication from 'services/api/authentication'; import oauth from 'services/api/oauth'; +import signup from 'services/api/signup'; export function login({login = '', password = '', rememberMe = false}) { const PASSWORD_REQUIRED = 'error.password_required'; @@ -50,14 +50,11 @@ export function forgotPassword({ login = '' }) { return wrapInLoader((dispatch, getState) => - request.post( - '/api/authentication/forgot-password', - {login} - ) - .then(({data = {}}) => dispatch(updateUser({ - maskedEmail: data.emailMask || getState().user.email - }))) - .catch(validationErrorsHandler(dispatch)) + authentication.forgotPassword({login}) + .then(({data = {}}) => dispatch(updateUser({ + maskedEmail: data.emailMask || getState().user.email + }))) + .catch(validationErrorsHandler(dispatch)) ); } @@ -67,12 +64,9 @@ export function recoverPassword({ newRePassword = '' }) { return wrapInLoader((dispatch) => - request.post( - '/api/authentication/recover-password', - {key, newPassword, newRePassword} - ) - .then(authHandler(dispatch)) - .catch(validationErrorsHandler(dispatch, '/forgot-password')) + authentication.recoverPassword({key, newPassword, newRePassword}) + .then(authHandler(dispatch)) + .catch(validationErrorsHandler(dispatch, '/forgot-password')) ); } @@ -84,10 +78,11 @@ export function register({ rulesAgreement = false }) { return wrapInLoader((dispatch, getState) => - request.post( - '/api/signup', - {email, username, password, rePassword, rulesAgreement, lang: getState().user.lang} - ) + signup.register({ + email, username, + password, rePassword, + rulesAgreement, lang: getState().user.lang + }) .then(() => { dispatch(updateUser({ username, @@ -102,29 +97,23 @@ export function register({ export function activate({key = ''}) { return wrapInLoader((dispatch) => - request.post( - '/api/signup/confirm', - {key} - ) - .then(authHandler(dispatch)) - .catch(validationErrorsHandler(dispatch, '/resend-activation')) + signup.activate({key}) + .then(authHandler(dispatch)) + .catch(validationErrorsHandler(dispatch, '/resend-activation')) ); } export function resendActivation({email = ''}) { return wrapInLoader((dispatch) => - request.post( - '/api/signup/repeat-message', - {email} - ) - .then((resp) => { - dispatch(updateUser({ - email - })); + signup.resendActivation({email}) + .then((resp) => { + dispatch(updateUser({ + email + })); - return resp; - }) - .catch(validationErrorsHandler(dispatch)) + return resp; + }) + .catch(validationErrorsHandler(dispatch)) ); } diff --git a/src/services/api/authentication.js b/src/services/api/authentication.js index 863f68f..3f9320c 100644 --- a/src/services/api/authentication.js +++ b/src/services/api/authentication.js @@ -16,6 +16,26 @@ export default { return request.post('/api/authentication/logout'); }, + forgotPassword({ + login = '' + }) { + return request.post( + '/api/authentication/forgot-password', + {login} + ); + }, + + recoverPassword({ + key = '', + newPassword = '', + newRePassword = '' + }) { + return request.post( + '/api/authentication/recover-password', + {key, newPassword, newRePassword} + ); + }, + refreshToken(refresh_token) { return request.post( '/api/authentication/refresh-token', diff --git a/src/services/api/signup.js b/src/services/api/signup.js new file mode 100644 index 0000000..ce3069b --- /dev/null +++ b/src/services/api/signup.js @@ -0,0 +1,31 @@ +import request from 'services/request'; + +export default { + register({ + email = '', + username = '', + password = '', + rePassword = '', + rulesAgreement = false, + lang = '' + }) { + return request.post( + '/api/signup', + {email, username, password, rePassword, rulesAgreement, lang} + ); + }, + + activate({key = ''}) { + return request.post( + '/api/signup/confirm', + {key} + ); + }, + + resendActivation({email = ''}) { + return request.post( + '/api/signup/repeat-message', + {email} + ); + } +};