Cleanup user's reducer

This commit is contained in:
ErickSkrauch 2020-07-22 02:40:39 +03:00
parent 21949c780c
commit 96e74cf9bb
3 changed files with 44 additions and 41 deletions

View File

@ -1,39 +1,60 @@
import { Action as ReduxAction } from 'redux';
import { changeLang as changeLangEndpoint, acceptRules as acceptRulesEndpoint } from 'app/services/api/accounts'; import { changeLang as changeLangEndpoint, acceptRules as acceptRulesEndpoint } from 'app/services/api/accounts';
import { setLocale } from 'app/components/i18n/actions'; import { setLocale } from 'app/components/i18n/actions';
import { ThunkAction } from 'app/reducers'; import { ThunkAction } from 'app/reducers';
import { User } from './reducer'; 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 * Merge data into user's state
* *
* @param {object} payload * @param {object} payload
* @returns {object} - action definition * @returns {object} - action definition
*/ */
export function updateUser(payload: Partial<User>) { export function updateUser(payload: Partial<User>): UpdateAction {
// Temp workaround // Temp workaround
return { return {
type: UPDATE, type: 'user:update',
payload, 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 * Replace current user's state with a new one
* *
* @param {User} payload * @param {User} payload
* @returns {object} - action definition * @returns {object} - action definition
*/ */
export function setUser(payload: Partial<User>) { export function setUser(payload: Partial<User>): SetAction {
return { 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, payload,
}; };
} }
export const CHANGE_LANG = 'USER_CHANGE_LANG';
export function changeLang(targetLang: string): ThunkAction<Promise<void>> { export function changeLang(targetLang: string): ThunkAction<Promise<void>> {
return (dispatch, getState) => return (dispatch, getState) =>
dispatch(setLocale(targetLang)).then((lang: string) => { dispatch(setLocale(targetLang)).then((lang: string) => {
@ -47,12 +68,7 @@ export function changeLang(targetLang: string): ThunkAction<Promise<void>> {
changeLangEndpoint(id, lang); changeLangEndpoint(id, lang);
} }
dispatch({ dispatch(changeLangPure(lang));
type: CHANGE_LANG,
payload: {
lang,
},
});
}); });
} }
@ -86,3 +102,5 @@ export function acceptRules(): ThunkAction<Promise<{ success: boolean }>> {
}); });
}; };
} }
export type Action = UpdateAction | SetAction | ChangeLangAction;

View File

@ -1,4 +1,4 @@
import { UPDATE, SET, CHANGE_LANG } from './actions'; import { Action } from './actions';
export interface User { export interface User {
id: number | null; id: number | null;
@ -17,11 +17,9 @@ export interface User {
shouldAcceptRules?: boolean; shouldAcceptRules?: boolean;
} }
export type State = { export type State = User;
user: User;
};
const defaults: User = { const defaults: State = {
id: null, id: null,
uuid: null, uuid: null,
username: '', username: '',
@ -42,36 +40,23 @@ const defaults: User = {
isGuest: true, isGuest: true,
}; };
export default function user(state: User = defaults, { type, payload }: { type: string; payload: any }) { export default function user(state: State = defaults, action: Action): State {
switch (type) { switch (action.type) {
case CHANGE_LANG: case 'user:changeLang':
if (!payload || !payload.lang) {
throw new Error('payload.lang is required for user reducer');
}
return { return {
...state, ...state,
lang: payload.lang, lang: action.payload,
}; };
case 'user:update':
case UPDATE:
if (!payload) {
throw new Error('payload is required for user reducer');
}
return { return {
...state, ...state,
...payload, ...action.payload,
}; };
case 'user:set':
case SET:
payload = payload || {};
return { return {
...defaults, ...defaults,
...payload, ...action.payload,
}; };
default: default:
return ( return (
state || { state || {

View File

@ -1,7 +1,7 @@
import { combineReducers } from 'redux'; import { combineReducers } from 'redux';
import auth, { State as AuthState } from 'app/components/auth/reducer'; 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 accounts, { State as AccountsState } from 'app/components/accounts/reducer';
import i18n, { State as I18nState } from 'app/components/i18n/reducer'; import i18n, { State as I18nState } from 'app/components/i18n/reducer';
import popup, { State as PopupState } from 'app/components/ui/popup/reducer'; import popup, { State as PopupState } from 'app/components/ui/popup/reducer';
@ -14,7 +14,7 @@ export interface RootState {
auth: AuthState; auth: AuthState;
bsod: BsodState; bsod: BsodState;
accounts: AccountsState; accounts: AccountsState;
user: User; user: UserState;
popup: PopupState; popup: PopupState;
apps: Apps; apps: Apps;
i18n: I18nState; i18n: I18nState;