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

83 lines
1.8 KiB
JavaScript
Raw Normal View History

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
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
*/
export function updateUser(payload) {
return {
type: UPDATE,
payload
};
}
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-11-05 15:41:41 +05:30
dispatch({
type: CHANGE_LANG,
payload: {
lang
}
});
}
2016-05-20 01:11:43 +05:30
});
}
export function setGuest() {
return (dispatch, getState) => {
dispatch(setUser({
lang: getState().user.lang,
isGuest: true
}));
};
}
2016-02-26 11:55:47 +05:30
export function fetchUserData() {
return (dispatch) =>
accounts.current()
.then((resp) => {
2016-11-05 15:41:41 +05:30
dispatch(updateUser({
isGuest: false,
...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) =>
2016-11-05 15:41:41 +05:30
accounts.acceptRules().then((resp) => {
2016-08-03 00:29:29 +05:30
dispatch(updateUser({
shouldAcceptRules: false
}));
return resp;
});
}