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

@ -6,6 +6,7 @@ export enum AuthActionTypes {
SIGN_IN_SUCCESS = 'auth/sign_in_success',
SIGN_IN_FAILURE = 'auth/sign_in_failure',
SIGN_OUT = 'auth/SIGN_OUT',
UPDATE_AUTH_USER = 'auth/update_auth_user',
REGISTRATION_START = 'auth/registration_start',
REGISTRATION_FAILURE = 'auth/registration_failure',
}

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);

View file

@ -0,0 +1,18 @@
import { UserRole } from '../users';
import { Auth } from './types';
export const transformAuthUser = (response: any): Auth => {
const resolvedUserRole = !response.userInfo.role_id ? UserRole.User : response.userInfo.role_id;
return {
token: response.accessToken,
userInfo: {
id: response.userInfo.id,
role_id: resolvedUserRole,
email: response.userInfo.email ?? null,
name: response.userInfo.name ?? null,
preferredUsername: response.userInfo.preferredUsername,
status: response.userInfo.state ?? null,
},
};
};

View file

@ -1,11 +1,6 @@
import { User } from '../users';
export interface Auth {
token: string | null;
userInfo: CurrentUser;
}
export interface CurrentUser {
email: string | null;
name: string | null;
preferredUsername: string | null;
id: string | null;
userInfo: User;
}

View file

@ -1,7 +1,8 @@
import { Dispatch } from 'redux';
import { showToast, ToastType } from 'src/common/util/show-toast';
import { performApiCall } from 'src/services/api';
import { transformRequestUser, transformResponseUser } from '../transformations';
import { AuthActionTypes } from 'src/services/auth';
import { transformRequestUser, transformUser } from '../transformations';
export enum UserActionTypes {
FETCH_USERS = 'users/fetch_users',
@ -38,7 +39,7 @@ export const fetchUsers = () => async (dispatch: Dispatch<any>) => {
dispatch({
type: UserActionTypes.FETCH_USERS,
payload: data.map(transformResponseUser),
payload: data.map(transformUser),
});
} catch (err) {
console.error(err);
@ -58,7 +59,7 @@ export const fetchUserById = (id: string) => async (dispatch: Dispatch<any>) =>
dispatch({
type: UserActionTypes.FETCH_USER,
payload: transformResponseUser(data),
payload: transformUser(data),
});
} catch (err) {
console.error(err);
@ -79,7 +80,12 @@ export const updateUserById = (user: any) => async (dispatch: Dispatch<any>) =>
dispatch({
type: UserActionTypes.UPDATE_USER,
payload: transformResponseUser(data),
payload: transformUser(data),
});
dispatch({
type: AuthActionTypes.UPDATE_AUTH_USER,
payload: transformUser(data),
});
showToast('User updated successfully.', ToastType.Success);
@ -104,7 +110,7 @@ export const createUser = (user: any) => async (dispatch: Dispatch<any>) => {
dispatch({
type: UserActionTypes.CREATE_USER,
payload: transformResponseUser(data),
payload: transformUser(data),
});
showToast('User created successfully.', ToastType.Success);

View file

@ -1,31 +1,32 @@
import _ from 'lodash';
import { User } from './types';
export const transformResponseUser = (response: any): User => {
const userResponse = _.get(response, 'user', response);
return {
id: userResponse.id,
email: userResponse.traits.email,
name: userResponse.traits.name ?? null,
status: userResponse.state,
};
};
import { User, UserRole } from './types';
export const transformUser = (response: any): User => {
const userResponse = _.get(response, 'user', response);
const resolvedUserRole = !userResponse.traits.role_id ? UserRole.User : userResponse.traits.role_id;
return {
id: userResponse.id,
email: userResponse.email,
name: userResponse.name,
status: userResponse.status,
role_id: resolvedUserRole,
email: userResponse.traits.email,
name: userResponse.traits.name ?? null,
preferredUsername: userResponse.preferredUsername,
status: userResponse.state,
};
};
export const transformRequestUser = (data: any) => {
export const transformRequestUser = (data: Pick<User, 'role_id' | 'name' | 'email'>) => {
if (data.role_id === UserRole.User) {
return {
email: data.email,
name: data.name,
};
}
return {
role_id: Number(data.role_id),
email: data.email,
name: data.name,
};

View file

@ -1,7 +1,9 @@
export interface User {
id: number | null;
role_id: UserRole | null;
email: string | null;
name: string | null;
preferredUsername: string | null;
status: string | null;
}
@ -10,10 +12,9 @@ export interface FormUser extends User {
confirmPassword?: string;
}
export interface UserRole {
id?: number;
name: string;
isAdministrator: boolean;
export enum UserRole {
Admin = '1',
User = '2',
}
export interface UserApiRequest {