2020-05-24 04:38:24 +05:30
|
|
|
import { changeLang as changeLangEndpoint, acceptRules as acceptRulesEndpoint } from 'app/services/api/accounts';
|
2019-12-08 00:32:00 +05:30
|
|
|
import { setLocale } from 'app/components/i18n/actions';
|
|
|
|
import { ThunkAction } from 'app/reducers';
|
2016-02-26 11:55:47 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
import { User } from './reducer';
|
2019-01-28 00:42:58 +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
|
2019-11-27 14:33:32 +05:30
|
|
|
* @returns {object} - action definition
|
2016-02-13 20:58:47 +05:30
|
|
|
*/
|
2019-12-07 16:58:52 +05:30
|
|
|
export function updateUser(payload: Partial<User>) {
|
2020-05-24 04:38:24 +05:30
|
|
|
// Temp workaround
|
|
|
|
return {
|
|
|
|
type: UPDATE,
|
|
|
|
payload,
|
|
|
|
};
|
2016-02-13 20:58:47 +05:30
|
|
|
}
|
|
|
|
|
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
|
2019-11-27 14:33:32 +05:30
|
|
|
* @returns {object} - action definition
|
2017-01-04 11:22:46 +05:30
|
|
|
*/
|
2019-12-07 16:58:52 +05:30
|
|
|
export function setUser(payload: Partial<User>) {
|
2020-05-24 04:38:24 +05:30
|
|
|
return {
|
|
|
|
type: SET,
|
|
|
|
payload,
|
|
|
|
};
|
2017-01-04 11:22:46 +05:30
|
|
|
}
|
|
|
|
|
2016-05-20 01:11:43 +05:30
|
|
|
export const CHANGE_LANG = 'USER_CHANGE_LANG';
|
2019-12-29 17:27:44 +05:30
|
|
|
export function changeLang(targetLang: string): ThunkAction<Promise<void>> {
|
2020-07-22 04:55:45 +05:30
|
|
|
return (dispatch, getState) =>
|
2020-05-24 04:38:24 +05:30
|
|
|
dispatch(setLocale(targetLang)).then((lang: string) => {
|
|
|
|
const { id, isGuest, lang: oldLang } = getState().user;
|
2019-11-11 14:10:05 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
if (oldLang === lang) {
|
|
|
|
return;
|
|
|
|
}
|
2016-05-20 01:11:43 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
if (!isGuest && id) {
|
|
|
|
changeLangEndpoint(id, lang);
|
|
|
|
}
|
2016-07-31 19:23:16 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
dispatch({
|
|
|
|
type: CHANGE_LANG,
|
|
|
|
payload: {
|
|
|
|
lang,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2016-05-20 01:11:43 +05:30
|
|
|
}
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
export function setGuest(): ThunkAction<Promise<void>> {
|
2020-05-24 04:38:24 +05:30
|
|
|
return async (dispatch, getState) => {
|
|
|
|
dispatch(
|
|
|
|
setUser({
|
|
|
|
lang: getState().user.lang,
|
|
|
|
isGuest: true,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
};
|
2016-02-13 20:58:47 +05:30
|
|
|
}
|
2016-02-26 11:55:47 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
export function acceptRules(): ThunkAction<Promise<{ success: boolean }>> {
|
2020-05-24 04:38:24 +05:30
|
|
|
return (dispatch, getState) => {
|
|
|
|
const { id } = getState().user;
|
2019-01-28 00:42:58 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
if (!id) {
|
|
|
|
throw new Error('user id is should be set at the moment when this action is called');
|
|
|
|
}
|
2016-08-03 00:29:29 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return acceptRulesEndpoint(id).then((resp) => {
|
|
|
|
dispatch(
|
|
|
|
updateUser({
|
|
|
|
shouldAcceptRules: false,
|
|
|
|
}),
|
|
|
|
);
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return resp;
|
|
|
|
});
|
|
|
|
};
|
2016-02-26 23:43:41 +05:30
|
|
|
}
|