keycloak-scim/src/user/UsersTabs.tsx
Eugenia 236e89dc63
Users: Add groups tab and list groups (#450)
* usergroups call wip

* user groups

* add user groups tab and list group data

* clean up log stmts

* add cypress test

* clean up userGroups

* remove comment

* fix types

* cypress test

* fix lint and cypress test

* lint

* address PR feedback from Mark

* clean up

* remove component from viewheader

* rebase and format

* remove duplicate identifier

* wrap groups in section

* fix ts errors

* add search functionality

* remove comment

* list groups initially

* remove log stmt
2021-03-23 15:02:27 -04:00

87 lines
2.7 KiB
TypeScript

import React, { useEffect, useState } from "react";
import {
AlertVariant,
PageSection,
Tab,
TabTitleText,
} from "@patternfly/react-core";
import { useTranslation } from "react-i18next";
import { useForm } from "react-hook-form";
import { ViewHeader } from "../components/view-header/ViewHeader";
import UserRepresentation from "keycloak-admin/lib/defs/userRepresentation";
import { UserForm } from "./UserForm";
import { useAlerts } from "../components/alert/Alerts";
import { useAdminClient } from "../context/auth/AdminClient";
import { useHistory, useParams, useRouteMatch } from "react-router-dom";
import { KeycloakTabs } from "../components/keycloak-tabs/KeycloakTabs";
import { UserGroups } from "./UserGroups";
export const UsersTabs = () => {
const { t } = useTranslation("roles");
const { addAlert } = useAlerts();
const { url } = useRouteMatch();
const history = useHistory();
const adminClient = useAdminClient();
const form = useForm<UserRepresentation>({ mode: "onChange" });
const { id } = useParams<{ id: string }>();
const [user, setUser] = useState("");
useEffect(() => {
const update = async () => {
if (id) {
const fetchedUser = await adminClient.users.findOne({ id });
setUser(fetchedUser.username!);
}
};
setTimeout(update, 100);
}, []);
const save = async (user: UserRepresentation) => {
try {
if (id) {
await adminClient.users.update({ id: user.id! }, user);
addAlert(t("users:userSaved"), AlertVariant.success);
} else {
await adminClient.users.create(user);
addAlert(t("users:userCreated"), AlertVariant.success);
history.push(url.substr(0, url.lastIndexOf("/")));
}
} catch (error) {
addAlert(
t("users:userCreateError", {
error: error.response.data?.errorMessage || error,
}),
AlertVariant.danger
);
}
};
return (
<>
<ViewHeader titleKey={user! || t("users:createUser")} subKey="" />
<PageSection variant="light">
{id && (
<KeycloakTabs isBox>
<Tab
eventKey="details"
data-testid="user-details-tab"
title={<TabTitleText>{t("details")}</TabTitleText>}
>
<UserForm form={form} save={save} editMode={true} />
</Tab>
<Tab
eventKey="groups"
data-testid="user-groups-tab"
title={<TabTitleText>{t("groups")}</TabTitleText>}
>
<UserGroups />
</Tab>
</KeycloakTabs>
)}
{!id && <UserForm form={form} save={save} editMode={false} />}
</PageSection>
</>
);
};