import React, { useState } from "react"; import { ActionGroup, AlertVariant, Button, ButtonVariant, Divider, DropdownItem, Flex, FlexItem, FormGroup, PageSection, Text, TextArea, TextInput, TextVariants, ValidatedOptions, } from "@patternfly/react-core"; import { useTranslation } from "react-i18next"; import { useForm } from "react-hook-form"; import { FormAccess } from "../components/form-access/FormAccess"; import { ViewHeader } from "../components/view-header/ViewHeader"; import { Link, useHistory, useParams } from "react-router-dom"; import { useRealm } from "../context/realm-context/RealmContext"; import { useAlerts } from "../components/alert/Alerts"; import { useAdminClient, useFetch } from "../context/auth/AdminClient"; import type ClientProfileRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientProfileRepresentation"; import { HelpItem } from "../components/help-enabler/HelpItem"; import { PlusCircleIcon } from "@patternfly/react-icons"; import "./RealmSettingsSection.css"; import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog"; import type ClientPolicyRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientPolicyRepresentation"; import { toClientPolicies } from "./routes/ClientPolicies"; import type { EditClientPolicyParams } from "./routes/EditClientPolicy"; type NewClientPolicyForm = Required; const defaultValues: NewClientPolicyForm = { name: "", description: "", conditions: [], enabled: true, profiles: [], }; export const NewClientPolicyForm = () => { const { t } = useTranslation("realm-settings"); const { errors, reset: resetForm } = useForm({ defaultValues, }); const { realm } = useRealm(); const { policyName } = useParams(); const { addAlert, addError } = useAlerts(); const adminClient = useAdminClient(); const [policies, setPolicies] = useState([]); const [ showAddConditionsAndProfilesForm, setShowAddConditionsAndProfilesForm, ] = useState(false); const [createdPolicy, setCreatedPolicy] = useState(); const history = useHistory(); const form = useForm({ mode: "onChange" }); useFetch( () => adminClient.clientPolicies.listPolicies(), (policies) => { setPolicies(policies.policies ?? []); const currentPolicy = policies.policies?.find( (item) => item.name === policyName ); if (currentPolicy) { setupForm(currentPolicy); } }, [] ); const setupForm = (policy: ClientPolicyRepresentation) => { resetForm(); Object.entries(policy).map(([key, value]) => { form.setValue(key, value); }); }; const save = async () => { const createdForm = form.getValues(); const createdPolicy = { ...createdForm, profiles: [], conditions: [], }; const policyNameExists = policies.find( (policy) => policy.name === createdPolicy.name ); const res = policies.map((policy) => policy.name === createdPolicy.name ? createdPolicy : policy ); const allPolicies = policyNameExists ? res : policies.concat(createdForm); try { await adminClient.clientPolicies.updatePolicy({ policies: allPolicies, }); addAlert( t("realm-settings:createClientPolicySuccess"), AlertVariant.success ); setShowAddConditionsAndProfilesForm(true); setCreatedPolicy(createdPolicy); } catch (error) { addError("realm-settings:createClientProfileError", error); } }; const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({ titleKey: t("deleteClientProfileConfirmTitle"), messageKey: t("deleteClientProfileConfirm"), continueButtonLabel: t("delete"), continueButtonVariant: ButtonVariant.danger, onConfirm: async () => { const updatedPolicies = policies.filter( (policy) => policy.name !== createdPolicy?.name ); try { await adminClient.clientPolicies.updatePolicy({ policies: updatedPolicies, }); addAlert(t("deleteClientSuccess"), AlertVariant.success); history.push(toClientPolicies({ realm })); } catch (error) { addError(t("deleteClientError"), error); } }, }); return ( <> { toggleDeleteDialog; }} data-testid="deleteClientProfileDropdown" > {t("deleteClientProfile")} , ] : undefined } />