accounts-frontend/src/components/user/reducer.js

78 lines
1.7 KiB
JavaScript
Raw Normal View History

// @flow
2016-05-20 01:11:43 +05:30
import { UPDATE, SET, CHANGE_LANG } from './actions';
2017-06-08 23:29:31 +05:30
export type User = {
id: ?number,
uuid: ?string,
2017-06-08 23:29:31 +05:30
token: string,
username: string,
email: string,
avatar: string,
isGuest: boolean,
isActive: boolean,
passwordChangedAt: ?number,
2017-06-08 23:29:31 +05:30
hasMojangUsernameCollision: bool,
};
const defaults: User = {
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: '',
isActive: false,
shouldAcceptRules: false, // whether user need to review updated rules
passwordChangedAt: null,
hasMojangUsernameCollision: false,
// frontend specific attributes
isGuest: true,
goal: null // the goal with wich user entered site
};
export default function user(
state: User = defaults,
{type, payload}: {type: string, payload: ?Object}
) {
switch (type) {
2016-05-20 01:11:43 +05:30
case CHANGE_LANG:
if (!payload || !payload.lang) {
throw new Error('payload.lang is required for user reducer');
}
return {
2016-05-20 01:11:43 +05:30
...state,
lang: payload.lang
};
2016-05-20 01:11:43 +05:30
case UPDATE:
if (!payload) {
throw new Error('payload is required for user reducer');
}
return {
...state,
...payload
};
case SET:
payload = payload || {};
return {
...defaults,
...payload
};
default:
return state || {
...defaults
};
}
}