2020-11-12 12:55:52 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
2020-09-18 08:04:55 +00:00
|
|
|
import { useHistory } from "react-router-dom";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2020-10-28 18:33:54 +00:00
|
|
|
import { Button, PageSection } from "@patternfly/react-core";
|
2020-09-18 08:04:55 +00:00
|
|
|
|
|
|
|
import { RolesList } from "./RoleList";
|
2020-11-12 12:55:52 +00:00
|
|
|
import { useAdminClient } from "../context/auth/AdminClient";
|
2020-10-14 20:50:10 +00:00
|
|
|
import { PaginatingTableToolbar } from "../components/table-toolbar/PaginatingTableToolbar";
|
2020-11-12 12:55:52 +00:00
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
|
|
|
import RoleRepresentation from "keycloak-admin/lib/defs/roleRepresentation";
|
2020-10-28 18:33:54 +00:00
|
|
|
import { ListEmptyState } from "../components/list-empty-state/ListEmptyState";
|
2020-09-09 09:07:17 +00:00
|
|
|
|
2020-09-10 18:04:03 +00:00
|
|
|
export const RealmRolesSection = () => {
|
2020-10-14 20:50:10 +00:00
|
|
|
const [max, setMax] = useState(10);
|
|
|
|
const [first, setFirst] = useState(0);
|
2020-09-18 08:04:55 +00:00
|
|
|
const { t } = useTranslation("roles");
|
|
|
|
const history = useHistory();
|
2020-11-12 12:55:52 +00:00
|
|
|
const adminClient = useAdminClient();
|
2020-10-14 20:50:10 +00:00
|
|
|
const [roles, setRoles] = useState<RoleRepresentation[]>();
|
|
|
|
|
2020-11-12 12:55:52 +00:00
|
|
|
const params: { [name: string]: string | number } = { first, max };
|
|
|
|
const loader = async () => setRoles(await adminClient.roles.find(params));
|
2020-09-18 08:04:55 +00:00
|
|
|
|
2020-10-14 20:50:10 +00:00
|
|
|
useEffect(() => {
|
|
|
|
loader();
|
|
|
|
}, [first, max]);
|
|
|
|
|
2020-12-02 21:04:54 +00:00
|
|
|
const goToCreate = () => history.push("/roles/add-role");
|
2020-09-18 08:04:55 +00:00
|
|
|
return (
|
2020-10-07 15:47:03 +00:00
|
|
|
<>
|
|
|
|
<ViewHeader titleKey="roles:title" subKey="roles:roleExplain" />
|
2020-10-14 20:50:10 +00:00
|
|
|
<PageSection variant="light">
|
2020-10-28 18:33:54 +00:00
|
|
|
{roles && roles.length > 0 ? (
|
2020-10-14 20:50:10 +00:00
|
|
|
<PaginatingTableToolbar
|
|
|
|
count={roles!.length}
|
|
|
|
first={first}
|
|
|
|
max={max}
|
|
|
|
onNextClick={setFirst}
|
|
|
|
onPreviousClick={setFirst}
|
|
|
|
onPerPageSelect={(first, max) => {
|
|
|
|
setFirst(first);
|
|
|
|
setMax(max);
|
|
|
|
}}
|
|
|
|
toolbarItem={
|
|
|
|
<>
|
2020-12-02 21:04:54 +00:00
|
|
|
<Button onClick={goToCreate}>{t("createRole")}</Button>
|
2020-10-14 20:50:10 +00:00
|
|
|
</>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<RolesList roles={roles} refresh={loader} />
|
|
|
|
</PaginatingTableToolbar>
|
2020-10-28 18:33:54 +00:00
|
|
|
) : (
|
|
|
|
<ListEmptyState
|
|
|
|
hasIcon={true}
|
|
|
|
message={t("noRolesInThisRealm")}
|
|
|
|
instructions={t("noRolesInThisRealmInstructions")}
|
|
|
|
primaryActionText={t("createRole")}
|
2020-12-02 21:04:54 +00:00
|
|
|
onPrimaryAction={goToCreate}
|
2020-10-28 18:33:54 +00:00
|
|
|
/>
|
2020-10-14 20:50:10 +00:00
|
|
|
)}
|
2020-10-07 15:47:03 +00:00
|
|
|
</PageSection>
|
|
|
|
</>
|
2020-09-18 08:04:55 +00:00
|
|
|
);
|
2020-09-09 09:07:17 +00:00
|
|
|
};
|