keycloak-scim/src/user-federation/UserFederationKerberosSettings.tsx

101 lines
3.1 KiB
TypeScript
Raw Normal View History

import {
ActionGroup,
AlertVariant,
Button,
Form,
PageSection,
} from "@patternfly/react-core";
import { KerberosSettingsRequired } from "./kerberos/KerberosSettingsRequired";
import { SettingsCache } from "./shared/SettingsCache";
import { useRealm } from "../context/realm-context/RealmContext";
import type ComponentRepresentation from "@keycloak/keycloak-admin-client/lib/defs/componentRepresentation";
2021-01-20 15:43:52 +00:00
import { FormProvider, useForm } from "react-hook-form";
import { useAdminClient, useFetch } from "../context/auth/AdminClient";
2021-01-20 15:43:52 +00:00
import { useAlerts } from "../components/alert/Alerts";
import { useTranslation } from "react-i18next";
import { useHistory, useParams } from "react-router-dom";
import { Header } from "./shared/Header";
import { toUserFederation } from "./routes/UserFederation";
2021-10-29 16:11:06 +00:00
export default function UserFederationKerberosSettings() {
const { t } = useTranslation("user-federation");
const form = useForm<ComponentRepresentation>({ mode: "onChange" });
const history = useHistory();
const { adminClient } = useAdminClient();
const { realm } = useRealm();
const { id } = useParams<{ id?: string }>();
const { addAlert, addError } = useAlerts();
useFetch(
async () => {
if (id) {
return adminClient.components.findOne({ id });
}
},
(fetchedComponent) => {
if (fetchedComponent) {
setupForm(fetchedComponent);
} else if (id) {
throw new Error(t("common:notFound"));
}
},
[]
);
const setupForm = (component: ComponentRepresentation) => {
form.reset({ ...component });
};
const save = async (component: ComponentRepresentation) => {
try {
if (!id) {
await adminClient.components.create(component);
history.push(`/${realm}/user-federation`);
} else {
await adminClient.components.update({ id }, component);
2021-02-09 21:12:50 +00:00
}
setupForm(component as ComponentRepresentation);
addAlert(t(!id ? "createSuccess" : "saveSuccess"), AlertVariant.success);
} catch (error) {
addError(`user-federation:${!id ? "createError" : "saveError"}`, error);
}
};
2021-01-20 15:43:52 +00:00
return (
<>
<FormProvider {...form}>
<Header provider="Kerberos" save={() => form.handleSubmit(save)()} />
</FormProvider>
<PageSection variant="light">
<KerberosSettingsRequired form={form} showSectionHeading />
</PageSection>
<PageSection variant="light" isFilled>
<SettingsCache form={form} showSectionHeading />
<Form onSubmit={form.handleSubmit(save)}>
<ActionGroup>
2021-03-04 16:02:33 +00:00
<Button
isDisabled={!form.formState.isDirty}
variant="primary"
type="submit"
data-testid="kerberos-save"
>
{t("common:save")}
</Button>
<Button
variant="link"
onClick={() => history.push(toUserFederation({ realm }))}
2021-02-19 21:22:05 +00:00
data-testid="kerberos-cancel"
>
{t("common:cancel")}
</Button>
</ActionGroup>
</Form>
</PageSection>
</>
);
2021-10-29 16:11:06 +00:00
}