2020-10-07 15:47:03 +00:00
|
|
|
import React, { useContext, useEffect, useState } from "react";
|
2020-09-15 19:54:52 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2020-10-06 08:25:38 +00:00
|
|
|
import { HttpClientContext } from "../context/http-service/HttpClientContext";
|
2020-09-15 19:54:52 +00:00
|
|
|
import { GroupsList } from "./GroupsList";
|
2020-09-25 18:21:18 +00:00
|
|
|
import { GroupsCreateModal } from "./GroupsCreateModal";
|
2020-09-15 19:54:52 +00:00
|
|
|
import { GroupRepresentation } from "./models/groups";
|
2020-09-28 15:58:03 +00:00
|
|
|
import {
|
|
|
|
ServerGroupsArrayRepresentation,
|
|
|
|
ServerGroupMembersRepresentation,
|
|
|
|
} from "./models/server-info";
|
2020-09-15 19:54:52 +00:00
|
|
|
import { TableToolbar } from "../components/table-toolbar/TableToolbar";
|
2020-09-29 18:21:07 +00:00
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
2020-10-14 19:48:36 +00:00
|
|
|
import { ListEmptyState } from "../components/list-empty-state/ListEmptyState";
|
2020-09-28 15:58:03 +00:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
Dropdown,
|
|
|
|
DropdownItem,
|
|
|
|
KebabToggle,
|
|
|
|
PageSection,
|
|
|
|
PageSectionVariants,
|
2020-10-07 15:47:03 +00:00
|
|
|
Spinner,
|
2020-09-28 15:58:03 +00:00
|
|
|
ToolbarItem,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
import "./GroupsSection.css";
|
2020-09-09 09:07:17 +00:00
|
|
|
|
2020-09-10 18:04:03 +00:00
|
|
|
export const GroupsSection = () => {
|
2020-09-15 19:54:52 +00:00
|
|
|
const { t } = useTranslation("groups");
|
|
|
|
const httpClient = useContext(HttpClientContext)!;
|
2020-10-07 15:47:03 +00:00
|
|
|
const [rawData, setRawData] = useState<{ [key: string]: any }[]>();
|
|
|
|
const [filteredData, setFilteredData] = useState<object[]>();
|
2020-09-28 15:58:03 +00:00
|
|
|
const [isKebabOpen, setIsKebabOpen] = useState(false);
|
2020-09-30 13:55:18 +00:00
|
|
|
const [createGroupName, setCreateGroupName] = useState("");
|
2020-10-13 20:52:23 +00:00
|
|
|
const [isCreateModalOpen, setIsCreateModalOpen] = useState(false);
|
2020-09-28 15:58:03 +00:00
|
|
|
const columnID: keyof GroupRepresentation = "id";
|
|
|
|
const membersLength: keyof GroupRepresentation = "membersLength";
|
|
|
|
const columnGroupName: keyof GroupRepresentation = "name";
|
2020-09-15 19:54:52 +00:00
|
|
|
|
|
|
|
const loader = async () => {
|
2020-09-28 15:58:03 +00:00
|
|
|
const groups = await httpClient.doGet<ServerGroupsArrayRepresentation[]>(
|
2020-10-13 20:52:23 +00:00
|
|
|
"/admin/realms/master/groups"
|
2020-09-28 15:58:03 +00:00
|
|
|
);
|
|
|
|
const groupsData = groups.data!;
|
|
|
|
|
|
|
|
const getMembers = async (id: number) => {
|
|
|
|
const response = await httpClient.doGet<
|
|
|
|
ServerGroupMembersRepresentation[]
|
|
|
|
>(`/admin/realms/master/groups/${id}/members`);
|
|
|
|
const responseData = response.data!;
|
|
|
|
return responseData.length;
|
|
|
|
};
|
|
|
|
|
|
|
|
const memberPromises = groupsData.map((group: { [key: string]: any }) =>
|
|
|
|
getMembers(group[columnID])
|
|
|
|
);
|
|
|
|
const memberData = await Promise.all(memberPromises);
|
|
|
|
const updatedObject = groupsData.map(
|
|
|
|
(group: { [key: string]: any }, i: number) => {
|
|
|
|
const object = Object.assign({}, group);
|
|
|
|
object[membersLength] = memberData[i];
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-10-13 20:52:23 +00:00
|
|
|
setRawData(updatedObject);
|
2020-09-28 15:58:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
2020-10-13 20:52:23 +00:00
|
|
|
loader();
|
2020-09-28 15:58:03 +00:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
// Filter groups
|
|
|
|
const filterGroups = (newInput: string) => {
|
2020-10-07 15:47:03 +00:00
|
|
|
const localRowData = rawData!.filter((obj: { [key: string]: string }) => {
|
2020-09-28 15:58:03 +00:00
|
|
|
const groupName = obj[columnGroupName];
|
2020-10-07 15:47:03 +00:00
|
|
|
return groupName.toLowerCase().includes(newInput.toLowerCase());
|
2020-09-28 15:58:03 +00:00
|
|
|
});
|
|
|
|
setFilteredData(localRowData);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Kebab delete action
|
|
|
|
const onKebabToggle = (isOpen: boolean) => {
|
|
|
|
setIsKebabOpen(isOpen);
|
|
|
|
};
|
|
|
|
|
|
|
|
const onKebabSelect = () => {
|
|
|
|
setIsKebabOpen(!isKebabOpen);
|
2020-09-15 19:54:52 +00:00
|
|
|
};
|
|
|
|
|
2020-10-01 18:52:18 +00:00
|
|
|
const handleModalToggle = () => {
|
2020-10-01 19:15:23 +00:00
|
|
|
setIsCreateModalOpen(!isCreateModalOpen);
|
2020-10-01 18:52:18 +00:00
|
|
|
};
|
|
|
|
|
2020-09-15 19:54:52 +00:00
|
|
|
return (
|
2020-10-14 20:47:29 +00:00
|
|
|
<>
|
2020-10-01 19:15:23 +00:00
|
|
|
<ViewHeader titleKey="groups:groups" subKey="groups:groupsDescription" />
|
2020-09-28 15:58:03 +00:00
|
|
|
<PageSection variant={PageSectionVariants.light}>
|
2020-10-14 21:02:31 +00:00
|
|
|
{!rawData && (
|
|
|
|
<div className="pf-u-text-align-center">
|
|
|
|
<Spinner />
|
|
|
|
</div>
|
|
|
|
)}
|
2020-10-14 19:48:36 +00:00
|
|
|
{rawData && rawData.length > 0 ? (
|
2020-10-13 20:52:23 +00:00
|
|
|
<>
|
|
|
|
<TableToolbar
|
2020-10-13 21:06:58 +00:00
|
|
|
inputGroupName="groupsToolbarTextInput"
|
|
|
|
inputGroupPlaceholder={t("searchGroups")}
|
|
|
|
inputGroupOnChange={filterGroups}
|
|
|
|
toolbarItem={
|
|
|
|
<>
|
|
|
|
<ToolbarItem>
|
|
|
|
<Button
|
|
|
|
variant="primary"
|
|
|
|
onClick={() => handleModalToggle()}
|
|
|
|
>
|
|
|
|
{t("createGroup")}
|
|
|
|
</Button>
|
|
|
|
</ToolbarItem>
|
|
|
|
<ToolbarItem>
|
|
|
|
<Dropdown
|
|
|
|
onSelect={onKebabSelect}
|
|
|
|
toggle={<KebabToggle onToggle={onKebabToggle} />}
|
|
|
|
isOpen={isKebabOpen}
|
|
|
|
isPlain
|
|
|
|
dropdownItems={[
|
|
|
|
<DropdownItem key="action" component="button">
|
|
|
|
{t("delete")}
|
|
|
|
</DropdownItem>,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</ToolbarItem>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
>
|
2020-10-14 20:53:32 +00:00
|
|
|
{rawData && (
|
|
|
|
<GroupsList
|
|
|
|
list={filteredData ? filteredData : rawData}
|
|
|
|
refresh={loader}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{filteredData && filteredData.length === 0 && (
|
|
|
|
<ListEmptyState
|
|
|
|
hasIcon={true}
|
|
|
|
isSearchVariant={true}
|
|
|
|
message={t("noSearchResults")}
|
|
|
|
instructions={t("noSearchResultsInstructions")}
|
|
|
|
/>
|
|
|
|
)}
|
2020-10-13 21:06:58 +00:00
|
|
|
</TableToolbar>
|
|
|
|
<GroupsCreateModal
|
|
|
|
isCreateModalOpen={isCreateModalOpen}
|
|
|
|
handleModalToggle={handleModalToggle}
|
|
|
|
setIsCreateModalOpen={setIsCreateModalOpen}
|
|
|
|
createGroupName={createGroupName}
|
|
|
|
setCreateGroupName={setCreateGroupName}
|
|
|
|
refresh={loader}
|
|
|
|
/>
|
|
|
|
</>
|
2020-10-13 20:52:23 +00:00
|
|
|
) : (
|
2020-10-14 19:48:36 +00:00
|
|
|
<ListEmptyState
|
|
|
|
hasIcon={true}
|
|
|
|
message={t("noGroupsInThisRealm")}
|
|
|
|
instructions={t("noGroupsInThisRealmInstructions")}
|
|
|
|
primaryActionText={t("createGroup")}
|
|
|
|
/>
|
2020-10-07 15:47:03 +00:00
|
|
|
)}
|
2020-09-18 08:04:55 +00:00
|
|
|
</PageSection>
|
2020-10-14 20:47:29 +00:00
|
|
|
</>
|
2020-09-15 19:54:52 +00:00
|
|
|
);
|
2020-09-09 09:07:17 +00:00
|
|
|
};
|