accounts-frontend/src/components/user/actions.js

111 lines
2.7 KiB
JavaScript
Raw Normal View History

2016-05-01 16:01:40 +05:30
import { routeActions } from 'react-router-redux';
import captcha from 'services/captcha';
import accounts from 'services/api/accounts';
import authentication from 'services/api/authentication';
2016-05-20 01:11:43 +05:30
import { setLocale } from 'components/i18n/actions';
2016-02-26 11:55:47 +05:30
export const UPDATE = 'USER_UPDATE';
/**
* @param {string|object} payload jwt token or user object
* @return {object} action definition
*/
export function updateUser(payload) {
return {
type: UPDATE,
payload
};
}
2016-05-20 01:11:43 +05:30
export const CHANGE_LANG = 'USER_CHANGE_LANG';
export function changeLang(lang) {
return (dispatch, getState) => dispatch(setLocale(lang))
.then((lang) => {
const {user: {isGuest, lang: oldLang}} = getState();
if (!isGuest && oldLang !== lang) {
accounts.changeLang(lang);
}
// TODO: probably should be moved from here, because it is side effect
captcha.setLang(lang);
2016-05-20 01:11:43 +05:30
dispatch({
type: CHANGE_LANG,
payload: {
lang
}
});
});
}
export const SET = 'USER_SET';
export function setUser(payload) {
return {
type: SET,
payload
};
}
export function logout() {
return (dispatch, getState) => {
if (getState().user.token) {
authentication.logout();
}
return new Promise((resolve) => {
setTimeout(() => { // a tiny timeout to allow logout before user's token will be removed
dispatch(setUser({
lang: getState().user.lang,
isGuest: true
}));
dispatch(routeActions.push('/login'));
resolve();
}, 0);
});
};
}
2016-02-26 11:55:47 +05:30
export function fetchUserData() {
return (dispatch) =>
accounts.current()
.then((resp) => {
dispatch(updateUser(resp));
2016-05-20 01:11:43 +05:30
return dispatch(changeLang(resp.lang));
});
2016-02-26 11:55:47 +05:30
}
2016-08-03 00:29:29 +05:30
export function acceptRules() {
return (dispatch) =>
accounts.acceptRules()
.then((resp) => {
dispatch(updateUser({
shouldAcceptRules: false
}));
return resp;
})
;
}
export function authenticate(token, refreshToken) { // TODO: this action, probably, belongs to components/auth
return (dispatch, getState) => {
refreshToken = refreshToken || getState().user.refreshToken;
dispatch(updateUser({
token,
refreshToken
}));
return dispatch(fetchUserData()).then((resp) => {
dispatch(updateUser({
isGuest: false
}));
return resp;
});
};
}