import React, { useMemo, useState } from "react"; import { AlertVariant, Button, ButtonVariant, Divider, Flex, FlexItem, PageSection, Radio, Spinner, Title, ToolbarItem, } from "@patternfly/react-core"; import { ListEmptyState } from "../components/list-empty-state/ListEmptyState"; import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable"; import { useTranslation } from "react-i18next"; import { useAdminClient, useFetch } from "../context/auth/AdminClient"; import { prettyPrintJSON, upperCaseFormatter } from "../util"; import { CodeEditor, Language } from "@patternfly/react-code-editor"; import { Link } from "react-router-dom"; import type ClientPolicyRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientPolicyRepresentation"; import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog"; import { useAlerts } from "../components/alert/Alerts"; import "./RealmSettingsSection.css"; export const PoliciesTab = () => { const { t } = useTranslation("realm-settings"); const adminClient = useAdminClient(); const { addAlert, addError } = useAlerts(); const [show, setShow] = useState(false); const [policies, setPolicies] = useState(); const [selectedPolicy, setSelectedPolicy] = useState(); useFetch( () => adminClient.clientPolicies.listPolicies(), (policies) => setPolicies(policies.policies), [] ); const loader = async () => policies ?? []; const code = useMemo(() => prettyPrintJSON(policies), [policies]); const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({ titleKey: t("deleteClientPolicyConfirmTitle"), messageKey: t("deleteClientPolicyConfirm", { policyName: selectedPolicy?.name, }), continueButtonLabel: t("delete"), continueButtonVariant: ButtonVariant.danger, onConfirm: async () => { try { // delete client policy here addAlert(t("deleteClientPolicySuccess"), AlertVariant.success); } catch (error) { addError(t("deleteClientPolicyError"), error); } }, }); if (!policies) { return (
); } return ( <> {t("policiesConfigType")} setShow(false)} label={t("policiesConfigTypes.formView")} id="formView-policiesView" className="kc-form-radio-btn pf-u-mr-sm pf-u-ml-sm" /> setShow(true)} label={t("policiesConfigTypes.jsonEditor")} id="jsonEditor-policiesView" className="kc-editor-radio-btn" /> {!show ? ( } ariaLabelKey="realm-settings:clientPolicies" searchPlaceholderKey="realm-settings:clientPolicySearch" loader={loader} toolbarItem={ } actions={[ { title: t("common:delete"), onRowClick: (item) => { toggleDeleteDialog(); setSelectedPolicy(item); }, }, ]} columns={[ { name: "name", }, { name: "enabled", cellFormatters: [upperCaseFormatter()], }, { name: "description", }, ]} /> ) : ( <>
)} ); };