| 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 { DnsServer, 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 { dnsServerAsRecord } from "src/utils/dnsServersUtils"; |
| 22 |
// Icons |
| 23 |
import { OutlinedQuestionCircleIcon } from "@patternfly/react-icons"; |
| 24 |
// RPC |
| 25 |
import { |
| 26 |
DnsServerModPayload, |
| 27 |
useDnsServersModMutation, |
| 28 |
} from "src/services/rpcDnsServers"; |
| 29 |
// Components |
| 30 |
import IpaTextInput from "src/components/Form/IpaTextInput/IpaTextInput"; |
| 31 |
import TabLayout from "src/components/layouts/TabLayout"; |
| 32 |
import HelpTextWithIconLayout from "src/components/layouts/HelpTextWithIconLayout"; |
| 33 |
import IpaTextboxList from "src/components/Form/IpaTextboxList/IpaTextboxList"; |
| 34 |
import IpaForwardPolicy from "src/components/Form/IpaForwardPolicy"; |
| 35 |
|
| 36 |
interface DnsServersSettingsProps { |
| 37 |
dnsServer: Partial<DnsServer>; |
| 38 |
originalDnsServer: Partial<DnsServer>; |
| 39 |
metadata: Metadata; |
| 40 |
onDnsServerChange: (dnsServer: Partial<DnsServer>) => void; |
| 41 |
onRefresh: () => void; |
| 42 |
isModified: boolean; |
| 43 |
isDataLoading: boolean; |
| 44 |
modifiedValues: () => Partial<DnsServer>; |
| 45 |
onResetValues: () => void; |
| 46 |
pathname: string; |
| 47 |
} |
| 48 |
|
| 49 |
const DnsServersSettings = (props: DnsServersSettingsProps) => { |
| 50 |
const dispatch = useAppDispatch(); |
| 51 |
|
| 52 |
// Update current route data to Redux and highlight the current page in the Nav bar |
| 53 |
useUpdateRoute({ pathname: props.pathname }); |
| 54 |
|
| 55 |
// States |
| 56 |
const [isDataLoading, setIsDataLoading] = React.useState(false); |
| 57 |
|
| 58 |
// Computed state |
| 59 |
const dnsServerId: string = props.dnsServer.idnsserverid || ""; |
| 60 |
|
| 61 |
// Get 'ipaObject' and 'recordOnChange' to use in 'IpaTextInput' |
| 62 |
const { ipaObject, recordOnChange } = dnsServerAsRecord( |
| 63 |
props.dnsServer, |
| 64 |
props.onDnsServerChange |
| 65 |
); |
| 66 |
|
| 67 |
// API calls |
| 68 |
const [saveDnsServer] = useDnsServersModMutation(); |
| 69 |
|
| 70 |
// 'Revert' handler method |
| 71 |
const onRevert = () => { |
| 72 |
props.onDnsServerChange(props.originalDnsServer); |
| 73 |
props.onRefresh(); |
| 74 |
dispatch( |
| 75 |
addAlert({ |
| 76 |
name: "revert-success", |
| 77 |
title: "DNS server data reverted", |
| 78 |
variant: "success", |
| 79 |
}) |
| 80 |
); |
| 81 |
}; |
| 82 |
|
| 83 |
// Helper method to build the payload based on values |
| 84 |
const buildPayload = ( |
| 85 |
modifiedValues: Partial<DnsServer>, |
| 86 |
keyArray: string[] |
| 87 |
): DnsServerModPayload => { |
| 88 |
const payload: DnsServerModPayload = { |
| 89 |
idnsserverid: dnsServerId, |
| 90 |
}; |
| 91 |
|
| 92 |
keyArray.forEach((key) => { |
| 93 |
// Modified values are either the value (in string) or an array ([]) when set to empty string |
| 94 |
if (modifiedValues[key] !== undefined) { |
| 95 |
if (modifiedValues[key] === "") { |
| 96 |
payload[key] = []; |
| 97 |
} else { |
| 98 |
payload[key] = modifiedValues[key]; |
| 99 |
} |
| 100 |
} |
| 101 |
}); |
| 102 |
return payload; |
| 103 |
}; |
| 104 |
|
| 105 |
// on Save handler method |
| 106 |
const onSave = (event: React.FormEvent<HTMLFormElement>) => { |
| 107 |
event.preventDefault(); |
| 108 |
setIsDataLoading(true); |
| 109 |
const modifiedValues = props.modifiedValues(); |
| 110 |
|
| 111 |
const payload = buildPayload(modifiedValues, [ |
| 112 |
"idnssoamname", |
| 113 |
"idnsforwarders", |
| 114 |
"idnsforwardpolicy", |
| 115 |
]); |
| 116 |
|
| 117 |
saveDnsServer(payload).then((response) => { |
| 118 |
if ("data" in response) { |
| 119 |
const data = response.data; |
| 120 |
if (data?.error) { |
| 121 |
dispatch( |
| 122 |
addAlert({ |
| 123 |
name: "error", |
| 124 |
title: (data.error as Error).message, |
| 125 |
variant: "danger", |
| 126 |
}) |
| 127 |
); |
| 128 |
} |
| 129 |
if (data?.result) { |
| 130 |
props.onDnsServerChange(data.result.result); |
| 131 |
dispatch( |
| 132 |
addAlert({ |
| 133 |
name: "success", |
| 134 |
title: "DNS server data updated", |
| 135 |
variant: "success", |
| 136 |
}) |
| 137 |
); |
| 138 |
props.onRefresh(); |
| 139 |
} |
| 140 |
} |
| 141 |
setIsDataLoading(false); |
| 142 |
}); |
| 143 |
}; |
| 144 |
|
| 145 |
// Toolbar |
| 146 |
const toolbarFields = [ |
| 147 |
{ |
| 148 |
key: 0, |
| 149 |
element: ( |
| 150 |
<Button |
| 151 |
variant="secondary" |
| 152 |
data-cy="dns-servers-tab-settings-button-refresh" |
| 153 |
onClick={props.onRefresh} |
| 154 |
> |
| 155 |
Refresh |
| 156 |
</Button> |
| 157 |
), |
| 158 |
}, |
| 159 |
{ |
| 160 |
key: 1, |
| 161 |
element: ( |
| 162 |
<Button |
| 163 |
variant="secondary" |
| 164 |
data-cy="dns-servers-tab-settings-button-revert" |
| 165 |
isDisabled={!props.isModified || isDataLoading} |
| 166 |
onClick={onRevert} |
| 167 |
> |
| 168 |
Revert |
| 169 |
</Button> |
| 170 |
), |
| 171 |
}, |
| 172 |
{ |
| 173 |
key: 2, |
| 174 |
element: ( |
| 175 |
<Button |
| 176 |
variant="primary" |
| 177 |
data-cy="dns-servers-tab-settings-button-save" |
| 178 |
isDisabled={!props.isModified || isDataLoading} |
| 179 |
type="submit" |
| 180 |
form="dns-servers-tab-settings-form" |
| 181 |
> |
| 182 |
Save |
| 183 |
</Button> |
| 184 |
), |
| 185 |
}, |
| 186 |
]; |
| 187 |
|
| 188 |
return ( |
| 189 |
<TabLayout id="settings-page" toolbarItems={toolbarFields}> |
| 190 |
<Sidebar isPanelRight> |
| 191 |
<SidebarPanel variant="sticky"> |
| 192 |
<HelpTextWithIconLayout |
| 193 |
textContent="Help" |
| 194 |
icon={ |
| 195 |
<OutlinedQuestionCircleIcon className="pf-v6-u-primary-color-100 pf-v6-u-mr-sm" /> |
| 196 |
} |
| 197 |
/> |
| 198 |
</SidebarPanel> |
| 199 |
<SidebarContent className="pf-v6-u-mr-xl"> |
| 200 |
<Flex direction={{ default: "column", lg: "row" }}> |
| 201 |
<FlexItem flex={{ default: "flex_1" }}> |
| 202 |
<Form |
| 203 |
className="pf-v6-u-mb-lg" |
| 204 |
id="dns-servers-tab-settings-form" |
| 205 |
onSubmit={onSave} |
| 206 |
> |
| 207 |
<FormGroup label="SOA name" role="idnssoamname"> |
| 208 |
<IpaTextInput |
| 209 |
dataCy="dns-servers-tab-settings-textbox-idnssoamname" |
| 210 |
name={"idnssoamname"} |
| 211 |
ariaLabel={"SOA name text input"} |
| 212 |
ipaObject={ipaObject} |
| 213 |
onChange={recordOnChange} |
| 214 |
objectName="dnsserver" |
| 215 |
metadata={props.metadata} |
| 216 |
/> |
| 217 |
</FormGroup> |
| 218 |
<FormGroup label="Server name" role="idnsserverid"> |
| 219 |
<IpaTextInput |
| 220 |
dataCy="dns-servers-tab-settings-textbox-idnsserverid" |
| 221 |
name={"idnsserverid"} |
| 222 |
ariaLabel={"Server name text input"} |
| 223 |
ipaObject={ipaObject} |
| 224 |
onChange={recordOnChange} |
| 225 |
objectName="dnsserver" |
| 226 |
metadata={props.metadata} |
| 227 |
/> |
| 228 |
</FormGroup> |
| 229 |
<FormGroup label="Forwarders" role="idnsforwarders"> |
| 230 |
<IpaTextboxList |
| 231 |
dataCy="dns-servers-tab-settings-textbox-idnsforwarders" |
| 232 |
ipaObject={ipaObject} |
| 233 |
setIpaObject={recordOnChange} |
| 234 |
name="idnsforwarders" |
| 235 |
ariaLabel={"Forwarders text input"} |
| 236 |
/> |
| 237 |
</FormGroup> |
| 238 |
<FormGroup label="Forward policy" role="idnsforwardpolicy"> |
| 239 |
<IpaForwardPolicy |
| 240 |
name={"idnsforwardpolicy"} |
| 241 |
ariaLabel={"Forward policy radio group"} |
| 242 |
ipaObject={ipaObject} |
| 243 |
onChange={recordOnChange} |
| 244 |
metadata={props.metadata} |
| 245 |
objectName="dnsserver" |
| 246 |
/> |
| 247 |
</FormGroup> |
| 248 |
</Form> |
| 249 |
</FlexItem> |
| 250 |
</Flex> |
| 251 |
</SidebarContent> |
| 252 |
</Sidebar> |
| 253 |
</TabLayout> |
| 254 |
); |
| 255 |
}; |
| 256 |
|
| 257 |
export default DnsServersSettings; |