| 1 |
import React from "react"; |
| 2 |
// PatternFly |
| 3 |
import { |
| 4 |
Flex, |
| 5 |
FlexItem, |
| 6 |
Form, |
| 7 |
FormGroup, |
| 8 |
Sidebar, |
| 9 |
SidebarContent, |
| 10 |
SidebarPanel, |
| 11 |
} from "@patternfly/react-core"; |
| 12 |
// Redux |
| 13 |
import { useAppDispatch } from "src/store/hooks"; |
| 14 |
// Hooks |
| 15 |
import { useCertMapConfigData } from "src/hooks/useCertMapConfigData"; |
| 16 |
import { addAlert } from "src/store/Global/alerts-slice"; |
| 17 |
import useUpdateRoute from "src/hooks/useUpdateRoute"; |
| 18 |
// RPC |
| 19 |
import { |
| 20 |
CertMapConfigPayload, |
| 21 |
useCertMapConfigModMutation, |
| 22 |
} from "src/services/rpcCertMapping"; |
| 23 |
// Utils |
| 24 |
import { certMapConfigAsRecord } from "src/utils/certMappingUtils"; |
| 25 |
// Icons |
| 26 |
import { OutlinedQuestionCircleIcon } from "@patternfly/react-icons"; |
| 27 |
// Components |
| 28 |
import { NotFound } from "src/components/errors/PageErrors"; |
| 29 |
import DataSpinner from "src/components/layouts/DataSpinner"; |
| 30 |
import HelpTextWithIconLayout from "src/components/layouts/HelpTextWithIconLayout"; |
| 31 |
import SecondaryButton from "src/components/layouts/SecondaryButton"; |
| 32 |
import PageWithGrayBorderLayout from "src/components/layouts/PageWithGrayBorderLayout"; |
| 33 |
import IpaCheckbox from "src/components/Form/IpaCheckbox"; |
| 34 |
|
| 35 |
const CertificateMappingGlobalConfig = () => { |
| 36 |
const dispatch = useAppDispatch(); |
| 37 |
|
| 38 |
// API calls |
| 39 |
const certMapConfigData = useCertMapConfigData(); |
| 40 |
const [saveConfigInfo] = useCertMapConfigModMutation(); |
| 41 |
|
| 42 |
// Update current route data to Redux and highlight the current page in the Nav bar |
| 43 |
const { browserTitle } = useUpdateRoute({ |
| 44 |
pathname: "cert-id-mapping-global-config", |
| 45 |
}); |
| 46 |
|
| 47 |
// Set the page title to be shown in the browser tab |
| 48 |
React.useEffect(() => { |
| 49 |
document.title = browserTitle; |
| 50 |
}, [browserTitle]); |
| 51 |
|
| 52 |
// States |
| 53 |
const [isDataLoading, setIsDataLoading] = React.useState(false); |
| 54 |
|
| 55 |
// Get 'ipaObject' and 'recordOnChange' to use in 'IpaTextInput' |
| 56 |
const { ipaObject, recordOnChange } = certMapConfigAsRecord( |
| 57 |
certMapConfigData.certMapConfig, |
| 58 |
certMapConfigData.setCertMapConfig |
| 59 |
); |
| 60 |
|
| 61 |
// 'Revert' handler method |
| 62 |
const onRevert = () => { |
| 63 |
certMapConfigData.setCertMapConfig(certMapConfigData.originalCertMapConfig); |
| 64 |
certMapConfigData.refetch(); |
| 65 |
dispatch( |
| 66 |
addAlert({ |
| 67 |
name: "revert-success", |
| 68 |
title: "Certificate mapping configuration data reverted", |
| 69 |
variant: "success", |
| 70 |
}) |
| 71 |
); |
| 72 |
}; |
| 73 |
|
| 74 |
// on Save handler method |
| 75 |
const onSave = () => { |
| 76 |
setIsDataLoading(true); |
| 77 |
const modifiedValues = certMapConfigData.modifiedValues(); |
| 78 |
|
| 79 |
const payload: CertMapConfigPayload = { |
| 80 |
ipacertmappromptusername: |
| 81 |
modifiedValues.ipacertmappromptusername as boolean, |
| 82 |
}; |
| 83 |
|
| 84 |
saveConfigInfo(payload).then((response) => { |
| 85 |
if ("data" in response) { |
| 86 |
const data = response.data; |
| 87 |
if (data?.error) { |
| 88 |
dispatch( |
| 89 |
addAlert({ |
| 90 |
name: "error", |
| 91 |
title: (data.error as Error).message, |
| 92 |
variant: "danger", |
| 93 |
}) |
| 94 |
); |
| 95 |
} |
| 96 |
if (data?.result) { |
| 97 |
certMapConfigData.setCertMapConfig(data.result.result); |
| 98 |
dispatch( |
| 99 |
addAlert({ |
| 100 |
name: "success", |
| 101 |
title: "Certificate mapping configuration updated", |
| 102 |
variant: "success", |
| 103 |
}) |
| 104 |
); |
| 105 |
// Reset values. Disable 'revert' and 'save' buttons |
| 106 |
certMapConfigData.resetValues(); |
| 107 |
} |
| 108 |
} |
| 109 |
setIsDataLoading(false); |
| 110 |
}); |
| 111 |
}; |
| 112 |
|
| 113 |
// Toolbar |
| 114 |
const toolbarFields = [ |
| 115 |
{ |
| 116 |
key: 0, |
| 117 |
element: ( |
| 118 |
<SecondaryButton |
| 119 |
dataCy="certificate-mapping-global-config-button-refresh" |
| 120 |
onClickHandler={certMapConfigData.refetch} |
| 121 |
> |
| 122 |
Refresh |
| 123 |
</SecondaryButton> |
| 124 |
), |
| 125 |
}, |
| 126 |
{ |
| 127 |
key: 1, |
| 128 |
element: ( |
| 129 |
<SecondaryButton |
| 130 |
dataCy="certificate-mapping-global-config-button-revert" |
| 131 |
isDisabled={!certMapConfigData.modified || isDataLoading} |
| 132 |
onClickHandler={onRevert} |
| 133 |
> |
| 134 |
Revert |
| 135 |
</SecondaryButton> |
| 136 |
), |
| 137 |
}, |
| 138 |
{ |
| 139 |
key: 2, |
| 140 |
element: ( |
| 141 |
<SecondaryButton |
| 142 |
dataCy="certificate-mapping-global-config-button-save" |
| 143 |
isDisabled={!certMapConfigData.modified || isDataLoading} |
| 144 |
onClickHandler={onSave} |
| 145 |
> |
| 146 |
Save |
| 147 |
</SecondaryButton> |
| 148 |
), |
| 149 |
}, |
| 150 |
]; |
| 151 |
|
| 152 |
// Handling of the API data |
| 153 |
if ( |
| 154 |
certMapConfigData.isLoading || |
| 155 |
!certMapConfigData.certMapConfig || |
| 156 |
!certMapConfigData.metadata |
| 157 |
) { |
| 158 |
return <DataSpinner />; |
| 159 |
} |
| 160 |
|
| 161 |
// Show the 'NotFound' page if the host is not found |
| 162 |
if ( |
| 163 |
!certMapConfigData.isLoading && |
| 164 |
Object.keys(certMapConfigData.certMapConfig).length === 0 |
| 165 |
) { |
| 166 |
return <NotFound />; |
| 167 |
} |
| 168 |
|
| 169 |
// Return component |
| 170 |
return ( |
| 171 |
<PageWithGrayBorderLayout |
| 172 |
id="certificate-id-mapping-global-config-page" |
| 173 |
pageTitle="Certificate Identity Mapping Global Configuration" |
| 174 |
toolbarItems={toolbarFields} |
| 175 |
> |
| 176 |
<Sidebar isPanelRight className="pf-v6-u-mb-0"> |
| 177 |
<SidebarPanel variant="sticky"> |
| 178 |
<HelpTextWithIconLayout |
| 179 |
textContent="Help" |
| 180 |
icon={ |
| 181 |
<OutlinedQuestionCircleIcon className="pf-v6-u-primary-color-100 pf-v6-u-mr-sm" /> |
| 182 |
} |
| 183 |
/> |
| 184 |
</SidebarPanel> |
| 185 |
<SidebarContent className="pf-v6-u-mr-xl"> |
| 186 |
<Flex direction={{ default: "column", lg: "row" }}> |
| 187 |
<FlexItem flex={{ default: "flex_1" }}> |
| 188 |
<Form className="pf-v6-u-mb-lg"> |
| 189 |
<FormGroup fieldId="ipacertmappromptusername" role="group"> |
| 190 |
<IpaCheckbox |
| 191 |
dataCy="certificate-mapping-global-config-checkbox-ipacertmappromptusername" |
| 192 |
name="ipacertmappromptusername" |
| 193 |
value={String( |
| 194 |
certMapConfigData.certMapConfig.ipacertmappromptusername |
| 195 |
)} |
| 196 |
text="Prompt for the username" |
| 197 |
ipaObject={ipaObject} |
| 198 |
onChange={recordOnChange} |
| 199 |
objectName="certmapconfig" |
| 200 |
metadata={certMapConfigData.metadata} |
| 201 |
/> |
| 202 |
</FormGroup> |
| 203 |
</Form> |
| 204 |
</FlexItem> |
| 205 |
</Flex> |
| 206 |
</SidebarContent> |
| 207 |
</Sidebar> |
| 208 |
</PageWithGrayBorderLayout> |
| 209 |
); |
| 210 |
}; |
| 211 |
|
| 212 |
export default CertificateMappingGlobalConfig; |