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";
|
2021-05-04 17:58:18 +00:00
|
|
|
import type ClientRepresentation from "keycloak-admin/lib/defs/clientRepresentation";
|
2020-11-12 12:55:52 +00:00
|
|
|
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
|
|
|
|
2021-03-29 10:00:56 +00:00
|
|
|
const [showCapabilityConfig, setShowCapabilityConfig] = useState(false);
|
2020-08-31 18:26:25 +00:00
|
|
|
const [client, setClient] = useState<ClientRepresentation>({
|
2021-03-29 10:00:56 +00:00
|
|
|
protocol: "openid-connect",
|
2020-08-31 18:26:25 +00:00
|
|
|
clientId: "",
|
|
|
|
name: "",
|
|
|
|
description: "",
|
2021-04-06 18:14:56 +00:00
|
|
|
publicClient: true,
|
2020-08-31 18:26:25 +00:00
|
|
|
authorizationServicesEnabled: false,
|
2020-09-17 13:51:40 +00:00
|
|
|
serviceAccountsEnabled: false,
|
|
|
|
implicitFlowEnabled: false,
|
2021-03-19 07:49:33 +00:00
|
|
|
directAccessGrantsEnabled: true,
|
|
|
|
standardFlowEnabled: true,
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-04-13 00:05:00 +00:00
|
|
|
const forward = async (onNext: () => void) => {
|
|
|
|
if (await methods.trigger()) {
|
|
|
|
setClient({ ...client, ...methods.getValues() });
|
|
|
|
setShowCapabilityConfig(true);
|
|
|
|
onNext();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const back = () => {
|
|
|
|
setClient({ ...client, ...methods.getValues() });
|
|
|
|
methods.reset({ ...client, ...methods.getValues() });
|
|
|
|
};
|
|
|
|
|
|
|
|
const onGoToStep = (newStep: { id?: string | number }) => {
|
|
|
|
if (newStep.id === "generalSettings") {
|
|
|
|
back();
|
|
|
|
} else {
|
|
|
|
forward(() => {});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-17 13:51:40 +00:00
|
|
|
const Footer = () => (
|
|
|
|
<WizardFooter>
|
|
|
|
<WizardContextConsumer>
|
|
|
|
{({ activeStep, onNext, onBack, onClose }) => {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Button
|
|
|
|
variant="primary"
|
|
|
|
type="submit"
|
2021-04-13 00:05:00 +00:00
|
|
|
onClick={() => {
|
|
|
|
forward(onNext);
|
2020-09-17 13:51:40 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{activeStep.name === t("capabilityConfig")
|
|
|
|
? t("common:save")
|
|
|
|
: t("common:next")}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
variant="secondary"
|
|
|
|
onClick={() => {
|
2021-04-13 00:05:00 +00:00
|
|
|
back();
|
2020-09-17 13:51:40 +00:00
|
|
|
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={[
|
|
|
|
{
|
2021-03-29 10:00:56 +00:00
|
|
|
id: "generalSettings",
|
2021-02-28 20:02:31 +00:00
|
|
|
name: t("generalSettings"),
|
|
|
|
component: <GeneralSettings />,
|
|
|
|
},
|
2021-03-29 10:00:56 +00:00
|
|
|
...(showCapabilityConfig
|
|
|
|
? [
|
|
|
|
{
|
|
|
|
id: "capabilityConfig",
|
|
|
|
name: t("capabilityConfig"),
|
|
|
|
component: (
|
|
|
|
<CapabilityConfig protocol={client.protocol} />
|
|
|
|
),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
: []),
|
2021-02-28 20:02:31 +00:00
|
|
|
]}
|
|
|
|
footer={<Footer />}
|
2021-03-09 13:59:41 +00:00
|
|
|
onSave={save}
|
2021-04-13 00:05:00 +00:00
|
|
|
onGoToStep={onGoToStep}
|
2021-02-28 20:02:31 +00:00
|
|
|
/>
|
|
|
|
</FormProvider>
|
2020-08-31 18:26:25 +00:00
|
|
|
</PageSection>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|