mirror of
https://github.com/elyby/accounts-frontend.git
synced 2024-11-30 02:32:58 +05:30
Cleanup user's reducer
This commit is contained in:
parent
21949c780c
commit
96e74cf9bb
@ -1,39 +1,60 @@
|
||||
import { Action as ReduxAction } from 'redux';
|
||||
|
||||
import { changeLang as changeLangEndpoint, acceptRules as acceptRulesEndpoint } from 'app/services/api/accounts';
|
||||
import { setLocale } from 'app/components/i18n/actions';
|
||||
import { ThunkAction } from 'app/reducers';
|
||||
|
||||
import { User } from './reducer';
|
||||
|
||||
export const UPDATE = 'USER_UPDATE';
|
||||
interface UpdateAction extends ReduxAction {
|
||||
type: 'user:update';
|
||||
payload: Partial<User>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge data into user's state
|
||||
*
|
||||
* @param {object} payload
|
||||
* @returns {object} - action definition
|
||||
*/
|
||||
export function updateUser(payload: Partial<User>) {
|
||||
export function updateUser(payload: Partial<User>): UpdateAction {
|
||||
// Temp workaround
|
||||
return {
|
||||
type: UPDATE,
|
||||
type: 'user:update',
|
||||
payload,
|
||||
};
|
||||
}
|
||||
|
||||
export const SET = 'USER_SET';
|
||||
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
|
||||
*/
|
||||
export function setUser(payload: Partial<User>) {
|
||||
export function setUser(payload: Partial<User>): SetAction {
|
||||
return {
|
||||
type: SET,
|
||||
type: 'user:set',
|
||||
payload,
|
||||
};
|
||||
}
|
||||
|
||||
interface ChangeLangAction extends ReduxAction {
|
||||
type: 'user:changeLang';
|
||||
payload: string;
|
||||
}
|
||||
|
||||
function changeLangPure(payload: string): ChangeLangAction {
|
||||
return {
|
||||
type: 'user:changeLang',
|
||||
payload,
|
||||
};
|
||||
}
|
||||
|
||||
export const CHANGE_LANG = 'USER_CHANGE_LANG';
|
||||
export function changeLang(targetLang: string): ThunkAction<Promise<void>> {
|
||||
return (dispatch, getState) =>
|
||||
dispatch(setLocale(targetLang)).then((lang: string) => {
|
||||
@ -47,12 +68,7 @@ export function changeLang(targetLang: string): ThunkAction<Promise<void>> {
|
||||
changeLangEndpoint(id, lang);
|
||||
}
|
||||
|
||||
dispatch({
|
||||
type: CHANGE_LANG,
|
||||
payload: {
|
||||
lang,
|
||||
},
|
||||
});
|
||||
dispatch(changeLangPure(lang));
|
||||
});
|
||||
}
|
||||
|
||||
@ -86,3 +102,5 @@ export function acceptRules(): ThunkAction<Promise<{ success: boolean }>> {
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
export type Action = UpdateAction | SetAction | ChangeLangAction;
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { UPDATE, SET, CHANGE_LANG } from './actions';
|
||||
import { Action } from './actions';
|
||||
|
||||
export interface User {
|
||||
id: number | null;
|
||||
@ -17,11 +17,9 @@ export interface User {
|
||||
shouldAcceptRules?: boolean;
|
||||
}
|
||||
|
||||
export type State = {
|
||||
user: User;
|
||||
};
|
||||
export type State = User;
|
||||
|
||||
const defaults: User = {
|
||||
const defaults: State = {
|
||||
id: null,
|
||||
uuid: null,
|
||||
username: '',
|
||||
@ -42,36 +40,23 @@ const defaults: User = {
|
||||
isGuest: true,
|
||||
};
|
||||
|
||||
export default function user(state: User = defaults, { type, payload }: { type: string; payload: any }) {
|
||||
switch (type) {
|
||||
case CHANGE_LANG:
|
||||
if (!payload || !payload.lang) {
|
||||
throw new Error('payload.lang is required for user reducer');
|
||||
}
|
||||
|
||||
export default function user(state: State = defaults, action: Action): State {
|
||||
switch (action.type) {
|
||||
case 'user:changeLang':
|
||||
return {
|
||||
...state,
|
||||
lang: payload.lang,
|
||||
lang: action.payload,
|
||||
};
|
||||
|
||||
case UPDATE:
|
||||
if (!payload) {
|
||||
throw new Error('payload is required for user reducer');
|
||||
}
|
||||
|
||||
case 'user:update':
|
||||
return {
|
||||
...state,
|
||||
...payload,
|
||||
...action.payload,
|
||||
};
|
||||
|
||||
case SET:
|
||||
payload = payload || {};
|
||||
|
||||
case 'user:set':
|
||||
return {
|
||||
...defaults,
|
||||
...payload,
|
||||
...action.payload,
|
||||
};
|
||||
|
||||
default:
|
||||
return (
|
||||
state || {
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { combineReducers } from 'redux';
|
||||
|
||||
import auth, { State as AuthState } from 'app/components/auth/reducer';
|
||||
import user, { User } from 'app/components/user/reducer';
|
||||
import user, { State as UserState } from 'app/components/user/reducer';
|
||||
import accounts, { State as AccountsState } from 'app/components/accounts/reducer';
|
||||
import i18n, { State as I18nState } from 'app/components/i18n/reducer';
|
||||
import popup, { State as PopupState } from 'app/components/ui/popup/reducer';
|
||||
@ -14,7 +14,7 @@ export interface RootState {
|
||||
auth: AuthState;
|
||||
bsod: BsodState;
|
||||
accounts: AccountsState;
|
||||
user: User;
|
||||
user: UserState;
|
||||
popup: PopupState;
|
||||
apps: Apps;
|
||||
i18n: I18nState;
|
||||
|
Loading…
Reference in New Issue
Block a user