Skip to content

Commit

Permalink
fix(RBACProvider): Move to components package
Browse files Browse the repository at this point in the history
  • Loading branch information
bastilian committed Jul 27, 2022
1 parent 0de30e4 commit 6e7176b
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import React, { useEffect, useState } from 'react';
import { Access } from '@redhat-cloud-services/rbac-client';
import { doesHavePermissions, getRBAC, hasAllPermissions } from '../RBAC';
import { RBACContext, initialPermissions } from './constants';
import { Bullseye, Spinner } from '@patternfly/react-core';

import {
RBACContext,
doesHavePermissions,
getRBAC,
hasAllPermissions,
initialPermissions,
} from '@redhat-cloud-services/frontend-components-utilities/RBAC';

const hasAccessWithUserPermissions = (userPermissions: Access[]) => {
return (requiredPermissions: any, checkAll = true) => {
Expand All @@ -13,7 +20,7 @@ export interface RBACProviderProps {
appName: string;
}

const RBACProvider: React.FunctionComponent<RBACProviderProps> = ({ appName, children }) => {
export const RBACProvider: React.FunctionComponent<RBACProviderProps> = ({ appName, children }) => {
const [permissionState, setPermissionState] = useState(initialPermissions);
let loading = false;

Expand All @@ -31,7 +38,6 @@ const RBACProvider: React.FunctionComponent<RBACProviderProps> = ({ appName, chi
};

useEffect(() => {
console.log('SADASD', appName, permissionState, loading);
if (!loading && permissionState.permissions.length == 0) {
fetchPermissions();
}
Expand All @@ -41,10 +47,16 @@ const RBACProvider: React.FunctionComponent<RBACProviderProps> = ({ appName, chi
<RBACContext.Provider
value={{
...permissionState,
hasAccess: hasAccessWithUserPermissions(permissionState?.permissions),
hasAccess: hasAccessWithUserPermissions(permissionState?.permissions || []),
}}
>
{!permissionState.isLoading && children}
{!permissionState.isLoading ? (
children
) : (
<Bullseye>
<Spinner size="xl" />
</Bullseye>
)}
</RBACContext.Provider>
);
};
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/RBACProvider/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as RBACProvider } from './RBACProvider';
1 change: 1 addition & 0 deletions packages/components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ export * from './FilterHooks';
export * from './AsyncComponent';
export * from './Ouia';
export * from './ErrorBoundary';
export * from './RBACProvider';
17 changes: 17 additions & 0 deletions packages/utils/src/RBAC/RBAC.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createContext } from 'react';
import { Access } from '@redhat-cloud-services/rbac-client';
import type { ChromeAPI } from '@redhat-cloud-services/types';

Expand Down Expand Up @@ -60,4 +61,20 @@ export function hasAllPermissions(userPermissions: (Access | string)[], permissi
});
}

export const initialPermissions = {
isLoading: true,
isOrgAdmin: false,
permissions: [],
hasAccess: (_?: any) => {
return;
},
};

export const RBACContext = createContext(initialPermissions);

export interface UsePermissionsState extends RBAC {
isLoading: boolean;
hasAccess?: boolean;
}

export default getRBAC;
3 changes: 1 addition & 2 deletions packages/utils/src/RBACHook/RBACHook.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useContext, useEffect, useState } from 'react';
import { RBACContext, UsePermissionsState } from './constants';
import { doesHavePermissions, getRBAC, hasAllPermissions } from '../RBAC';
import { RBACContext, UsePermissionsState, doesHavePermissions, getRBAC, hasAllPermissions } from '../RBAC';

export function usePermissions(appName: string, permissionsList: string[], disableCache?: boolean, checkAll?: boolean): UsePermissionsState {
const [permissions, setPermissions] = useState<UsePermissionsState>({
Expand Down
19 changes: 0 additions & 19 deletions packages/utils/src/RBACHook/constants.ts
Original file line number Diff line number Diff line change
@@ -1,19 +0,0 @@
import { createContext } from 'react';
import { RBAC } from '../RBAC';

export const initialPermissions = {
isLoading: true,
loaded: false,
isOrgAdmin: false,
permissions: [],
hasAccess: (_?: any) => {
return;
},
};

export const RBACContext = createContext(initialPermissions);

export interface UsePermissionsState extends RBAC {
isLoading: boolean;
hasAccess?: boolean;
}
1 change: 0 additions & 1 deletion packages/utils/src/RBACHook/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { default as RBACProvider } from './RBACProvider';
export { default } from './RBACHook';
export * from './RBACHook';

0 comments on commit 6e7176b

Please sign in to comment.