accounts-frontend/packages/app/components/user/actions.ts

107 lines
2.5 KiB
TypeScript
Raw Normal View History

2020-07-22 05:10:39 +05:30
import { Action as ReduxAction } from 'redux';
2020-05-24 04:38:24 +05:30
import { changeLang as changeLangEndpoint, acceptRules as acceptRulesEndpoint } from 'app/services/api/accounts';
import { setLocale } from 'app/components/i18n/actions';
import { Action as AppAction } from 'app/types';
2016-02-26 11:55:47 +05:30
2019-12-07 16:58:52 +05:30
import { User } from './reducer';
2020-07-22 05:10:39 +05:30
interface UpdateAction extends ReduxAction {
type: 'user:update';
payload: Partial<User>;
}
/**
2016-11-05 15:41:41 +05:30
* Merge data into user's state
*
* @param {object} payload
* @returns {object} - action definition
*/
2020-07-22 05:10:39 +05:30
export function updateUser(payload: Partial<User>): UpdateAction {
2020-05-24 04:38:24 +05:30
// Temp workaround
return {
2020-07-22 05:10:39 +05:30
type: 'user:update',
2020-05-24 04:38:24 +05:30
payload,
};
}
2020-07-22 05:10:39 +05:30
interface SetAction extends ReduxAction {
type: 'user:set';
payload: Partial<User>;
}
/**
* Replace current user's state with a new one
*
* @param {User} payload
* @returns {object} - action definition
*/
2020-07-22 05:10:39 +05:30
export function setUser(payload: Partial<User>): SetAction {
2020-05-24 04:38:24 +05:30
return {
2020-07-22 05:10:39 +05:30
type: 'user:set',
payload,
};
}
interface ChangeLangAction extends ReduxAction {
type: 'user:changeLang';
payload: string;
}
function changeLangPure(payload: string): ChangeLangAction {
return {
type: 'user:changeLang',
2020-05-24 04:38:24 +05:30
payload,
};
}
export function changeLang(targetLang: string): AppAction<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);
}
2020-07-22 05:10:39 +05:30
dispatch(changeLangPure(lang));
2020-05-24 04:38:24 +05:30
});
2016-05-20 01:11:43 +05:30
}
export function setGuest(): AppAction<Promise<void>> {
2020-05-24 04:38:24 +05:30
return async (dispatch, getState) => {
dispatch(
setUser({
lang: getState().user.lang,
isGuest: true,
}),
);
};
}
2016-02-26 11:55:47 +05:30
export function acceptRules(): AppAction<Promise<{ success: boolean }>> {
2020-05-24 04:38:24 +05:30
return (dispatch, getState) => {
const { id } = getState().user;
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,
}),
);
2020-05-24 04:38:24 +05:30
return resp;
});
};
}
2020-07-22 05:10:39 +05:30
export type Action = UpdateAction | SetAction | ChangeLangAction;