mirror of
https://github.com/elyby/accounts-frontend.git
synced 2025-05-31 14:11:58 +05:30
#246: remove User class in favor of plain object in reducer
This commit is contained in:
@@ -1,18 +1,17 @@
|
|||||||
import { routeActions } from 'react-router-redux';
|
import { routeActions } from 'react-router-redux';
|
||||||
|
|
||||||
import authentication from 'services/api/authentication';
|
import authentication from 'services/api/authentication';
|
||||||
import accounts from 'services/api/accounts';
|
|
||||||
import { updateUser, setGuest } from 'components/user/actions';
|
import { updateUser, setGuest } from 'components/user/actions';
|
||||||
import { setLocale } from 'components/i18n/actions';
|
import { setLocale } from 'components/i18n/actions';
|
||||||
import logger from 'services/logger';
|
import logger from 'services/logger';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {object} Account
|
* @typedef {object} Account
|
||||||
* @property {string} account.id
|
* @property {string} id
|
||||||
* @property {string} account.username
|
* @property {string} username
|
||||||
* @property {string} account.email
|
* @property {string} email
|
||||||
* @property {string} account.token
|
* @property {string} token
|
||||||
* @property {string} account.refreshToken
|
* @property {string} refreshToken
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -1,66 +1,18 @@
|
|||||||
import { PropTypes } from 'react';
|
import { PropTypes } from 'react';
|
||||||
|
|
||||||
const KEY_USER = 'user';
|
|
||||||
|
|
||||||
export default class User {
|
|
||||||
/**
|
/**
|
||||||
* @param {object} [data] - plain object or jwt token or empty to load from storage
|
* @typedef {object} User
|
||||||
*
|
* @property {number} id
|
||||||
* @return {User}
|
* @property {string} uuid
|
||||||
|
* @property {string} token
|
||||||
|
* @property {string} username
|
||||||
|
* @property {string} email
|
||||||
|
* @property {string} avatar
|
||||||
|
* @property {bool} isGuest
|
||||||
|
* @property {bool} isActive
|
||||||
|
* @property {number} passwordChangedAt - timestamp
|
||||||
|
* @property {bool} hasMojangUsernameCollision
|
||||||
*/
|
*/
|
||||||
constructor(data) {
|
|
||||||
if (!data) {
|
|
||||||
return this.load();
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: strict value types validation
|
|
||||||
|
|
||||||
const defaults = {
|
|
||||||
id: null,
|
|
||||||
uuid: null,
|
|
||||||
username: '',
|
|
||||||
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 app specific attributes
|
|
||||||
isGuest: true,
|
|
||||||
goal: null, // the goal with wich user entered site
|
|
||||||
|
|
||||||
// TODO: remove me after migration to multy accs
|
|
||||||
token: '',
|
|
||||||
refreshToken: ''
|
|
||||||
};
|
|
||||||
|
|
||||||
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({
|
export const userShape = PropTypes.shape({
|
||||||
id: PropTypes.number,
|
id: PropTypes.number,
|
||||||
uuid: PropTypes.string,
|
uuid: PropTypes.string,
|
||||||
|
@@ -1,10 +1,28 @@
|
|||||||
import { UPDATE, SET, CHANGE_LANG } from './actions';
|
import { UPDATE, SET, CHANGE_LANG } from './actions';
|
||||||
|
|
||||||
import User from './User';
|
|
||||||
|
|
||||||
// TODO: возможно есть смысл инитить обьект User снаружи, так как редусер не должен столько знать
|
const defaults = {
|
||||||
|
id: null,
|
||||||
|
uuid: null,
|
||||||
|
username: '',
|
||||||
|
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(
|
export default function user(
|
||||||
state = new User(),
|
state = null,
|
||||||
{type, payload = null}
|
{type, payload = null}
|
||||||
) {
|
) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
@@ -13,25 +31,32 @@ export default function user(
|
|||||||
throw new Error('payload.lang is required for user reducer');
|
throw new Error('payload.lang is required for user reducer');
|
||||||
}
|
}
|
||||||
|
|
||||||
return new User({
|
return {
|
||||||
...state,
|
...state,
|
||||||
lang: payload.lang
|
lang: payload.lang
|
||||||
});
|
};
|
||||||
|
|
||||||
case UPDATE:
|
case UPDATE:
|
||||||
if (!payload) {
|
if (!payload) {
|
||||||
throw new Error('payload is required for user reducer');
|
throw new Error('payload is required for user reducer');
|
||||||
}
|
}
|
||||||
|
|
||||||
return new User({
|
return {
|
||||||
...state,
|
...state,
|
||||||
...payload
|
...payload
|
||||||
});
|
};
|
||||||
|
|
||||||
case SET:
|
case SET:
|
||||||
return new User(payload || {});
|
payload = payload || {};
|
||||||
|
|
||||||
|
return {
|
||||||
|
...defaults,
|
||||||
|
...payload
|
||||||
|
};
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return state;
|
return state || {
|
||||||
|
...defaults
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user