keycloak-scim/src/user/UsersTabs.tsx

129 lines
3.9 KiB
TypeScript
Raw Normal View History

import React, { useEffect, useState } from "react";
import {
AlertVariant,
PageSection,
Tab,
TabTitleText,
} from "@patternfly/react-core";
2021-03-03 13:53:42 +00:00
import { useTranslation } from "react-i18next";
2021-03-03 19:12:16 +00:00
import { useForm } from "react-hook-form";
2021-03-03 13:53:42 +00:00
import { ViewHeader } from "../components/view-header/ViewHeader";
import type UserRepresentation from "keycloak-admin/lib/defs/userRepresentation";
2021-03-03 13:53:42 +00:00
import { UserForm } from "./UserForm";
import { useAlerts } from "../components/alert/Alerts";
import { useAdminClient } from "../context/auth/AdminClient";
import { useHistory, useParams } from "react-router-dom";
import { KeycloakTabs } from "../components/keycloak-tabs/KeycloakTabs";
import { UserGroups } from "./UserGroups";
import { UserConsents } from "./UserConsents";
import type GroupRepresentation from "keycloak-admin/lib/defs/groupRepresentation";
import { useRealm } from "../context/realm-context/RealmContext";
2021-03-03 13:53:42 +00:00
export const UsersTabs = () => {
const { t } = useTranslation("roles");
const { addAlert } = useAlerts();
const history = useHistory();
const { realm } = useRealm();
const adminClient = useAdminClient();
const userForm = useForm<UserRepresentation>({ mode: "onChange" });
const { id } = useParams<{ id: string }>();
const [user, setUser] = useState("");
2021-04-20 12:22:36 +00:00
const [addedGroups, setAddedGroups] = useState<GroupRepresentation[]>([]);
useEffect(() => {
const update = async () => {
if (id) {
const fetchedUser = await adminClient.users.findOne({ id });
setUser(fetchedUser.username!);
}
};
setTimeout(update, 100);
}, []);
2021-04-20 12:22:36 +00:00
const updateGroups = (groups: GroupRepresentation[]) => {
setAddedGroups(groups);
};
const save = async (user: UserRepresentation) => {
try {
if (id) {
await adminClient.users.update({ id }, user);
addAlert(t("users:userSaved"), AlertVariant.success);
} else {
const createdUser = await adminClient.users.create(user);
2021-04-20 12:22:36 +00:00
addedGroups.forEach(async (group) => {
await adminClient.users.addToGroup({
id: createdUser.id!,
2021-04-20 12:22:36 +00:00
groupId: group.id!,
});
});
addAlert(t("users:userCreated"), AlertVariant.success);
history.push(`/${realm}/users/${createdUser.id}/settings`);
}
} catch (error) {
addAlert(
t("users:userCreateError", {
error: error.response?.data?.errorMessage || error,
}),
AlertVariant.danger
);
}
};
2021-03-03 13:53:42 +00:00
return (
<>
<ViewHeader titleKey={user! || t("users:createUser")} divider={!id} />
<PageSection variant="light" className="pf-u-p-0">
{id && (
<KeycloakTabs isBox>
<Tab
eventKey="settings"
data-testid="user-details-tab"
title={<TabTitleText>{t("details")}</TabTitleText>}
>
<PageSection variant="light">
<UserForm
onGroupsUpdate={updateGroups}
form={userForm}
save={save}
editMode={true}
/>
</PageSection>
</Tab>
<Tab
eventKey="groups"
data-testid="user-groups-tab"
title={<TabTitleText>{t("groups")}</TabTitleText>}
>
<PageSection variant="light">
<UserGroups />
</PageSection>
</Tab>
<Tab
eventKey="consents"
data-testid="user-consents-tab"
title={<TabTitleText>{t("users:consents")}</TabTitleText>}
>
2021-06-08 13:23:32 +00:00
<UserConsents />
</Tab>
</KeycloakTabs>
)}
2021-04-20 12:22:36 +00:00
{!id && (
<PageSection variant="light">
<UserForm
onGroupsUpdate={updateGroups}
form={userForm}
save={save}
editMode={false}
/>
</PageSection>
2021-04-20 12:22:36 +00:00
)}
2021-03-03 13:53:42 +00:00
</PageSection>
</>
);
};