2021-03-23 19:02:27 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
2021-03-11 20:23:08 +00:00
|
|
|
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 UserRepresentation from "keycloak-admin/lib/defs/userRepresentation";
|
|
|
|
import { UserForm } from "./UserForm";
|
2021-03-03 21:17:01 +00:00
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
|
|
|
import { useAdminClient } from "../context/auth/AdminClient";
|
2021-03-11 20:23:08 +00:00
|
|
|
import { useHistory, useParams, useRouteMatch } from "react-router-dom";
|
|
|
|
import { KeycloakTabs } from "../components/keycloak-tabs/KeycloakTabs";
|
2021-03-23 19:02:27 +00:00
|
|
|
import { UserGroups } from "./UserGroups";
|
2021-04-14 18:39:21 +00:00
|
|
|
import { UserConsents } from "./UserConsents";
|
2021-03-03 13:53:42 +00:00
|
|
|
|
|
|
|
export const UsersTabs = () => {
|
|
|
|
const { t } = useTranslation("roles");
|
2021-03-03 21:17:01 +00:00
|
|
|
const { addAlert } = useAlerts();
|
2021-03-04 18:49:05 +00:00
|
|
|
const { url } = useRouteMatch();
|
|
|
|
const history = useHistory();
|
|
|
|
|
2021-03-03 21:17:01 +00:00
|
|
|
const adminClient = useAdminClient();
|
2021-04-14 18:39:21 +00:00
|
|
|
const userForm = useForm<UserRepresentation>({ mode: "onChange" });
|
2021-03-11 20:23:08 +00:00
|
|
|
const { id } = useParams<{ id: string }>();
|
2021-03-23 19:02:27 +00:00
|
|
|
const [user, setUser] = useState("");
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const update = async () => {
|
|
|
|
if (id) {
|
|
|
|
const fetchedUser = await adminClient.users.findOne({ id });
|
|
|
|
setUser(fetchedUser.username!);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
setTimeout(update, 100);
|
|
|
|
}, []);
|
2021-03-03 21:17:01 +00:00
|
|
|
|
|
|
|
const save = async (user: UserRepresentation) => {
|
|
|
|
try {
|
2021-03-11 20:23:08 +00:00
|
|
|
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("/")));
|
|
|
|
}
|
2021-03-03 21:17:01 +00:00
|
|
|
} catch (error) {
|
|
|
|
addAlert(
|
|
|
|
t("users:userCreateError", {
|
2021-03-29 11:37:47 +00:00
|
|
|
error: error.response?.data?.errorMessage || error,
|
2021-03-03 21:17:01 +00:00
|
|
|
}),
|
|
|
|
AlertVariant.danger
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2021-03-03 13:53:42 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2021-03-23 19:02:27 +00:00
|
|
|
<ViewHeader titleKey={user! || t("users:createUser")} subKey="" />
|
2021-03-03 13:53:42 +00:00
|
|
|
<PageSection variant="light">
|
2021-03-11 20:23:08 +00:00
|
|
|
{id && (
|
|
|
|
<KeycloakTabs isBox>
|
|
|
|
<Tab
|
|
|
|
eventKey="details"
|
|
|
|
data-testid="user-details-tab"
|
|
|
|
title={<TabTitleText>{t("details")}</TabTitleText>}
|
|
|
|
>
|
2021-04-14 18:39:21 +00:00
|
|
|
<UserForm form={userForm} save={save} editMode={true} />
|
2021-03-11 20:23:08 +00:00
|
|
|
</Tab>
|
2021-03-23 19:02:27 +00:00
|
|
|
<Tab
|
|
|
|
eventKey="groups"
|
|
|
|
data-testid="user-groups-tab"
|
|
|
|
title={<TabTitleText>{t("groups")}</TabTitleText>}
|
|
|
|
>
|
|
|
|
<UserGroups />
|
|
|
|
</Tab>
|
2021-04-14 18:39:21 +00:00
|
|
|
<Tab
|
|
|
|
eventKey="consents"
|
|
|
|
data-testid="user-consents-tab"
|
|
|
|
title={<TabTitleText>{t("users:consents")}</TabTitleText>}
|
|
|
|
>
|
|
|
|
<UserConsents />
|
|
|
|
</Tab>
|
2021-03-11 20:23:08 +00:00
|
|
|
</KeycloakTabs>
|
|
|
|
)}
|
2021-04-14 18:39:21 +00:00
|
|
|
{!id && <UserForm form={userForm} save={save} editMode={false} />}
|
2021-03-03 13:53:42 +00:00
|
|
|
</PageSection>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|