| 1 |
import React from "react"; |
| 2 |
// PatternFly |
| 3 |
import { |
| 4 |
Button, |
| 5 |
Flex, |
| 6 |
FlexItem, |
| 7 |
Form, |
| 8 |
FormGroup, |
| 9 |
Sidebar, |
| 10 |
SidebarContent, |
| 11 |
SidebarPanel, |
| 12 |
} from "@patternfly/react-core"; |
| 13 |
// Data types |
| 14 |
import { Metadata } from "src/utils/datatypes/globalDataTypes"; |
| 15 |
// Redux |
| 16 |
import { useAppDispatch } from "src/store/hooks"; |
| 17 |
// Hooks |
| 18 |
import useUpdateRoute from "src/hooks/useUpdateRoute"; |
| 19 |
import { addAlert } from "src/store/Global/alerts-slice"; |
| 20 |
// Utils |
| 21 |
import { dnsForwardZoneAsRecord } from "src/utils/dnsForwardZonesUtils"; |
| 22 |
// Icons |
| 23 |
import { OutlinedQuestionCircleIcon } from "@patternfly/react-icons"; |
| 24 |
// RPC |
| 25 |
import { |
| 26 |
DnsForwardZoneModPayload, |
| 27 |
IPAddressWithPort, |
| 28 |
useSaveDnsForwardZoneMutation, |
| 29 |
} from "src/services/rpcDnsForwardZones"; |
| 30 |
// Components |
| 31 |
import IpaTextInput from "src/components/Form/IpaTextInput/IpaTextInput"; |
| 32 |
import TabLayout from "src/components/layouts/TabLayout"; |
| 33 |
import HelpTextWithIconLayout from "src/components/layouts/HelpTextWithIconLayout"; |
| 34 |
import IpaForwardPolicy from "src/components/Form/IpaForwardPolicy"; |
| 35 |
import IPAddressWithPortInputList from "src/components/Form/IPAddressWithPortInputList"; |
| 36 |
|
| 37 |
interface DnsForwardZonesSettingsProps { |
| 38 |
dnsForwardZone: Partial<DnsForwardZoneModPayload>; |
| 39 |
originalDnsForwardZone: Partial<DnsForwardZoneModPayload>; |
| 40 |
metadata: Metadata; |
| 41 |
onDnsForwardZoneChange: ( |
| 42 |
dnsForwardZone: Partial<DnsForwardZoneModPayload> |
| 43 |
) => void; |
| 44 |
onRefresh: () => void; |
| 45 |
isModified: boolean; |
| 46 |
isDataLoading: boolean; |
| 47 |
modifiedValues: () => Partial<DnsForwardZoneModPayload>; |
| 48 |
onResetValues: () => void; |
| 49 |
pathname: string; |
| 50 |
} |
| 51 |
|
| 52 |
const DnsForwardZonesSettings = (props: DnsForwardZonesSettingsProps) => { |
| 53 |
// Alerts to show in the UI |
| 54 |
const dispatch = useAppDispatch(); |
| 55 |
|
| 56 |
// Update current route data to Redux and highlight the current page in the Nav bar |
| 57 |
useUpdateRoute({ pathname: props.pathname }); |
| 58 |
|
| 59 |
// States |
| 60 |
const [isDataLoading, setIsDataLoading] = React.useState(false); |
| 61 |
|
| 62 |
// Get 'ipaObject' and 'recordOnChange' to use in 'IpaTextInput' |
| 63 |
const { ipaObject, recordOnChange } = dnsForwardZoneAsRecord( |
| 64 |
props.dnsForwardZone, |
| 65 |
props.onDnsForwardZoneChange |
| 66 |
); |
| 67 |
|
| 68 |
// API calls |
| 69 |
const [saveDnsForwardZone] = useSaveDnsForwardZoneMutation(); |
| 70 |
|
| 71 |
// 'Revert' handler method |
| 72 |
const onRevert = () => { |
| 73 |
props.onDnsForwardZoneChange(props.originalDnsForwardZone); |
| 74 |
dispatch( |
| 75 |
addAlert({ |
| 76 |
name: "revert-success", |
| 77 |
title: "DNS forward zone data reverted", |
| 78 |
variant: "success", |
| 79 |
}) |
| 80 |
); |
| 81 |
}; |
| 82 |
|
| 83 |
// on Save handler method |
| 84 |
const onSave = (event: React.FormEvent<HTMLFormElement>) => { |
| 85 |
event.preventDefault(); |
| 86 |
setIsDataLoading(true); |
| 87 |
|
| 88 |
const modifiedValues = props.modifiedValues(); |
| 89 |
const payload: DnsForwardZoneModPayload = { |
| 90 |
idnsname: ipaObject.idnsname, |
| 91 |
...(modifiedValues.idnsforwarders && { |
| 92 |
idnsforwarders: modifiedValues.idnsforwarders, |
| 93 |
}), |
| 94 |
...(modifiedValues.idnsforwardpolicy && { |
| 95 |
idnsforwardpolicy: modifiedValues.idnsforwardpolicy, |
| 96 |
}), |
| 97 |
}; |
| 98 |
|
| 99 |
saveDnsForwardZone(payload) |
| 100 |
.then((response) => { |
| 101 |
if ("data" in response) { |
| 102 |
const data = response.data; |
| 103 |
if (data?.error) { |
| 104 |
dispatch( |
| 105 |
addAlert({ |
| 106 |
name: "error", |
| 107 |
title: (data.error as Error).message, |
| 108 |
variant: "danger", |
| 109 |
}) |
| 110 |
); |
| 111 |
} |
| 112 |
if (data?.result) { |
| 113 |
props.onDnsForwardZoneChange({ |
| 114 |
...props.dnsForwardZone, |
| 115 |
...modifiedValues, |
| 116 |
}); |
| 117 |
dispatch( |
| 118 |
addAlert({ |
| 119 |
name: "success", |
| 120 |
title: "DNS forward zone data updated", |
| 121 |
variant: "success", |
| 122 |
}) |
| 123 |
); |
| 124 |
props.onRefresh(); |
| 125 |
} |
| 126 |
} |
| 127 |
}) |
| 128 |
.finally(() => { |
| 129 |
setIsDataLoading(false); |
| 130 |
}); |
| 131 |
}; |
| 132 |
|
| 133 |
// Toolbar |
| 134 |
const toolbarFields = [ |
| 135 |
{ |
| 136 |
key: 0, |
| 137 |
element: ( |
| 138 |
<Button |
| 139 |
variant="secondary" |
| 140 |
data-cy="dns-forward-zones-tab-settings-button-refresh" |
| 141 |
onClick={props.onRefresh} |
| 142 |
> |
| 143 |
Refresh |
| 144 |
</Button> |
| 145 |
), |
| 146 |
}, |
| 147 |
{ |
| 148 |
key: 1, |
| 149 |
element: ( |
| 150 |
<Button |
| 151 |
variant="secondary" |
| 152 |
data-cy="dns-forward-zones-tab-settings-button-revert" |
| 153 |
isDisabled={!props.isModified || isDataLoading} |
| 154 |
onClick={onRevert} |
| 155 |
> |
| 156 |
Revert |
| 157 |
</Button> |
| 158 |
), |
| 159 |
}, |
| 160 |
{ |
| 161 |
key: 2, |
| 162 |
element: ( |
| 163 |
<Button |
| 164 |
variant="primary" |
| 165 |
data-cy="dns-forward-zones-tab-settings-button-save" |
| 166 |
isDisabled={!props.isModified || isDataLoading} |
| 167 |
type="submit" |
| 168 |
form="dns-forward-zones-tab-settings-form" |
| 169 |
> |
| 170 |
Save |
| 171 |
</Button> |
| 172 |
), |
| 173 |
}, |
| 174 |
]; |
| 175 |
|
| 176 |
return ( |
| 177 |
<TabLayout id="settings-page" toolbarItems={toolbarFields}> |
| 178 |
<Sidebar isPanelRight> |
| 179 |
<SidebarPanel variant="sticky"> |
| 180 |
<HelpTextWithIconLayout |
| 181 |
textContent="Help" |
| 182 |
icon={ |
| 183 |
<OutlinedQuestionCircleIcon className="pf-v6-u-primary-color-100 pf-v6-u-mr-sm" /> |
| 184 |
} |
| 185 |
/> |
| 186 |
</SidebarPanel> |
| 187 |
<SidebarContent className="pf-v6-u-mr-xl"> |
| 188 |
<Flex direction={{ default: "column", lg: "row" }}> |
| 189 |
<FlexItem flex={{ default: "flex_1" }}> |
| 190 |
<Form |
| 191 |
className="pf-v6-u-mb-lg" |
| 192 |
id="dns-forward-zones-tab-settings-form" |
| 193 |
onSubmit={onSave} |
| 194 |
> |
| 195 |
<FormGroup label="Zone name" role="idnsname"> |
| 196 |
<IpaTextInput |
| 197 |
dataCy="dns-zones-tab-settings-textbox-idnsname" |
| 198 |
name={"idnsname"} |
| 199 |
ariaLabel={"Zone name text input"} |
| 200 |
ipaObject={ipaObject} |
| 201 |
onChange={recordOnChange} |
| 202 |
objectName="dnsforwardzone" |
| 203 |
metadata={props.metadata} |
| 204 |
/> |
| 205 |
</FormGroup> |
| 206 |
<FormGroup label="Forwarders" role="idnsforwarders"> |
| 207 |
<IPAddressWithPortInputList |
| 208 |
dataCy="modal-textbox-forwarders" |
| 209 |
name="idnsforwarders" |
| 210 |
ariaLabel="Forwarders text input" |
| 211 |
list={ipaObject.idnsforwarders} |
| 212 |
setList={(values: IPAddressWithPort[]) => |
| 213 |
recordOnChange({ ...ipaObject, idnsforwarders: values }) |
| 214 |
} |
| 215 |
/> |
| 216 |
</FormGroup> |
| 217 |
<FormGroup label="Forward policy" role="idnsforwardpolicy"> |
| 218 |
<IpaForwardPolicy |
| 219 |
name={"idnsforwardpolicy"} |
| 220 |
ariaLabel={"Forward policy radio group"} |
| 221 |
ipaObject={ipaObject} |
| 222 |
onChange={recordOnChange} |
| 223 |
metadata={props.metadata} |
| 224 |
objectName="dnsforwardzone" |
| 225 |
/> |
| 226 |
</FormGroup> |
| 227 |
</Form> |
| 228 |
</FlexItem> |
| 229 |
</Flex> |
| 230 |
</SidebarContent> |
| 231 |
</Sidebar> |
| 232 |
</TabLayout> |
| 233 |
); |
| 234 |
}; |
| 235 |
|
| 236 |
export default DnsForwardZonesSettings; |