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

81 lines
2.4 KiB
TypeScript
Raw Normal View History

2021-02-24 20:05:19 +00:00
import React, { useState, useEffect } from "react";
import { useParams } from "react-router-dom";
2021-02-24 13:47:27 +00:00
import { useTranslation } from "react-i18next";
2021-02-24 20:05:19 +00:00
import { PageSection } from "@patternfly/react-core";
2021-02-24 13:47:27 +00:00
import { ListEmptyState } from "../components/list-empty-state/ListEmptyState";
import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable";
import { boolFormatter, emptyFormatter } from "../util";
import { useAdminClient } from "../context/auth/AdminClient";
2021-02-24 20:05:19 +00:00
import UserRepresentation from "keycloak-admin/lib/defs/userRepresentation";
2021-02-24 13:47:27 +00:00
2021-02-24 20:05:19 +00:00
export const UsersInRoleTab = () => {
2021-02-24 13:47:27 +00:00
const { t } = useTranslation("roles");
2021-02-24 20:05:19 +00:00
const [users, setUsers] = useState<UserRepresentation[]>([]);
2021-02-24 13:47:27 +00:00
2021-02-24 20:05:19 +00:00
const { id } = useParams<{ id: string }>();
2021-02-24 13:47:27 +00:00
const adminClient = useAdminClient();
const loader = async () => {
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!,
});
setUsers(usersWithRole);
return usersWithRole;
2021-02-24 13:47:27 +00:00
};
2021-02-24 20:05:19 +00:00
useEffect(() => {
loader();
}, []);
2021-02-24 13:47:27 +00:00
return (
<>
<PageSection variant="light">
2021-02-24 20:05:19 +00:00
{users.length == 0 ? (
<ListEmptyState
hasIcon={true}
message={t("noDirectUsers")}
instructions={t("noUsersEmptyStateDescription")}
primaryActionText={t("addUser")}
/>
) : (
<KeycloakDataTable
loader={loader}
ariaLabelKey="roles:roleList"
searchPlaceholderKey=""
columns={[
{
name: "id",
displayKey: "roles:id",
cellFormatters: [emptyFormatter()],
},
{
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: [boolFormatter(), emptyFormatter()],
2021-02-24 13:47:27 +00:00
},
2021-02-24 20:05:19 +00:00
]}
/>
)}
2021-02-24 13:47:27 +00:00
</PageSection>
</>
);
};