Skip to content

Commit

Permalink
refactor. props in UserHasAccess component
Browse files Browse the repository at this point in the history
  • Loading branch information
Arjun-Go committed Jul 6, 2022
1 parent bb61737 commit 87a89e1
Showing 1 changed file with 4 additions and 23 deletions.
27 changes: 4 additions & 23 deletions packages/framework/esm-react-utils/src/UserHasAccess.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
/** @module @category API */
import { getCurrentUser, LoggedInUser, userHasAccess } from "@openmrs/esm-api";
import { InlineNotification } from "carbon-components-react";
import React, { useEffect, useState } from "react";

export interface UserHasAccessProps {
interface UserHasAccessProps {
privilege: string;
unauthorisedResponse?: string | undefined;
redirectUrl?: string | undefined;
unauthorisedResponse?: React.ReactNode | undefined;
}

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

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

if (unauthorisedResponse) {
return (
<div className="omrs-inline-notifications-container">
<InlineNotification
title="Unauthorised"
subtitle={unauthorisedResponse}
kind="error"
/>
</div>
);
}

if (redirectUrl) {
window.location.replace(redirectUrl);
}

return null;
};

0 comments on commit 87a89e1

Please sign in to comment.