Decouple the rest of an api calls

This commit is contained in:
SleepWalker
2016-07-28 08:03:30 +03:00
parent 5fdb662934
commit 3804c1c143
3 changed files with 76 additions and 36 deletions

View File

@@ -1,9 +1,9 @@
import { routeActions } from 'react-router-redux'; import { routeActions } from 'react-router-redux';
import { updateUser, logout as logoutUser, changePassword as changeUserPassword, authenticate } from 'components/user/actions'; 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 authentication from 'services/api/authentication';
import oauth from 'services/api/oauth'; import oauth from 'services/api/oauth';
import signup from 'services/api/signup';
export function login({login = '', password = '', rememberMe = false}) { export function login({login = '', password = '', rememberMe = false}) {
const PASSWORD_REQUIRED = 'error.password_required'; const PASSWORD_REQUIRED = 'error.password_required';
@@ -50,10 +50,7 @@ export function forgotPassword({
login = '' login = ''
}) { }) {
return wrapInLoader((dispatch, getState) => return wrapInLoader((dispatch, getState) =>
request.post( authentication.forgotPassword({login})
'/api/authentication/forgot-password',
{login}
)
.then(({data = {}}) => dispatch(updateUser({ .then(({data = {}}) => dispatch(updateUser({
maskedEmail: data.emailMask || getState().user.email maskedEmail: data.emailMask || getState().user.email
}))) })))
@@ -67,10 +64,7 @@ export function recoverPassword({
newRePassword = '' newRePassword = ''
}) { }) {
return wrapInLoader((dispatch) => return wrapInLoader((dispatch) =>
request.post( authentication.recoverPassword({key, newPassword, newRePassword})
'/api/authentication/recover-password',
{key, newPassword, newRePassword}
)
.then(authHandler(dispatch)) .then(authHandler(dispatch))
.catch(validationErrorsHandler(dispatch, '/forgot-password')) .catch(validationErrorsHandler(dispatch, '/forgot-password'))
); );
@@ -84,10 +78,11 @@ export function register({
rulesAgreement = false rulesAgreement = false
}) { }) {
return wrapInLoader((dispatch, getState) => return wrapInLoader((dispatch, getState) =>
request.post( signup.register({
'/api/signup', email, username,
{email, username, password, rePassword, rulesAgreement, lang: getState().user.lang} password, rePassword,
) rulesAgreement, lang: getState().user.lang
})
.then(() => { .then(() => {
dispatch(updateUser({ dispatch(updateUser({
username, username,
@@ -102,10 +97,7 @@ export function register({
export function activate({key = ''}) { export function activate({key = ''}) {
return wrapInLoader((dispatch) => return wrapInLoader((dispatch) =>
request.post( signup.activate({key})
'/api/signup/confirm',
{key}
)
.then(authHandler(dispatch)) .then(authHandler(dispatch))
.catch(validationErrorsHandler(dispatch, '/resend-activation')) .catch(validationErrorsHandler(dispatch, '/resend-activation'))
); );
@@ -113,10 +105,7 @@ export function activate({key = ''}) {
export function resendActivation({email = ''}) { export function resendActivation({email = ''}) {
return wrapInLoader((dispatch) => return wrapInLoader((dispatch) =>
request.post( signup.resendActivation({email})
'/api/signup/repeat-message',
{email}
)
.then((resp) => { .then((resp) => {
dispatch(updateUser({ dispatch(updateUser({
email email

View File

@@ -16,6 +16,26 @@ export default {
return request.post('/api/authentication/logout'); 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) { refreshToken(refresh_token) {
return request.post( return request.post(
'/api/authentication/refresh-token', '/api/authentication/refresh-token',

View File

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