1.2.6 - Removed specific user groups and roles. Records must be dynamic.
parent
7b22385a02
commit
90f934671b
|
@ -43,4 +43,5 @@ All tests in the package can be executed by running: `npm test`.
|
|||
1.2.2 - Upgraded @flare/tuitio-client and @flare/js-utils packages.
|
||||
1.2.3 - User roles were handled in useTuitioUserInfo hook.
|
||||
1.2.4 - Upgraded @flare/tuitio-client.
|
||||
1.2.5 - Upgraded @flare/tuitio-client.
|
||||
1.2.5 - Upgraded @flare/tuitio-client.
|
||||
1.2.6 - Removed specific user groups and roles. Records must be dynamic.
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
{
|
||||
"name": "@flare/tuitio-client-react",
|
||||
"version": "1.2.5",
|
||||
"version": "1.2.6",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@flare/tuitio-client-react",
|
||||
"version": "1.2.5",
|
||||
"version": "1.2.6",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@flare/js-utils": "^1.1.0",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@flare/tuitio-client-react",
|
||||
"version": "1.2.5",
|
||||
"version": "1.2.6",
|
||||
"description": "Tuitio client react is an npm package written in typescript that facilitates the integration of a react application with Tuitio.",
|
||||
"main": "./dist/cjs/index.js",
|
||||
"module": "./dist/esm/index.js",
|
||||
|
|
|
@ -1,11 +0,0 @@
|
|||
// Copyright (c) 2023 Tudor Stanciu
|
||||
|
||||
export const userRoles = {
|
||||
SYS_ADMIN: "SYSTEM_ADMINISTRATOR",
|
||||
DEVELOPER: "FULLSTACK_DEVELOPER",
|
||||
POWER_USER: "POWER_USER",
|
||||
REGULAR_USER: "REGULAR_USER",
|
||||
VIEWER: "READONLY_USER",
|
||||
GUEST: "ANONYMOUS_USER",
|
||||
DEMO: "DEMO_USER"
|
||||
};
|
|
@ -1,46 +1,14 @@
|
|||
// Copyright (c) 2023 Tudor Stanciu
|
||||
|
||||
import { useContext, useCallback, useEffect, useMemo } from "react";
|
||||
import { useContext, useCallback, useEffect } from "react";
|
||||
import { TuitioClient } from "@flare/tuitio-client";
|
||||
import type { TuitioUserInfo, UserRole } from "@flare/tuitio-client";
|
||||
import type { TuitioUserInfo } from "@flare/tuitio-client";
|
||||
import { TuitioContext, TuitioDispatchContext } from "../contexts";
|
||||
import { userRoles } from "../constants/userRoles";
|
||||
|
||||
type UserFlags = {
|
||||
isSysAdmin: boolean;
|
||||
isDeveloper: boolean;
|
||||
isPowerUser: boolean;
|
||||
isRegularUser: boolean;
|
||||
isViewer: boolean;
|
||||
isGuest: boolean;
|
||||
isDemoGuest: boolean;
|
||||
};
|
||||
|
||||
const defaultUserFlags: UserFlags = {
|
||||
isSysAdmin: false,
|
||||
isDeveloper: false,
|
||||
isPowerUser: false,
|
||||
isRegularUser: false,
|
||||
isViewer: false,
|
||||
isGuest: false,
|
||||
isDemoGuest: false
|
||||
};
|
||||
|
||||
export type TuitioUserInfoHookResult = UserFlags & {
|
||||
export type TuitioUserInfoHookResult = {
|
||||
userInfo?: TuitioUserInfo;
|
||||
};
|
||||
|
||||
const getFlags = (roles: UserRole[]): UserFlags => {
|
||||
const isSysAdmin = roles.some(role => role.code === userRoles.SYS_ADMIN);
|
||||
const isDeveloper = roles.some(role => role.code === userRoles.DEVELOPER);
|
||||
const isPowerUser = roles.some(role => role.code === userRoles.POWER_USER);
|
||||
const isRegularUser = roles.some(role => role.code === userRoles.REGULAR_USER);
|
||||
const isViewer = roles.some(role => role.code === userRoles.VIEWER);
|
||||
const isGuest = roles.some(role => role.code === userRoles.GUEST);
|
||||
const isDemoGuest = roles.some(role => role.code === userRoles.DEMO);
|
||||
return { isSysAdmin, isDeveloper, isPowerUser, isRegularUser, isViewer, isGuest, isDemoGuest };
|
||||
};
|
||||
|
||||
const useTuitioUserInfo = (): TuitioUserInfoHookResult => {
|
||||
const state = useContext(TuitioContext);
|
||||
const dispatchActions = useContext(TuitioDispatchContext);
|
||||
|
@ -58,9 +26,8 @@ const useTuitioUserInfo = (): TuitioUserInfoHookResult => {
|
|||
}, [getUserInfo, state.userInfo]);
|
||||
|
||||
const userInfo = state.userInfo;
|
||||
const flags = useMemo(() => (userInfo ? getFlags(userInfo.userRoles) : defaultUserFlags), [userInfo]);
|
||||
|
||||
return { userInfo, ...flags };
|
||||
return { userInfo };
|
||||
};
|
||||
|
||||
export { useTuitioUserInfo };
|
||||
|
|
|
@ -75,14 +75,11 @@ describe("useTuitioUserInfo: dispatchActions", () => {
|
|||
expect(onUserInfoLoadedSpy).toHaveBeenCalled();
|
||||
expect(onUserInfoLoadedSpy).toHaveBeenCalledWith(userInfoMock);
|
||||
|
||||
const { userInfo, isSysAdmin, isDeveloper, isGuest } = result.current;
|
||||
const { userInfo } = result.current;
|
||||
expect(userInfo).toBeDefined();
|
||||
expect(userInfo?.userId).toBe(1);
|
||||
expect(userInfo?.userName).toBe("tuitio.test");
|
||||
expect(userInfo?.userRoles).toHaveLength(2);
|
||||
expect(userInfo?.userGroups).toHaveLength(1);
|
||||
expect(isSysAdmin).toBe(true);
|
||||
expect(isDeveloper).toBe(true);
|
||||
expect(isGuest).toBe(false);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue