2020-11-12 12:55:52 +00:00
|
|
|
import React, { useState } from "react";
|
2021-02-23 08:55:24 +00:00
|
|
|
import { useHistory } from "react-router-dom";
|
2020-08-31 18:26:25 +00:00
|
|
|
import {
|
|
|
|
PageSection,
|
|
|
|
Wizard,
|
|
|
|
AlertVariant,
|
2020-09-17 13:51:40 +00:00
|
|
|
WizardFooter,
|
|
|
|
WizardContextConsumer,
|
|
|
|
Button,
|
2020-08-31 18:26:25 +00:00
|
|
|
} from "@patternfly/react-core";
|
2020-09-17 13:51:40 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2021-02-28 20:02:31 +00:00
|
|
|
import { FormProvider, useForm } from "react-hook-form";
|
2020-09-17 13:51:40 +00:00
|
|
|
|
2020-09-22 12:43:51 +00:00
|
|
|
import { GeneralSettings } from "./GeneralSettings";
|
|
|
|
import { CapabilityConfig } from "./CapabilityConfig";
|
2020-08-31 18:26:25 +00:00
|
|
|
import { useAlerts } from "../../components/alert/Alerts";
|
2020-09-25 17:29:03 +00:00
|
|
|
import { ViewHeader } from "../../components/view-header/ViewHeader";
|
2020-11-12 12:55:52 +00:00
|
|
|
import ClientRepresentation from "keycloak-admin/lib/defs/clientRepresentation";
|
|
|
|
import { useAdminClient } from "../../context/auth/AdminClient";
|
2021-02-23 08:55:24 +00:00
|
|
|
import { useRealm } from "../../context/realm-context/RealmContext";
|
2020-08-31 18:26:25 +00:00
|
|
|
|
|
|
|
export const NewClientForm = () => {
|
2020-09-10 18:04:03 +00:00
|
|
|
const { t } = useTranslation("clients");
|
2021-02-23 08:55:24 +00:00
|
|
|
const { realm } = useRealm();
|
2020-11-12 12:55:52 +00:00
|
|
|
const adminClient = useAdminClient();
|
2020-09-17 13:51:40 +00:00
|
|
|
const history = useHistory();
|
2020-09-17 11:37:30 +00:00
|
|
|
|
2020-08-31 18:26:25 +00:00
|
|
|
const [client, setClient] = useState<ClientRepresentation>({
|
|
|
|
protocol: "",
|
|
|
|
clientId: "",
|
|
|
|
name: "",
|
|
|
|
description: "",
|
|
|
|
publicClient: false,
|
|
|
|
authorizationServicesEnabled: false,
|
2020-09-17 13:51:40 +00:00
|
|
|
serviceAccountsEnabled: false,
|
|
|
|
implicitFlowEnabled: false,
|
|
|
|
directAccessGrantsEnabled: false,
|
|
|
|
standardFlowEnabled: false,
|
2020-08-31 18:26:25 +00:00
|
|
|
});
|
2020-10-06 19:25:05 +00:00
|
|
|
const { addAlert } = useAlerts();
|
2020-09-17 13:51:40 +00:00
|
|
|
const methods = useForm<ClientRepresentation>({ defaultValues: client });
|
2020-08-31 18:26:25 +00:00
|
|
|
|
|
|
|
const save = async () => {
|
|
|
|
try {
|
2021-02-23 08:55:24 +00:00
|
|
|
const newClient = await adminClient.clients.create({ ...client });
|
2020-10-07 16:01:16 +00:00
|
|
|
addAlert(t("createSuccess"), AlertVariant.success);
|
2021-03-03 08:10:30 +00:00
|
|
|
history.push(`/${realm}/clients/${newClient.id}/settings`);
|
2020-08-31 18:26:25 +00:00
|
|
|
} catch (error) {
|
2020-10-07 16:01:16 +00:00
|
|
|
addAlert(t("createError", { error }), AlertVariant.danger);
|
2020-08-31 18:26:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-17 13:51:40 +00:00
|
|
|
const Footer = () => (
|
|
|
|
<WizardFooter>
|
|
|
|
<WizardContextConsumer>
|
|
|
|
{({ activeStep, onNext, onBack, onClose }) => {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Button
|
|
|
|
variant="primary"
|
|
|
|
type="submit"
|
|
|
|
onClick={async () => {
|
|
|
|
if (await methods.trigger()) {
|
|
|
|
setClient({ ...client, ...methods.getValues() });
|
|
|
|
onNext();
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{activeStep.name === t("capabilityConfig")
|
|
|
|
? t("common:save")
|
|
|
|
: t("common:next")}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
variant="secondary"
|
|
|
|
onClick={() => {
|
|
|
|
setClient({ ...client, ...methods.getValues() });
|
|
|
|
methods.reset({ ...client, ...methods.getValues() });
|
|
|
|
onBack();
|
|
|
|
}}
|
|
|
|
isDisabled={activeStep.name === t("generalSettings")}
|
|
|
|
>
|
|
|
|
{t("common:back")}
|
|
|
|
</Button>
|
|
|
|
<Button variant="link" onClick={onClose}>
|
|
|
|
{t("common:cancel")}
|
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</WizardContextConsumer>
|
|
|
|
</WizardFooter>
|
|
|
|
);
|
2020-08-31 18:26:25 +00:00
|
|
|
|
2020-09-16 14:56:23 +00:00
|
|
|
const title = t("createClient");
|
2020-08-31 18:26:25 +00:00
|
|
|
return (
|
|
|
|
<>
|
2020-09-25 17:29:03 +00:00
|
|
|
<ViewHeader
|
|
|
|
titleKey="clients:createClient"
|
|
|
|
subKey="clients:clientsExplain"
|
|
|
|
/>
|
2020-08-31 18:26:25 +00:00
|
|
|
<PageSection variant="light">
|
2021-02-28 20:02:31 +00:00
|
|
|
<FormProvider {...methods}>
|
|
|
|
<Wizard
|
|
|
|
onClose={() => history.push(`/${realm}/clients`)}
|
|
|
|
navAriaLabel={`${title} steps`}
|
|
|
|
mainAriaLabel={`${title} content`}
|
|
|
|
steps={[
|
|
|
|
{
|
|
|
|
name: t("generalSettings"),
|
|
|
|
component: <GeneralSettings />,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: t("capabilityConfig"),
|
|
|
|
component: <CapabilityConfig />,
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
footer={<Footer />}
|
|
|
|
onSave={() => save()}
|
|
|
|
/>
|
|
|
|
</FormProvider>
|
2020-08-31 18:26:25 +00:00
|
|
|
</PageSection>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|