keycloak-scim/src/realm-settings/ExecutorForm.tsx

208 lines
7.2 KiB
TypeScript
Raw Normal View History

Profile view update (#1357) * added routing for viewing client profile * added add executors form template * added add executors form template * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * added adding excutors to profiles * added displaying executors - wip * added displaying executors - wip * added navigation to client profile edit on executor creation * replaced table with list for listing executors added * added support for editing client profile * added logic making executors with config links only * added read only values for edit/view client temporarily * added helpText for added executors listed in executors list * added helpText for added executors listed in executors list * added deleting executor from client profile * fixed deleting client profile and fixed messages for delete modals * fixed message for delete clinet profile modal * combined delete dialogs for client profile and executor * displaying global executors for global profiles, hiding add executor button * fixed eslint issue * fixed executors list * added back button to global profile view/edit view * fixed test * added global batche and hid actions dropdown for global profiles * fixed switch on/off labels * fixed hide/display items for global and non-global profiles * added isDirty * feedback fixes * feedback fixes * feedback fixes * feedback fixes * feedback fixes * feedback fix * small refactor * added name and removed unused state * fixed executor creation * fixed executor creation * added saving edited client profile * added saving edited client profile * improved trash icon styles * test fix * Some code suggestions * Some more code suggestions * feedback fixes * feedback fixes * use find instead of filter * feedback fixes * added defaultValues for executors * removed defaultValues for executors * final feedback fixes * minor fixes Co-authored-by: Agnieszka Gancarczyk <agancarc@redhat.com> Co-authored-by: Erik Jan de Wit <erikjan.dewit@gmail.com> Co-authored-by: Jon Koops <jonkoops@gmail.com>
2021-10-26 20:16:19 +00:00
import React, { useState } from "react";
import {
ActionGroup,
AlertVariant,
Button,
FormGroup,
PageSection,
Select,
SelectOption,
SelectVariant,
} from "@patternfly/react-core";
import { useTranslation } from "react-i18next";
import { FormAccess } from "../components/form-access/FormAccess";
import { ViewHeader } from "../components/view-header/ViewHeader";
import { useAlerts } from "../components/alert/Alerts";
import { useServerInfo } from "../context/server-info/ServerInfoProvider";
import { Controller, FormProvider, useForm } from "react-hook-form";
import { HelpItem } from "../components/help-enabler/HelpItem";
import { Link, useHistory, useParams } from "react-router-dom";
import { useAdminClient, useFetch } from "../context/auth/AdminClient";
import type ComponentTypeRepresentation from "@keycloak/keycloak-admin-client/lib/defs/componentTypeRepresentation";
import type { ConfigPropertyRepresentation } from "@keycloak/keycloak-admin-client/lib/defs/authenticatorConfigInfoRepresentation";
import type ClientProfileRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientProfileRepresentation";
import type { ClientProfileParams } from "./routes/ClientProfile";
import {
COMPONENTS,
isValidComponentType,
} from "../client-scopes/add/components/components";
import type ClientPolicyExecutorRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientPolicyExecutorRepresentation";
type ExecutorForm = Required<ClientPolicyExecutorRepresentation>;
const defaultValues: ExecutorForm = {
configuration: {},
executor: "",
};
2021-10-29 16:11:06 +00:00
export default function ExecutorForm() {
Profile view update (#1357) * added routing for viewing client profile * added add executors form template * added add executors form template * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * added adding excutors to profiles * added displaying executors - wip * added displaying executors - wip * added navigation to client profile edit on executor creation * replaced table with list for listing executors added * added support for editing client profile * added logic making executors with config links only * added read only values for edit/view client temporarily * added helpText for added executors listed in executors list * added helpText for added executors listed in executors list * added deleting executor from client profile * fixed deleting client profile and fixed messages for delete modals * fixed message for delete clinet profile modal * combined delete dialogs for client profile and executor * displaying global executors for global profiles, hiding add executor button * fixed eslint issue * fixed executors list * added back button to global profile view/edit view * fixed test * added global batche and hid actions dropdown for global profiles * fixed switch on/off labels * fixed hide/display items for global and non-global profiles * added isDirty * feedback fixes * feedback fixes * feedback fixes * feedback fixes * feedback fixes * feedback fix * small refactor * added name and removed unused state * fixed executor creation * fixed executor creation * added saving edited client profile * added saving edited client profile * improved trash icon styles * test fix * Some code suggestions * Some more code suggestions * feedback fixes * feedback fixes * use find instead of filter * feedback fixes * added defaultValues for executors * removed defaultValues for executors * final feedback fixes * minor fixes Co-authored-by: Agnieszka Gancarczyk <agancarc@redhat.com> Co-authored-by: Erik Jan de Wit <erikjan.dewit@gmail.com> Co-authored-by: Jon Koops <jonkoops@gmail.com>
2021-10-26 20:16:19 +00:00
const { t } = useTranslation("realm-settings");
const history = useHistory();
const { realm, profileName } = useParams<ClientProfileParams>();
const { addAlert, addError } = useAlerts();
const [selectExecutorTypeOpen, setSelectExecutorTypeOpen] = useState(false);
const serverInfo = useServerInfo();
const adminClient = useAdminClient();
const executorTypes =
serverInfo.componentTypes?.[
"org.keycloak.services.clientpolicy.executor.ClientPolicyExecutorProvider"
];
const [executors, setExecutors] = useState<ComponentTypeRepresentation[]>([]);
const [executorProperties, setExecutorProperties] = useState<
ConfigPropertyRepresentation[]
>([]);
const [globalProfiles, setGlobalProfiles] = useState<
ClientProfileRepresentation[]
>([]);
const [profiles, setProfiles] = useState<ClientProfileRepresentation[]>([]);
const form = useForm<ExecutorForm>({ defaultValues });
const { control } = form;
useFetch(
() =>
adminClient.clientPolicies.listProfiles({ includeGlobalProfiles: true }),
(profiles) => {
setGlobalProfiles(profiles.globalProfiles ?? []);
setProfiles(profiles.profiles ?? []);
},
[]
);
const fldNameFormatter = (name: string) =>
name.toLowerCase().trim().split(/\s+/).join("-");
const save = async () => {
const formValues = form.getValues();
const updatedProfiles = profiles.map((profile) => {
if (profile.name !== profileName) {
return profile;
}
const executors = (profile.executors ?? []).concat({
executor: formValues.executor,
configuration: formValues.configuration,
});
return {
...profile,
executors,
};
});
try {
await adminClient.clientPolicies.createProfiles({
profiles: updatedProfiles,
globalProfiles: globalProfiles,
});
addAlert(t("realm-settings:addExecutorSuccess"), AlertVariant.success);
history.push(`/${realm}/realm-settings/clientPolicies/${profileName}`);
} catch (error) {
addError("realm-settings:addExecutorError", error);
}
};
return (
<>
<ViewHeader titleKey={t("addExecutor")} divider />
<PageSection variant="light">
<FormAccess isHorizontal role="manage-realm" className="pf-u-mt-lg">
<FormGroup
label={t("executorType")}
fieldId="kc-executorType"
labelIcon={
executors.length > 0 && executors[0].helpText! !== "" ? (
<HelpItem
helpText={executors[0].helpText}
forLabel={t("executorTypeHelpText")}
forID={t(`common:helpLabel`, {
label: t("executorTypeHelpText"),
})}
/>
) : undefined
}
>
<Controller
name="executor"
defaultValue=""
control={control}
render={({ onChange, value }) => (
<Select
toggleId="kc-executor"
placeholderText="Select an executor"
onToggle={(isOpen) => setSelectExecutorTypeOpen(isOpen)}
onSelect={(_, value) => {
onChange(value.toString());
const selectedExecutor = executorTypes?.filter(
(type) => type.id === value
);
setExecutors(selectedExecutor ?? []);
setExecutorProperties(
selectedExecutor?.[0].properties ?? []
);
setSelectExecutorTypeOpen(false);
}}
selections={value}
variant={SelectVariant.single}
data-testid="executorType-select"
aria-label={t("executorType")}
isOpen={selectExecutorTypeOpen}
maxHeight={580}
>
{executorTypes?.map((option) => (
<SelectOption
selected={option.id === value}
key={option.id}
value={option.id}
description={option.helpText}
/>
))}
</Select>
)}
/>
</FormGroup>
<FormProvider {...form}>
{executorProperties.map((option) => {
const componentType = option.type!;
if (isValidComponentType(componentType)) {
const Component = COMPONENTS[componentType];
return (
<Component
key={option.name}
{...option}
name={fldNameFormatter(option.label!)}
label={option.label}
/>
);
} else {
console.warn(
`There is no editor registered for ${componentType}`
);
}
})}
</FormProvider>
<ActionGroup>
<Button
variant="primary"
onClick={save}
data-testid="addExecutor-saveBtn"
Profile view update (#1357) * added routing for viewing client profile * added add executors form template * added add executors form template * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * added adding excutors to profiles * added displaying executors - wip * added displaying executors - wip * added navigation to client profile edit on executor creation * replaced table with list for listing executors added * added support for editing client profile * added logic making executors with config links only * added read only values for edit/view client temporarily * added helpText for added executors listed in executors list * added helpText for added executors listed in executors list * added deleting executor from client profile * fixed deleting client profile and fixed messages for delete modals * fixed message for delete clinet profile modal * combined delete dialogs for client profile and executor * displaying global executors for global profiles, hiding add executor button * fixed eslint issue * fixed executors list * added back button to global profile view/edit view * fixed test * added global batche and hid actions dropdown for global profiles * fixed switch on/off labels * fixed hide/display items for global and non-global profiles * added isDirty * feedback fixes * feedback fixes * feedback fixes * feedback fixes * feedback fixes * feedback fix * small refactor * added name and removed unused state * fixed executor creation * fixed executor creation * added saving edited client profile * added saving edited client profile * improved trash icon styles * test fix * Some code suggestions * Some more code suggestions * feedback fixes * feedback fixes * use find instead of filter * feedback fixes * added defaultValues for executors * removed defaultValues for executors * final feedback fixes * minor fixes Co-authored-by: Agnieszka Gancarczyk <agancarc@redhat.com> Co-authored-by: Erik Jan de Wit <erikjan.dewit@gmail.com> Co-authored-by: Jon Koops <jonkoops@gmail.com>
2021-10-26 20:16:19 +00:00
>
{t("common:add")}
</Button>
<Button
variant="link"
component={(props) => (
<Link
{...props}
to={`/${realm}/realm-settings/clientPolicies/${profileName}`}
/>
)}
data-testid="addExecutor-cancelBtn"
Profile view update (#1357) * added routing for viewing client profile * added add executors form template * added add executors form template * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * added adding excutors to profiles * added displaying executors - wip * added displaying executors - wip * added navigation to client profile edit on executor creation * replaced table with list for listing executors added * added support for editing client profile * added logic making executors with config links only * added read only values for edit/view client temporarily * added helpText for added executors listed in executors list * added helpText for added executors listed in executors list * added deleting executor from client profile * fixed deleting client profile and fixed messages for delete modals * fixed message for delete clinet profile modal * combined delete dialogs for client profile and executor * displaying global executors for global profiles, hiding add executor button * fixed eslint issue * fixed executors list * added back button to global profile view/edit view * fixed test * added global batche and hid actions dropdown for global profiles * fixed switch on/off labels * fixed hide/display items for global and non-global profiles * added isDirty * feedback fixes * feedback fixes * feedback fixes * feedback fixes * feedback fixes * feedback fix * small refactor * added name and removed unused state * fixed executor creation * fixed executor creation * added saving edited client profile * added saving edited client profile * improved trash icon styles * test fix * Some code suggestions * Some more code suggestions * feedback fixes * feedback fixes * use find instead of filter * feedback fixes * added defaultValues for executors * removed defaultValues for executors * final feedback fixes * minor fixes Co-authored-by: Agnieszka Gancarczyk <agancarc@redhat.com> Co-authored-by: Erik Jan de Wit <erikjan.dewit@gmail.com> Co-authored-by: Jon Koops <jonkoops@gmail.com>
2021-10-26 20:16:19 +00:00
>
{t("common:cancel")}
</Button>
</ActionGroup>
</FormAccess>
</PageSection>
</>
);
2021-10-29 16:11:06 +00:00
}