keycloak-scim/src/user/UsersTabs.tsx

46 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-03-03 19:12:16 +00:00
import React from "react";
import { AlertVariant, PageSection } 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";
import { useAlerts } from "../components/alert/Alerts";
import { useAdminClient } from "../context/auth/AdminClient";
2021-03-03 13:53:42 +00:00
export const UsersTabs = () => {
const { t } = useTranslation("roles");
const form = useForm<UserRepresentation>({ mode: "onChange" });
const { addAlert } = useAlerts();
const adminClient = useAdminClient();
const save = async (user: UserRepresentation) => {
try {
await adminClient.users.create({ username: user!.username });
addAlert(t("users:userCreated"), AlertVariant.success);
} 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-03 19:12:16 +00:00
<ViewHeader
titleKey={t("users:createUser")}
subKey=""
dividerComponent="div"
/>
2021-03-03 13:53:42 +00:00
<PageSection variant="light">
<UserForm form={form} save={save} />
2021-03-03 13:53:42 +00:00
</PageSection>
</>
);
};