Skip to content

Commit

Permalink
Enhancing UserHasAccess component (#472)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arjun-Go authored Jul 11, 2022
1 parent 7799e25 commit 800fe7e
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/framework/esm-react-utils/src/UserHasAccess.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/** @module @category API */
import { getCurrentUser, LoggedInUser, userHasAccess } from "@openmrs/esm-api";
import React, { useEffect, useState } from "react";
import { getCurrentUser, userHasAccess, LoggedInUser } from "@openmrs/esm-api";

export interface UserHasAccessProps {
privilege: string;
fallback?: React.ReactNode;
}

export const UserHasAccess: React.FC<UserHasAccessProps> = ({
privilege,
fallback,
children,
}) => {
const [user, setUser] = useState<LoggedInUser | null>(null);
Expand All @@ -21,7 +23,7 @@ export const UserHasAccess: React.FC<UserHasAccessProps> = ({

if (user && userHasAccess(privilege, user)) {
return <>{children}</>;
} else {
return fallback ? <>{fallback}</> : null;
}

return null;
};

0 comments on commit 800fe7e

Please sign in to comment.