Delightful UI updates
This commit is contained in:
parent
76cbd10a6f
commit
3bcc854ba1
19 changed files with 576 additions and 88 deletions
|
|
@ -1,26 +1,46 @@
|
|||
import JwtDecode from 'jwt-decode';
|
||||
import moment from 'moment';
|
||||
|
||||
const getUserFromLocalStorage = () => {
|
||||
const u = JSON.parse(window.localStorage.getItem('redux-persistent'));
|
||||
if (!u) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (moment().isAfter(moment(u.expiresAt))) {
|
||||
window.localStorage.removeItem('redux-persistent');
|
||||
return null;
|
||||
}
|
||||
return u;
|
||||
};
|
||||
|
||||
const defaultState = {
|
||||
isAuthenticated: false,
|
||||
user: null,
|
||||
user: getUserFromLocalStorage(),
|
||||
i18n: null,
|
||||
langs: null,
|
||||
alert: null,
|
||||
};
|
||||
|
||||
const decodeUser = (token) => {
|
||||
const user = JwtDecode(token);
|
||||
const userData = { ...user, token };
|
||||
window.localStorage.setItem('redux-persistent', JSON.stringify(userData));
|
||||
return userData;
|
||||
};
|
||||
|
||||
// eslint-disable-next-line default-param-last
|
||||
export default (state = defaultState, action) => {
|
||||
switch (action.type) {
|
||||
case 'SET_USER':
|
||||
case 'SET_USER_TOKEN':
|
||||
return {
|
||||
...state,
|
||||
user: {
|
||||
...action.user,
|
||||
},
|
||||
isAuthenticated: true,
|
||||
user: decodeUser(action.token),
|
||||
};
|
||||
case 'CLEAR_USER':
|
||||
window.localStorage.removeItem('redux-persistent');
|
||||
return {
|
||||
...state,
|
||||
user: null,
|
||||
isAuthenticated: false,
|
||||
};
|
||||
case 'SET_I18N':
|
||||
return {
|
||||
|
|
@ -32,6 +52,11 @@ export default (state = defaultState, action) => {
|
|||
...state,
|
||||
langs: action.data,
|
||||
};
|
||||
case 'SET_ALERT':
|
||||
return {
|
||||
...state,
|
||||
alert: action.data || null,
|
||||
};
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue