keycloak-scim/src/realm-roles/UsersInRoleTab.tsx

135 lines
4.3 KiB
TypeScript
Raw Normal View History

2021-03-01 17:20:58 +00:00
import { Button, PageSection, Popover } from "@patternfly/react-core";
import { QuestionCircleIcon } from "@patternfly/react-icons";
import React from "react";
import { useTranslation } from "react-i18next";
import { useHistory, useParams } from "react-router-dom";
import { useHelp } from "../components/help-enabler/HelpHeader";
2021-02-24 13:47:27 +00:00
import { ListEmptyState } from "../components/list-empty-state/ListEmptyState";
import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable";
import { useAdminClient } from "../context/auth/AdminClient";
2021-03-01 17:20:58 +00:00
import { useRealm } from "../context/realm-context/RealmContext";
import { emptyFormatter, upperCaseFormatter } from "../util";
2021-02-24 13:47:27 +00:00
2021-02-24 20:05:19 +00:00
export const UsersInRoleTab = () => {
2021-03-01 17:20:58 +00:00
const history = useHistory();
const { realm } = useRealm();
2021-02-24 13:47:27 +00:00
const { t } = useTranslation("roles");
2021-02-24 20:05:19 +00:00
const { id } = useParams<{ id: string }>();
2021-02-24 13:47:27 +00:00
const adminClient = useAdminClient();
2021-03-02 15:27:33 +00:00
const loader = async (first?: number, max?: number) => {
2021-02-24 20:05:19 +00:00
const role = await adminClient.roles.findOneById({ id: id });
const usersWithRole = await adminClient.roles.findUsersWithRole({
name: role.name!,
2021-03-02 15:27:33 +00:00
first: first!,
max: max!,
2021-03-05 13:47:59 +00:00
});
2021-05-06 05:32:42 +00:00
return usersWithRole || [];
2021-02-24 13:47:27 +00:00
};
const { enabled } = useHelp();
2021-03-02 16:11:08 +00:00
2021-02-24 13:47:27 +00:00
return (
<>
2021-03-01 15:22:25 +00:00
<PageSection data-testid="users-page" variant="light">
2021-03-01 15:30:16 +00:00
<KeycloakDataTable
2021-03-02 15:27:33 +00:00
isPaginated
2021-03-01 15:30:16 +00:00
loader={loader}
ariaLabelKey="roles:roleList"
searchPlaceholderKey=""
2021-03-01 17:20:58 +00:00
toolbarItem={
2021-03-02 16:11:08 +00:00
enabled && (
<Popover
aria-label="Basic popover"
position="bottom"
bodyContent={
<div>
{t("roles:whoWillAppearPopoverText")}
<Button
className="kc-groups-link"
variant="link"
onClick={() => history.push(`/${realm}/groups`)}
>
{t("groups")}
</Button>
{t("or")}
<Button
className="kc-users-link"
variant="link"
onClick={() => history.push(`/${realm}/users`)}
>
{t("users")}.
</Button>
</div>
}
footerContent={t("roles:whoWillAppearPopoverFooterText")}
2021-03-01 17:20:58 +00:00
>
2021-03-02 16:11:08 +00:00
<Button
variant="link"
className="kc-who-will-appear-button"
key="who-will-appear-button"
icon={<QuestionCircleIcon />}
>
{t("roles:whoWillAppearLinkText")}
</Button>
</Popover>
)
2021-03-01 17:20:58 +00:00
}
2021-03-01 15:30:16 +00:00
emptyState={
<ListEmptyState
hasIcon={true}
message={t("noDirectUsers")}
2021-03-01 19:20:26 +00:00
instructions={
<div>
{t("noUsersEmptyStateDescription")}
<Button
className="kc-groups-link-empty-state"
variant="link"
onClick={() => history.push(`/${realm}/groups`)}
>
{t("groups")}
</Button>
{t("or")}
<Button
className="kc-users-link-empty-state"
variant="link"
onClick={() => history.push(`/${realm}/users`)}
>
{t("users")}
</Button>
{t("noUsersEmptyStateDescriptionContinued")}
</div>
}
2021-03-01 15:30:16 +00:00
/>
}
columns={[
{
name: "username",
displayKey: "roles:userName",
cellFormatters: [emptyFormatter()],
},
{
name: "email",
displayKey: "roles:email",
cellFormatters: [emptyFormatter()],
},
{
name: "lastName",
displayKey: "roles:lastName",
cellFormatters: [emptyFormatter()],
},
{
name: "firstName",
displayKey: "roles:firstName",
cellFormatters: [upperCaseFormatter(), emptyFormatter()],
2021-03-01 15:30:16 +00:00
},
]}
/>
2021-02-24 13:47:27 +00:00
</PageSection>
</>
);
};