2016-02-13 17:28:47 +02:00
|
|
|
import { PropTypes } from 'react';
|
|
|
|
|
|
|
|
const KEY_USER = 'user';
|
|
|
|
|
|
|
|
export default class User {
|
|
|
|
/**
|
2016-10-30 14:12:49 +02:00
|
|
|
* @param {object} [data] - plain object or jwt token or empty to load from storage
|
2016-02-13 17:28:47 +02:00
|
|
|
*
|
|
|
|
* @return {User}
|
|
|
|
*/
|
|
|
|
constructor(data) {
|
|
|
|
if (!data) {
|
|
|
|
return this.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: strict value types validation
|
|
|
|
|
|
|
|
const defaults = {
|
|
|
|
id: null,
|
2016-03-12 01:02:52 +03:00
|
|
|
uuid: null,
|
2016-02-13 17:28:47 +02:00
|
|
|
username: '',
|
|
|
|
email: '',
|
2016-05-14 23:53:58 +03:00
|
|
|
// will contain user's email or masked email
|
|
|
|
// (e.g. ex**ple@em*il.c**) depending on what information user have already provided
|
|
|
|
maskedEmail: '',
|
2016-02-13 17:28:47 +02:00
|
|
|
avatar: '',
|
2016-05-08 22:28:51 +03:00
|
|
|
lang: '',
|
2016-06-05 15:06:14 +03:00
|
|
|
isActive: false,
|
2016-08-02 21:59:29 +03:00
|
|
|
shouldAcceptRules: false, // whether user need to review updated rules
|
2016-04-23 21:44:10 +03:00
|
|
|
passwordChangedAt: null,
|
|
|
|
hasMojangUsernameCollision: false,
|
2016-10-30 14:12:49 +02:00
|
|
|
|
|
|
|
// frontend app specific attributes
|
|
|
|
isGuest: true,
|
|
|
|
goal: null, // the goal with wich user entered site
|
2016-02-13 17:28:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const user = Object.keys(defaults).reduce((user, key) => {
|
|
|
|
if (data.hasOwnProperty(key)) {
|
|
|
|
user[key] = data[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
return user;
|
|
|
|
}, defaults);
|
|
|
|
|
|
|
|
localStorage.setItem(KEY_USER, JSON.stringify(user));
|
|
|
|
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
load() {
|
|
|
|
try {
|
|
|
|
return new User(JSON.parse(localStorage.getItem(KEY_USER)));
|
|
|
|
} catch (error) {
|
|
|
|
return new User({isGuest: true});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const userShape = PropTypes.shape({
|
|
|
|
id: PropTypes.number,
|
2016-03-12 01:02:52 +03:00
|
|
|
uuid: PropTypes.string,
|
2016-02-13 17:28:47 +02:00
|
|
|
token: PropTypes.string,
|
|
|
|
username: PropTypes.string,
|
|
|
|
email: PropTypes.string,
|
|
|
|
avatar: PropTypes.string,
|
|
|
|
isGuest: PropTypes.bool.isRequired,
|
2016-03-12 01:02:52 +03:00
|
|
|
isActive: PropTypes.bool.isRequired,
|
2016-04-23 21:44:10 +03:00
|
|
|
passwordChangedAt: PropTypes.number,
|
|
|
|
hasMojangUsernameCollision: PropTypes.bool,
|
2016-02-13 17:28:47 +02:00
|
|
|
});
|