2016-05-01 15:58:54 +05:30
|
|
|
import accounts from 'services/api/accounts';
|
2016-05-20 01:11:43 +05:30
|
|
|
import { setLocale } from 'components/i18n/actions';
|
2016-02-26 11:55:47 +05:30
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
export const UPDATE = 'USER_UPDATE';
|
|
|
|
/**
|
2016-11-05 15:41:41 +05:30
|
|
|
* Merge data into user's state
|
|
|
|
*
|
|
|
|
* @param {object} payload
|
|
|
|
* @return {object} - action definition
|
2016-02-13 20:58:47 +05:30
|
|
|
*/
|
|
|
|
export function updateUser(payload) {
|
|
|
|
return {
|
|
|
|
type: UPDATE,
|
|
|
|
payload
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-01-04 11:22:46 +05:30
|
|
|
export const SET = 'USER_SET';
|
|
|
|
/**
|
|
|
|
* Replace current user's state with a new one
|
|
|
|
*
|
|
|
|
* @param {User} payload
|
|
|
|
* @return {object} - action definition
|
|
|
|
*/
|
|
|
|
export function setUser(payload) {
|
|
|
|
return {
|
|
|
|
type: SET,
|
|
|
|
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();
|
|
|
|
|
2016-11-05 15:41:41 +05:30
|
|
|
if (oldLang !== lang) {
|
|
|
|
!isGuest && accounts.changeLang(lang);
|
2016-07-31 19:23:16 +05:30
|
|
|
|
2016-11-05 15:41:41 +05:30
|
|
|
dispatch({
|
|
|
|
type: CHANGE_LANG,
|
|
|
|
payload: {
|
|
|
|
lang
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-05-20 01:11:43 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-04 11:22:46 +05:30
|
|
|
export function setGuest() {
|
2016-08-07 22:56:29 +05:30
|
|
|
return (dispatch, getState) => {
|
2016-11-15 11:25:15 +05:30
|
|
|
dispatch(setUser({
|
|
|
|
lang: getState().user.lang,
|
|
|
|
isGuest: true
|
|
|
|
}));
|
2016-08-07 22:56:29 +05:30
|
|
|
};
|
2016-02-13 20:58:47 +05:30
|
|
|
}
|
2016-02-26 11:55:47 +05:30
|
|
|
|
|
|
|
export function fetchUserData() {
|
2016-06-04 22:24:42 +05:30
|
|
|
return (dispatch) =>
|
2016-05-01 15:58:54 +05:30
|
|
|
accounts.current()
|
2016-06-04 20:28:29 +05:30
|
|
|
.then((resp) => {
|
2016-11-05 15:41:41 +05:30
|
|
|
dispatch(updateUser({
|
|
|
|
isGuest: false,
|
|
|
|
...resp
|
|
|
|
}));
|
2016-05-20 01:11:43 +05:30
|
|
|
|
2016-06-04 20:28:29 +05:30
|
|
|
return dispatch(changeLang(resp.lang));
|
|
|
|
});
|
2016-02-26 11:55:47 +05:30
|
|
|
}
|
2016-02-26 23:43:41 +05:30
|
|
|
|
2016-08-03 00:29:29 +05:30
|
|
|
export function acceptRules() {
|
|
|
|
return (dispatch) =>
|
2016-11-05 15:41:41 +05:30
|
|
|
accounts.acceptRules().then((resp) => {
|
2016-08-03 00:29:29 +05:30
|
|
|
dispatch(updateUser({
|
|
|
|
shouldAcceptRules: false
|
|
|
|
}));
|
|
|
|
|
2016-06-04 19:16:39 +05:30
|
|
|
return resp;
|
|
|
|
});
|
2016-02-26 23:43:41 +05:30
|
|
|
}
|