import React from "react"; // PatternFly import { DropdownItem, Flex, FlexItem, Form, FormGroup, Sidebar, SidebarContent, SidebarPanel, } from "@patternfly/react-core"; // Data types import { CertificateMapping, Metadata, } from "src/utils/datatypes/globalDataTypes"; // Redux import { useAppDispatch } from "src/store/hooks"; // Hooks import useUpdateRoute from "src/hooks/useUpdateRoute"; import { addAlert } from "src/store/Global/alerts-slice"; // Utils import { certMapRuleAsRecord } from "src/utils/certMappingUtils"; // Icons import { OutlinedQuestionCircleIcon } from "@patternfly/react-icons"; // RPC import { CertModPayload, useCertMapRuleModMutation, } from "src/services/rpcCertMapping"; // Components import IpaTextInput from "src/components/Form/IpaTextInput/IpaTextInput"; import TabLayout from "src/components/layouts/TabLayout"; import SecondaryButton from "src/components/layouts/SecondaryButton"; import HelpTextWithIconLayout from "src/components/layouts/HelpTextWithIconLayout"; import KebabLayout from "src/components/layouts/KebabLayout"; import IpaTextArea from "src/components/Form/IpaTextArea"; import PopoverWithIconLayout from "src/components/layouts/PopoverWithIconLayout"; import IpaTextboxList from "src/components/Form/IpaTextboxList"; import IpaNumberInput from "src/components/Form/IpaNumberInput"; import EnableDisableRuleModal from "src/components/modals/CertificateMapping/EnableDisableRuleModal"; import DeleteRuleModal from "src/components/modals/CertificateMapping/DeleteRuleModal"; interface CertificateMappingSettingsProps { certMapping: Partial; originalCertMapping: Partial; metadata: Metadata; onCertMappingChange: (idpRef: Partial) => void; onRefresh: () => void; isModified: boolean; isDataLoading?: boolean; modifiedValues: () => Partial; onResetValues: () => void; pathname: string; } const CertificateMappingSettings = (props: CertificateMappingSettingsProps) => { const dispatch = useAppDispatch(); // Update current route data to Redux and highlight the current page in the Nav bar useUpdateRoute({ pathname: props.pathname }); // States const [isDataLoading, setIsDataLoading] = React.useState(false); const [isRuleEnabled, setIsRuleEnabled] = React.useState(true); // Get 'ipaObject' and 'recordOnChange' to use in 'IpaTextInput' const { ipaObject, recordOnChange } = certMapRuleAsRecord( props.certMapping, props.onCertMappingChange ); // Determine the rule status React.useEffect(() => { if (props.certMapping) { setIsRuleEnabled( props.certMapping.ipaenabledflag?.toString() === "true" ? true : false ); } }, [props.certMapping]); // API calls const [saveCertMapping] = useCertMapRuleModMutation(); // 'Revert' handler method const onRevert = () => { props.onCertMappingChange(props.originalCertMapping); props.onRefresh(); dispatch( addAlert({ name: "revert-success", title: "Certificate mapping rule data reverted", variant: "success", }) ); }; // Helper method to build the payload based on values const buildPayload = ( modifiedValues: Partial, keyArray: string[] ): CertModPayload => { const payload: CertModPayload = { ruleId: props.certMapping.cn?.toString() as string, }; keyArray.forEach((key) => { // Modified values are either the value (in string) or an array ([]) when set to empty string if (modifiedValues[key] !== undefined) { if (modifiedValues[key] === "") { payload[key] = []; } else { payload[key] = modifiedValues[key]; } } }); return payload; }; // on Save handler method const onSave = () => { setIsDataLoading(true); const modifiedValues = props.modifiedValues(); // Generate payload const payload = buildPayload(modifiedValues, [ "description", "ipacertmapmaprule", "ipacertmapmatchrule", "associateddomain", "ipacertmappriority", ]); saveCertMapping(payload).then((response) => { if ("data" in response) { const data = response.data; if (data?.error) { dispatch( addAlert({ name: "error", title: (data.error as Error).message, variant: "danger", }) ); } if (data?.result) { props.onCertMappingChange(data.result.result); dispatch( addAlert({ name: "success", title: response.data.result.summary, variant: "success", }) ); // Disable 'revert' and 'save' buttons props.onRefresh(); } } setIsDataLoading(false); }); }; // Kebab const [isKebabOpen, setIsKebabOpen] = React.useState(false); const kebabItems = [ setIsEnableDisableOpen(true)} > Enable , setIsEnableDisableOpen(true)} > Disable , setIsDeleteOpen(true)} > Delete , ]; // Toolbar const toolbarFields = [ { key: 0, element: ( Refresh ), }, { key: 1, element: ( Revert ), }, { key: 2, element: ( Save ), }, { key: 3, element: ( setIsKebabOpen(!isKebabOpen)} onKebabToggle={() => setIsKebabOpen(!isKebabOpen)} idKebab="toggle-action-buttons" isKebabOpen={isKebabOpen} dropdownItems={kebabItems} isDisabled={isDataLoading} /> ), }, ]; // Tooltip messages const mappingRuleMessage = "Rule used to map the certificate with a user entry"; const matchingRuleMessage = "Rule used to check if a certificate can be used for authentication"; const domainNameMessage = "Domain where the user entry will be searched"; const priorityMessage = "Priority of the rule (higher number means lower priority)"; // Modals const [isEnableDisableOpen, setIsEnableDisableOpen] = React.useState(false); const [isDeleteOpen, setIsDeleteOpen] = React.useState(false); // Render component return ( <> } />
} > } > } > } >
setIsEnableDisableOpen(false)} ruleId={props.certMapping.cn?.toString() as string} operation={isRuleEnabled ? "disable" : "enable"} setIsLoading={setIsDataLoading} onRefresh={props.onRefresh} /> setIsDeleteOpen(false)} ruleId={props.certMapping.cn?.toString() as string} setIsLoading={setIsDataLoading} onRefresh={props.onRefresh} pathToMainPage={props.pathname} /> ); }; export default CertificateMappingSettings;