2016-05-01 13:31:40 +03:00
|
|
|
import { routeActions } from 'react-router-redux';
|
|
|
|
|
2016-05-01 13:28:54 +03:00
|
|
|
import accounts from 'services/api/accounts';
|
2016-11-14 07:28:25 +02:00
|
|
|
import { reset as resetAccounts } from 'components/accounts/actions';
|
2016-06-28 13:13:27 +03:00
|
|
|
import authentication from 'services/api/authentication';
|
2016-05-19 22:41:43 +03:00
|
|
|
import { setLocale } from 'components/i18n/actions';
|
2016-02-26 08:25:47 +02:00
|
|
|
|
2016-02-13 17:28:47 +02:00
|
|
|
export const UPDATE = 'USER_UPDATE';
|
|
|
|
/**
|
2016-11-05 12:11:41 +02:00
|
|
|
* Merge data into user's state
|
|
|
|
*
|
|
|
|
* @param {object} payload
|
|
|
|
* @return {object} - action definition
|
2016-02-13 17:28:47 +02:00
|
|
|
*/
|
|
|
|
export function updateUser(payload) {
|
|
|
|
return {
|
|
|
|
type: UPDATE,
|
|
|
|
payload
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-05-19 22:41:43 +03:00
|
|
|
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();
|
|
|
|
|
2016-11-05 12:11:41 +02:00
|
|
|
if (oldLang !== lang) {
|
|
|
|
!isGuest && accounts.changeLang(lang);
|
2016-07-31 16:53:16 +03:00
|
|
|
|
2016-11-05 12:11:41 +02:00
|
|
|
dispatch({
|
|
|
|
type: CHANGE_LANG,
|
|
|
|
payload: {
|
|
|
|
lang
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-05-19 22:41:43 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-13 17:28:47 +02:00
|
|
|
export const SET = 'USER_SET';
|
2016-11-05 12:11:41 +02:00
|
|
|
/**
|
|
|
|
* Replace current user's state with a new one
|
|
|
|
*
|
|
|
|
* @param {User} payload
|
|
|
|
* @return {object} - action definition
|
|
|
|
*/
|
2016-02-13 17:28:47 +02:00
|
|
|
export function setUser(payload) {
|
|
|
|
return {
|
|
|
|
type: SET,
|
|
|
|
payload
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function logout() {
|
2016-08-07 20:26:29 +03:00
|
|
|
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
|
|
|
|
}));
|
|
|
|
|
2016-11-14 07:28:25 +02:00
|
|
|
dispatch(resetAccounts());
|
|
|
|
|
2016-08-07 20:26:29 +03:00
|
|
|
dispatch(routeActions.push('/login'));
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
}, 0);
|
2016-07-28 22:51:47 +03:00
|
|
|
});
|
2016-08-07 20:26:29 +03:00
|
|
|
};
|
2016-02-13 17:28:47 +02:00
|
|
|
}
|
2016-02-26 08:25:47 +02:00
|
|
|
|
|
|
|
export function fetchUserData() {
|
2016-06-04 19:54:42 +03:00
|
|
|
return (dispatch) =>
|
2016-05-01 13:28:54 +03:00
|
|
|
accounts.current()
|
2016-06-04 17:58:29 +03:00
|
|
|
.then((resp) => {
|
2016-11-05 12:11:41 +02:00
|
|
|
dispatch(updateUser({
|
|
|
|
isGuest: false,
|
|
|
|
...resp
|
|
|
|
}));
|
2016-05-19 22:41:43 +03:00
|
|
|
|
2016-06-04 17:58:29 +03:00
|
|
|
return dispatch(changeLang(resp.lang));
|
|
|
|
});
|
2016-02-26 08:25:47 +02:00
|
|
|
}
|
2016-02-26 20:13:41 +02:00
|
|
|
|
2016-08-02 21:59:29 +03:00
|
|
|
export function acceptRules() {
|
|
|
|
return (dispatch) =>
|
2016-11-05 12:11:41 +02:00
|
|
|
accounts.acceptRules().then((resp) => {
|
2016-08-02 21:59:29 +03:00
|
|
|
dispatch(updateUser({
|
|
|
|
shouldAcceptRules: false
|
|
|
|
}));
|
|
|
|
|
2016-06-04 16:46:39 +03:00
|
|
|
return resp;
|
|
|
|
});
|
2016-02-26 20:13:41 +02:00
|
|
|
}
|