2021-03-03 19:12:16 +00:00
|
|
|
import React 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-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-03-04 18:49:05 +00:00
|
|
|
const form = useForm<UserRepresentation>({ mode: "onChange" });
|
2021-03-11 20:23:08 +00:00
|
|
|
const { id } = useParams<{ id: string }>();
|
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", {
|
|
|
|
error: error.response.data?.errorMessage || error,
|
|
|
|
}),
|
|
|
|
AlertVariant.danger
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2021-03-03 13:53:42 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2021-03-19 07:49:33 +00:00
|
|
|
<ViewHeader titleKey={id! || 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>}
|
|
|
|
>
|
|
|
|
<UserForm form={form} save={save} editMode={true} />
|
|
|
|
</Tab>
|
|
|
|
</KeycloakTabs>
|
|
|
|
)}
|
|
|
|
{!id && <UserForm form={form} save={save} editMode={false} />}
|
2021-03-03 13:53:42 +00:00
|
|
|
</PageSection>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|