2020-10-14 20:50:10 +00:00
|
|
|
import React, { useContext, 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
|
|
|
|
2020-10-06 08:25:38 +00:00
|
|
|
import { HttpClientContext } from "../context/http-service/HttpClientContext";
|
2020-09-18 08:04:55 +00:00
|
|
|
import { RoleRepresentation } from "../model/role-model";
|
|
|
|
import { RolesList } from "./RoleList";
|
2020-10-06 08:25:38 +00:00
|
|
|
import { RealmContext } from "../context/realm-context/RealmContext";
|
2020-10-07 15:47:03 +00:00
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
2020-10-14 20:50:10 +00:00
|
|
|
import { PaginatingTableToolbar } from "../components/table-toolbar/PaginatingTableToolbar";
|
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();
|
|
|
|
const httpClient = useContext(HttpClientContext)!;
|
2020-10-14 20:50:10 +00:00
|
|
|
const [roles, setRoles] = useState<RoleRepresentation[]>();
|
2020-09-18 08:04:55 +00:00
|
|
|
const { realm } = useContext(RealmContext);
|
|
|
|
|
|
|
|
const loader = async () => {
|
2020-10-14 20:50:10 +00:00
|
|
|
const params: { [name: string]: string | number } = { first, max };
|
|
|
|
|
2020-10-07 15:47:03 +00:00
|
|
|
const result = await httpClient.doGet<RoleRepresentation[]>(
|
2020-10-14 20:50:10 +00:00
|
|
|
`/admin/realms/${realm}/roles`,
|
|
|
|
{ params: params }
|
2020-10-07 15:47:03 +00:00
|
|
|
);
|
2020-10-14 20:50:10 +00:00
|
|
|
setRoles(result.data);
|
2020-09-18 08:04:55 +00:00
|
|
|
};
|
|
|
|
|
2020-10-14 20:50:10 +00:00
|
|
|
useEffect(() => {
|
|
|
|
loader();
|
|
|
|
}, [first, max]);
|
|
|
|
|
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={
|
|
|
|
<>
|
|
|
|
<Button onClick={() => history.push("/add-role")}>
|
|
|
|
{t("createRole")}
|
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<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")}
|
|
|
|
onPrimaryAction={() => history.push("/add-role")}
|
|
|
|
/>
|
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
|
|
|
};
|