Added user roles functionality

This commit is contained in:
Valentino K 2022-04-15 14:30:27 +02:00
parent 24bdcd14e0
commit 4e4fb630b4
11 changed files with 117 additions and 92 deletions

View file

@ -1,14 +1,17 @@
import { createApiReducer, chainReducers, INITIAL_API_STATUS } from 'src/services/api';
import { User } from 'src/services/users';
import { AuthState } from './types';
import { AuthActionTypes } from './actions';
import { CurrentUser } from '../types';
import { transformAuthUser } from '../transformations';
const initialCurrentUserState: CurrentUser = {
const initialCurrentUserState: User = {
email: null,
name: null,
preferredUsername: null,
id: null,
role_id: null,
status: null,
preferredUsername: null,
};
const initialState: AuthState = {
@ -17,9 +20,21 @@ const initialState: AuthState = {
_status: INITIAL_API_STATUS,
};
const authLocalReducer = (state: any = initialState, action: any) => {
switch (action.type) {
case AuthActionTypes.UPDATE_AUTH_USER:
return {
...state,
userInfo: action.payload,
};
default:
return state;
}
};
const auth = createApiReducer(
[AuthActionTypes.SIGN_IN_START, AuthActionTypes.SIGN_IN_SUCCESS, AuthActionTypes.SIGN_IN_FAILURE],
(data) => ({ token: data.accessToken, userInfo: data.userInfo }),
(data) => transformAuthUser(data),
(data) => data.error.message,
);
@ -29,4 +44,4 @@ const signOut = createApiReducer(
() => initialState,
);
export default chainReducers(initialState, auth, signOut);
export default chainReducers(initialState, auth, signOut, authLocalReducer);