Skip to content

Commit

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

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

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

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 bb61737

Please sign in to comment.