2021-03-16 12:37:57 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
|
import { useHistory } from "react-router-dom";
|
2020-09-15 19:54:52 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2021-03-16 12:37:57 +00:00
|
|
|
import { useErrorHandler } from "react-error-boundary";
|
2020-09-28 15:58:03 +00:00
|
|
|
import {
|
|
|
|
DropdownItem,
|
|
|
|
PageSection,
|
|
|
|
PageSectionVariants,
|
2020-10-27 23:45:35 +00:00
|
|
|
AlertVariant,
|
2021-03-16 12:37:57 +00:00
|
|
|
Tab,
|
|
|
|
TabTitleText,
|
|
|
|
Tabs,
|
2020-09-28 15:58:03 +00:00
|
|
|
} from "@patternfly/react-core";
|
2020-11-12 12:55:52 +00:00
|
|
|
import GroupRepresentation from "keycloak-admin/lib/defs/groupRepresentation";
|
2020-09-09 09:07:17 +00:00
|
|
|
|
2020-12-14 08:57:05 +00:00
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
2021-03-16 12:37:57 +00:00
|
|
|
import { asyncStateFetch, useAdminClient } from "../context/auth/AdminClient";
|
2020-12-14 08:57:05 +00:00
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
2021-01-12 14:00:44 +00:00
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
2020-12-14 08:57:05 +00:00
|
|
|
|
2021-03-16 12:37:57 +00:00
|
|
|
import { useSubGroups } from "./SubGroupsContext";
|
|
|
|
import { GroupTable } from "./GroupTable";
|
|
|
|
import { getId, getLastId } from "./groupIdUtils";
|
|
|
|
import { Members } from "./Members";
|
|
|
|
import { GroupAttributes } from "./GroupAttributes";
|
2021-01-12 14:00:44 +00:00
|
|
|
|
2021-03-16 12:37:57 +00:00
|
|
|
import "./GroupsSection.css";
|
2021-01-12 14:00:44 +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");
|
2021-03-16 12:37:57 +00:00
|
|
|
const [activeTab, setActiveTab] = useState(0);
|
|
|
|
|
2020-11-12 12:55:52 +00:00
|
|
|
const adminClient = useAdminClient();
|
2021-01-12 14:00:44 +00:00
|
|
|
const { subGroups, setSubGroups } = useSubGroups();
|
2020-10-27 23:45:35 +00:00
|
|
|
const { addAlert } = useAlerts();
|
2021-01-12 14:00:44 +00:00
|
|
|
const { realm } = useRealm();
|
2021-03-16 12:37:57 +00:00
|
|
|
const errorHandler = useErrorHandler();
|
2021-01-12 14:00:44 +00:00
|
|
|
|
2021-03-16 12:37:57 +00:00
|
|
|
const history = useHistory();
|
2021-01-12 14:00:44 +00:00
|
|
|
const id = getLastId(location.pathname);
|
|
|
|
|
2021-01-05 19:56:16 +00:00
|
|
|
const deleteGroup = async (group: GroupRepresentation) => {
|
2020-12-14 08:57:05 +00:00
|
|
|
try {
|
2021-01-05 19:56:16 +00:00
|
|
|
await adminClient.groups.del({
|
2020-12-14 08:57:05 +00:00
|
|
|
id: group.id!,
|
2020-10-27 23:45:35 +00:00
|
|
|
});
|
2021-01-05 19:56:16 +00:00
|
|
|
addAlert(t("groupDelete"), AlertVariant.success);
|
2020-12-14 08:57:05 +00:00
|
|
|
} catch (error) {
|
|
|
|
addAlert(t("groupDeleteError", { error }), AlertVariant.danger);
|
|
|
|
}
|
2021-01-05 19:56:16 +00:00
|
|
|
return true;
|
2020-12-14 08:57:05 +00:00
|
|
|
};
|
2020-10-27 23:45:35 +00:00
|
|
|
|
2021-03-16 12:37:57 +00:00
|
|
|
useEffect(
|
|
|
|
() =>
|
|
|
|
asyncStateFetch(
|
|
|
|
async () => {
|
|
|
|
const ids = getId(location.pathname);
|
|
|
|
const isNavigationStateInValid =
|
|
|
|
ids && ids.length !== subGroups.length + 1;
|
|
|
|
if (isNavigationStateInValid) {
|
|
|
|
const groups: GroupRepresentation[] = [];
|
|
|
|
for (const i of ids!) {
|
|
|
|
const group = await adminClient.groups.findOne({ id: i });
|
|
|
|
if (group) groups.push(group);
|
|
|
|
}
|
|
|
|
return groups;
|
|
|
|
} else {
|
|
|
|
if (id) {
|
|
|
|
const group = await adminClient.groups.findOne({ id: id });
|
|
|
|
if (group) {
|
|
|
|
return [...subGroups, group];
|
|
|
|
} else {
|
|
|
|
return subGroups;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return subGroups;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(groups: GroupRepresentation[]) => setSubGroups(groups),
|
|
|
|
errorHandler
|
|
|
|
),
|
|
|
|
[id]
|
2020-12-14 08:57:05 +00:00
|
|
|
);
|
|
|
|
|
2020-09-15 19:54:52 +00:00
|
|
|
return (
|
2020-10-14 20:47:29 +00:00
|
|
|
<>
|
2021-03-01 15:06:04 +00:00
|
|
|
<ViewHeader
|
|
|
|
titleKey="groups:groups"
|
|
|
|
subKey="groups:groupsDescription"
|
|
|
|
dropdownItems={[
|
|
|
|
<DropdownItem
|
|
|
|
data-testid="searchGroup"
|
|
|
|
key="searchGroup"
|
|
|
|
onClick={() => history.push(`/${realm}/groups/search`)}
|
|
|
|
>
|
|
|
|
{t("searchGroup")}
|
|
|
|
</DropdownItem>,
|
|
|
|
<DropdownItem
|
|
|
|
data-testid="renameGroup"
|
|
|
|
key="renameGroup"
|
|
|
|
onClick={() => addAlert("Not implemented")}
|
|
|
|
>
|
|
|
|
{t("renameGroup")}
|
|
|
|
</DropdownItem>,
|
|
|
|
<DropdownItem
|
|
|
|
data-testid="deleteGroup"
|
|
|
|
key="deleteGroup"
|
|
|
|
onClick={() => deleteGroup({ id })}
|
|
|
|
>
|
|
|
|
{t("deleteGroup")}
|
|
|
|
</DropdownItem>,
|
|
|
|
]}
|
|
|
|
/>
|
2020-09-28 15:58:03 +00:00
|
|
|
<PageSection variant={PageSectionVariants.light}>
|
2021-03-16 12:37:57 +00:00
|
|
|
{subGroups.length > 0 && (
|
|
|
|
<Tabs
|
|
|
|
activeKey={activeTab}
|
|
|
|
isSecondary
|
|
|
|
onSelect={(_, key) => setActiveTab(key as number)}
|
|
|
|
isBox
|
|
|
|
>
|
|
|
|
<Tab
|
|
|
|
data-testid="groups"
|
|
|
|
eventKey={0}
|
|
|
|
title={<TabTitleText>{t("childGroups")}</TabTitleText>}
|
|
|
|
>
|
|
|
|
<GroupTable />
|
|
|
|
</Tab>
|
|
|
|
<Tab
|
|
|
|
data-testid="members"
|
|
|
|
eventKey={1}
|
|
|
|
title={<TabTitleText>{t("members")}</TabTitleText>}
|
|
|
|
>
|
|
|
|
<Members />
|
|
|
|
</Tab>
|
|
|
|
<Tab
|
|
|
|
data-testid="attributes"
|
|
|
|
eventKey={2}
|
|
|
|
title={<TabTitleText>{t("attributes")}</TabTitleText>}
|
|
|
|
>
|
|
|
|
<GroupAttributes />
|
|
|
|
</Tab>
|
|
|
|
</Tabs>
|
2021-03-01 15:06:04 +00:00
|
|
|
)}
|
2021-03-16 12:37:57 +00:00
|
|
|
{subGroups.length === 0 && <GroupTable />}
|
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
|
|
|
};
|