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

68 lines
1.6 KiB
TypeScript
Raw Normal View History

2020-07-22 05:10:39 +05:30
import { Action } from './actions';
2019-12-07 16:58:52 +05:30
export interface User {
2020-05-24 04:38:24 +05:30
id: number | null;
uuid: string | null;
token: string;
username: string;
email: string;
avatar: string;
lang: string;
isGuest: boolean;
isDeleted: boolean;
2020-05-24 04:38:24 +05:30
isOtpEnabled: boolean;
passwordChangedAt: number;
hasMojangUsernameCollision: boolean;
maskedEmail?: string;
shouldAcceptRules?: boolean;
2019-12-07 16:58:52 +05:30
}
2017-06-08 23:29:31 +05:30
2020-07-22 05:10:39 +05:30
export type State = User;
2020-07-22 05:10:39 +05:30
const defaults: State = {
2020-05-24 04:38:24 +05:30
id: null,
uuid: null,
username: '',
token: '',
email: '',
// will contain user's email or masked email
// (e.g. ex**ple@em*il.c**) depending on what information user have already provided
maskedEmail: '',
avatar: '',
lang: '',
isDeleted: false,
2020-05-24 04:38:24 +05:30
isOtpEnabled: false,
shouldAcceptRules: false, // whether user need to review updated rules
passwordChangedAt: 0,
hasMojangUsernameCollision: false,
2020-05-24 04:38:24 +05:30
// frontend specific attributes
isGuest: true,
};
2020-07-22 05:10:39 +05:30
export default function user(state: State = defaults, action: Action): State {
switch (action.type) {
case 'user:changeLang':
2020-05-24 04:38:24 +05:30
return {
...state,
2020-07-22 05:10:39 +05:30
lang: action.payload,
2020-05-24 04:38:24 +05:30
};
2020-07-22 05:10:39 +05:30
case 'user:update':
2020-05-24 04:38:24 +05:30
return {
...state,
2020-07-22 05:10:39 +05:30
...action.payload,
2020-05-24 04:38:24 +05:30
};
2020-07-22 05:10:39 +05:30
case 'user:set':
2020-05-24 04:38:24 +05:30
return {
...defaults,
2020-07-22 05:10:39 +05:30
...action.payload,
2020-05-24 04:38:24 +05:30
};
default:
return (
state || {
...defaults,
}
);
}
}