Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancing UserHasAccess component #472

Merged
merged 3 commits into from
Jul 11, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 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 {
interface UserHasAccessProps {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should still be exported

privilege: string;
unauthorisedResponse?: React.ReactNode | undefined;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

response doesn't make sense as a name here. It's not a response, it's a thing to render. I suggest fallbackChildren. Or just fallback. Note that | undefined is in most cases redundant with ?:. Also, as an aside, we normatively use US spelling.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
unauthorisedResponse?: React.ReactNode | undefined;
fallback?: React.ReactNode;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure @brandones , changes have been made accordingly.

}

export const UserHasAccess: React.FC<UserHasAccessProps> = ({
privilege,
unauthorisedResponse,
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 unauthorisedResponse ? <>{unauthorisedResponse}</> : null;
}

return null;
};