| 1 |
import React from "react"; |
| 2 |
// PatternFly |
| 3 |
import { |
| 4 |
Flex, |
| 5 |
FlexItem, |
| 6 |
PageSection, |
| 7 |
PaginationVariant, |
| 8 |
ToolbarItemVariant, |
| 9 |
} from "@patternfly/react-core"; |
| 10 |
import { |
| 11 |
InnerScrollContainer, |
| 12 |
OuterScrollContainer, |
| 13 |
} from "@patternfly/react-table"; |
| 14 |
// Data types |
| 15 |
import { DNSForwardZone } from "src/utils/datatypes/globalDataTypes"; |
| 16 |
// Hooks |
| 17 |
import { addAlert } from "src/store/Global/alerts-slice"; |
| 18 |
import useUpdateRoute from "src/hooks/useUpdateRoute"; |
| 19 |
import useListPageSearchParams from "src/hooks/useListPageSearchParams"; |
| 20 |
import useApiError from "src/hooks/useApiError"; |
| 21 |
// Redux |
| 22 |
import { useAppDispatch, useAppSelector } from "src/store/hooks"; |
| 23 |
// RPC |
| 24 |
import { |
| 25 |
useGetDnsForwardZonesFullDataQuery, |
| 26 |
useSearchDnsForwardZonesEntriesMutation, |
| 27 |
} from "src/services/rpcDnsForwardZones"; |
| 28 |
// Utils |
| 29 |
import { isDnsForwardZoneSelectable } from "src/utils/utils"; |
| 30 |
// Components |
| 31 |
import ToolbarLayout, { |
| 32 |
ToolbarItem, |
| 33 |
} from "src/components/layouts/ToolbarLayout"; |
| 34 |
import SearchInputLayout from "src/components/layouts/SearchInputLayout"; |
| 35 |
import SecondaryButton from "src/components/layouts/SecondaryButton"; |
| 36 |
import HelpTextWithIconLayout from "src/components/layouts/HelpTextWithIconLayout"; |
| 37 |
import PaginationLayout from "src/components/layouts/PaginationLayout"; |
| 38 |
import TitleLayout from "src/components/layouts/TitleLayout"; |
| 39 |
import GlobalErrors from "src/components/errors/GlobalErrors"; |
| 40 |
import MainTable from "src/components/tables/MainTable"; |
| 41 |
import BulkSelectorPrep from "src/components/BulkSelectorPrep"; |
| 42 |
import { apiToDnsForwardZone } from "src/utils/dnsForwardZonesUtils"; |
| 43 |
import EnableDisableDnsForwardZonesModal from "src/components/modals/DnsZones/EnableDisableDnsForwardZonesModal"; |
| 44 |
import DeleteDnsForwardZonesModal from "src/components/modals/DnsZones/DeleteDnsForwardZonesModal"; |
| 45 |
import AddDnsForwardZoneModal from "src/components/modals/DnsZones/AddDnsForwardZoneModal"; |
| 46 |
|
| 47 |
const DnsForwardZones = () => { |
| 48 |
const dispatch = useAppDispatch(); |
| 49 |
|
| 50 |
// Update current route data to Redux and highlight the current page in the Nav bar |
| 51 |
const { browserTitle } = useUpdateRoute({ |
| 52 |
pathname: "dns-forward-zones", |
| 53 |
}); |
| 54 |
|
| 55 |
// Set the page title to be shown in the browser tab |
| 56 |
React.useEffect(() => { |
| 57 |
document.title = browserTitle; |
| 58 |
}, [browserTitle]); |
| 59 |
|
| 60 |
// Retrieve API version from environment data |
| 61 |
const apiVersion = useAppSelector( |
| 62 |
(state) => state.global.environment.api_version |
| 63 |
) as string; |
| 64 |
|
| 65 |
// URL parameters: page number, page size, search value |
| 66 |
const { page, setPage, perPage, setPerPage, searchValue, setSearchValue } = |
| 67 |
useListPageSearchParams(); |
| 68 |
|
| 69 |
// Handle API calls errors |
| 70 |
const globalErrors = useApiError([]); |
| 71 |
|
| 72 |
// Page indexes |
| 73 |
const firstUserIdx = (page - 1) * perPage; |
| 74 |
const lastUserIdx = page * perPage; |
| 75 |
|
| 76 |
// States |
| 77 |
const [dnsForwardZones, setDnsForwardZones] = React.useState< |
| 78 |
DNSForwardZone[] |
| 79 |
>([]); |
| 80 |
const [isSearchDisabled, setIsSearchDisabled] = |
| 81 |
React.useState<boolean>(false); |
| 82 |
const [totalCount, setTotalCount] = React.useState<number>(0); |
| 83 |
|
| 84 |
// API calls |
| 85 |
const forwardDnsZonesResponse = useGetDnsForwardZonesFullDataQuery({ |
| 86 |
searchValue, |
| 87 |
apiVersion, |
| 88 |
startIdx: firstUserIdx, |
| 89 |
stopIdx: lastUserIdx, |
| 90 |
}); |
| 91 |
|
| 92 |
const { data, isLoading, error } = forwardDnsZonesResponse; |
| 93 |
|
| 94 |
// Handle data when the API call is finished |
| 95 |
React.useEffect(() => { |
| 96 |
if (forwardDnsZonesResponse.isFetching) { |
| 97 |
setShowTableRows(false); |
| 98 |
// Reset selected elements on refresh |
| 99 |
setTotalCount(0); |
| 100 |
globalErrors.clear(); |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
// API response: Success |
| 105 |
if ( |
| 106 |
forwardDnsZonesResponse.isSuccess && |
| 107 |
forwardDnsZonesResponse.data && |
| 108 |
data != undefined |
| 109 |
) { |
| 110 |
const listResult = data.result.results; |
| 111 |
const elementsList: DNSForwardZone[] = ( |
| 112 |
listResult as unknown as Record<string, unknown>[] |
| 113 |
) |
| 114 |
.filter((result) => "result" in result) |
| 115 |
.map((result) => apiToDnsForwardZone(result.result)); |
| 116 |
|
| 117 |
setTotalCount(forwardDnsZonesResponse.data.result.totalCount); |
| 118 |
// Update the list of elements |
| 119 |
setDnsForwardZones(elementsList); |
| 120 |
// Show table elements |
| 121 |
setShowTableRows(true); |
| 122 |
} |
| 123 |
}, [forwardDnsZonesResponse]); |
| 124 |
|
| 125 |
// Selected elements |
| 126 |
const [selectedElements, setSelectedElements] = React.useState< |
| 127 |
DNSForwardZone[] |
| 128 |
>([]); |
| 129 |
const [selectedPerPage, setSelectedPerPage] = React.useState<number>(0); |
| 130 |
|
| 131 |
// Refresh button handling |
| 132 |
const refreshData = () => { |
| 133 |
// Hide table |
| 134 |
setShowTableRows(false); |
| 135 |
|
| 136 |
// Reset selected elements on refresh |
| 137 |
setTotalCount(0); |
| 138 |
setSelectedElements([]); |
| 139 |
|
| 140 |
forwardDnsZonesResponse.refetch().then(() => { |
| 141 |
setShowTableRows(true); |
| 142 |
}); |
| 143 |
}; |
| 144 |
|
| 145 |
// 'Delete' button state |
| 146 |
const [isDeleteButtonDisabled, setIsDeleteButtonDisabled] = |
| 147 |
React.useState<boolean>(true); |
| 148 |
|
| 149 |
const [isDeletion, setIsDeletion] = React.useState<boolean>(false); |
| 150 |
|
| 151 |
// 'Enable' button state |
| 152 |
const [isEnableButtonDisabled, setIsEnableButtonDisabled] = |
| 153 |
React.useState<boolean>(true); |
| 154 |
|
| 155 |
// 'Disable' button state |
| 156 |
const [isDisableButtonDisabled, setIsDisableButtonDisabled] = |
| 157 |
React.useState<boolean>(true); |
| 158 |
|
| 159 |
// Table-related shared functionality |
| 160 |
// - Selectable checkboxes on table |
| 161 |
const selectableDnsZonesTable = dnsForwardZones.filter( |
| 162 |
isDnsForwardZoneSelectable |
| 163 |
); // elements per Table |
| 164 |
|
| 165 |
const updateSelectedDnsZones = ( |
| 166 |
dnsZones: DNSForwardZone[], |
| 167 |
isSelected: boolean |
| 168 |
) => { |
| 169 |
if (isSelected) { |
| 170 |
setSelectedElements((current) => [...new Set([...current, ...dnsZones])]); |
| 171 |
} else { |
| 172 |
const newSelectedDnsZones = selectedElements.filter( |
| 173 |
(current) => |
| 174 |
!dnsZones.some((newZone) => newZone.idnsname === current.idnsname) |
| 175 |
); |
| 176 |
setSelectedElements(newSelectedDnsZones); |
| 177 |
} |
| 178 |
}; |
| 179 |
|
| 180 |
// - Helper method to set the selected entries from the table |
| 181 |
const setDnsZonesSelected = (dnsZone: DNSForwardZone, isSelecting = true) => { |
| 182 |
if (isDnsForwardZoneSelectable(dnsZone)) { |
| 183 |
updateSelectedDnsZones([dnsZone], isSelecting); |
| 184 |
} |
| 185 |
}; |
| 186 |
|
| 187 |
// Always refetch data when the component is loaded. |
| 188 |
// This ensures the data is always up-to-date. |
| 189 |
React.useEffect(() => { |
| 190 |
forwardDnsZonesResponse.refetch(); |
| 191 |
}, []); |
| 192 |
|
| 193 |
// Show table rows |
| 194 |
const [showTableRows, setShowTableRows] = React.useState(!isLoading); |
| 195 |
|
| 196 |
// Search API call |
| 197 |
const [searchEntry] = useSearchDnsForwardZonesEntriesMutation(); |
| 198 |
|
| 199 |
const submitSearchValue = () => { |
| 200 |
searchEntry({ |
| 201 |
searchValue: searchValue, |
| 202 |
apiVersion, |
| 203 |
startIdx: 0, |
| 204 |
stopIdx: 200, // Search will consider a max. of elements |
| 205 |
}).then((result) => { |
| 206 |
if ("error" in result && !("data" in result)) { |
| 207 |
const searchError = result.error; |
| 208 |
let error: string | undefined = ""; |
| 209 |
if ("error" in searchError) { |
| 210 |
error = searchError.error; |
| 211 |
} else if ("message" in searchError) { |
| 212 |
error = searchError.message; |
| 213 |
} |
| 214 |
dispatch( |
| 215 |
addAlert({ |
| 216 |
name: "submit-search-value-error", |
| 217 |
title: error || "Error when searching for elements", |
| 218 |
variant: "danger", |
| 219 |
}) |
| 220 |
); |
| 221 |
} else { |
| 222 |
// Success |
| 223 |
const dnsForwardZones = result.data?.result || []; |
| 224 |
|
| 225 |
setTotalCount(dnsForwardZones.length); |
| 226 |
setDnsForwardZones(dnsForwardZones); |
| 227 |
setShowTableRows(true); |
| 228 |
} |
| 229 |
setIsSearchDisabled(false); |
| 230 |
}); |
| 231 |
}; |
| 232 |
|
| 233 |
// Data wrappers |
| 234 |
// TODO: Better separation of concerns |
| 235 |
// - 'PaginationLayout' |
| 236 |
const paginationData = { |
| 237 |
page, |
| 238 |
perPage, |
| 239 |
updatePage: setPage, |
| 240 |
updatePerPage: setPerPage, |
| 241 |
updateSelectedPerPage: setSelectedPerPage, |
| 242 |
updateShownElementsList: setDnsForwardZones, |
| 243 |
totalCount, |
| 244 |
}; |
| 245 |
|
| 246 |
// SearchInputLayout |
| 247 |
const searchValueData = { |
| 248 |
searchValue, |
| 249 |
updateSearchValue: setSearchValue, |
| 250 |
submitSearchValue, |
| 251 |
}; |
| 252 |
|
| 253 |
// - 'BulkSelectorPrep' |
| 254 |
const bulkSelectorData = { |
| 255 |
selected: selectedElements, |
| 256 |
updateSelected: updateSelectedDnsZones, |
| 257 |
selectableTable: selectableDnsZonesTable, |
| 258 |
nameAttr: "idnsname", |
| 259 |
}; |
| 260 |
|
| 261 |
const selectedPerPageData = { |
| 262 |
selectedPerPage, |
| 263 |
updateSelectedPerPage: setSelectedPerPage, |
| 264 |
}; |
| 265 |
|
| 266 |
const [showEnableDisableModal, setShowEnableDisableModal] = |
| 267 |
React.useState(false); |
| 268 |
const [operation, setOperation] = React.useState<"enable" | "disable">( |
| 269 |
"enable" |
| 270 |
); |
| 271 |
|
| 272 |
const [showDeleteForwardZonesModal, setShowDeleteForwardZonesModal] = |
| 273 |
React.useState(false); |
| 274 |
|
| 275 |
const [showAddForwardZoneModal, setShowAddForwardZoneModal] = |
| 276 |
React.useState(false); |
| 277 |
|
| 278 |
const onEnableDisableHandler = (operation: "enable" | "disable") => { |
| 279 |
setOperation(operation); |
| 280 |
setShowEnableDisableModal(true); |
| 281 |
}; |
| 282 |
|
| 283 |
// List of Toolbar items |
| 284 |
const toolbarItems: ToolbarItem[] = [ |
| 285 |
{ |
| 286 |
key: 0, |
| 287 |
element: ( |
| 288 |
<BulkSelectorPrep |
| 289 |
list={dnsForwardZones} |
| 290 |
shownElementsList={dnsForwardZones} |
| 291 |
elementData={bulkSelectorData} |
| 292 |
buttonsData={{ |
| 293 |
updateIsDeleteButtonDisabled: setIsDeleteButtonDisabled, |
| 294 |
}} |
| 295 |
selectedPerPageData={selectedPerPageData} |
| 296 |
/> |
| 297 |
), |
| 298 |
}, |
| 299 |
{ |
| 300 |
key: 1, |
| 301 |
element: ( |
| 302 |
<SearchInputLayout |
| 303 |
name="search" |
| 304 |
ariaLabel="Search dns zones" |
| 305 |
placeholder="Search" |
| 306 |
searchValueData={searchValueData} |
| 307 |
isDisabled={isSearchDisabled} |
| 308 |
dataCy={"search"} |
| 309 |
/> |
| 310 |
), |
| 311 |
toolbarItemVariant: ToolbarItemVariant.label, |
| 312 |
toolbarItemGap: { default: "gapMd" }, |
| 313 |
}, |
| 314 |
{ |
| 315 |
key: 2, |
| 316 |
toolbarItemVariant: ToolbarItemVariant.separator, |
| 317 |
}, |
| 318 |
{ |
| 319 |
key: 3, |
| 320 |
element: ( |
| 321 |
<SecondaryButton |
| 322 |
onClickHandler={refreshData} |
| 323 |
isDisabled={!showTableRows} |
| 324 |
dataCy={"dns-forward-zones-refresh"} |
| 325 |
> |
| 326 |
Refresh |
| 327 |
</SecondaryButton> |
| 328 |
), |
| 329 |
}, |
| 330 |
{ |
| 331 |
key: 4, |
| 332 |
element: ( |
| 333 |
<SecondaryButton |
| 334 |
isDisabled={isDeleteButtonDisabled || !showTableRows} |
| 335 |
dataCy={"dns-forward-zones-delete"} |
| 336 |
onClickHandler={() => setShowDeleteForwardZonesModal(true)} |
| 337 |
> |
| 338 |
Delete |
| 339 |
</SecondaryButton> |
| 340 |
), |
| 341 |
}, |
| 342 |
{ |
| 343 |
key: 5, |
| 344 |
element: ( |
| 345 |
<SecondaryButton |
| 346 |
isDisabled={!showTableRows} |
| 347 |
dataCy={"dns-forward-zones-add"} |
| 348 |
onClickHandler={() => setShowAddForwardZoneModal(true)} |
| 349 |
> |
| 350 |
Add |
| 351 |
</SecondaryButton> |
| 352 |
), |
| 353 |
}, |
| 354 |
{ |
| 355 |
key: 6, |
| 356 |
element: ( |
| 357 |
<SecondaryButton |
| 358 |
onClickHandler={() => onEnableDisableHandler("disable")} |
| 359 |
isDisabled={isDisableButtonDisabled || !showTableRows} |
| 360 |
dataCy={"dns-forward-zones-disable"} |
| 361 |
> |
| 362 |
Disable |
| 363 |
</SecondaryButton> |
| 364 |
), |
| 365 |
}, |
| 366 |
{ |
| 367 |
key: 7, |
| 368 |
element: ( |
| 369 |
<SecondaryButton |
| 370 |
onClickHandler={() => onEnableDisableHandler("enable")} |
| 371 |
isDisabled={isEnableButtonDisabled || !showTableRows} |
| 372 |
dataCy={"dns-forward-zones-enable"} |
| 373 |
> |
| 374 |
Enable |
| 375 |
</SecondaryButton> |
| 376 |
), |
| 377 |
}, |
| 378 |
{ |
| 379 |
key: 8, |
| 380 |
toolbarItemVariant: ToolbarItemVariant.separator, |
| 381 |
}, |
| 382 |
{ |
| 383 |
key: 9, |
| 384 |
element: <HelpTextWithIconLayout textContent="Help" />, |
| 385 |
}, |
| 386 |
{ |
| 387 |
key: 10, |
| 388 |
element: ( |
| 389 |
<PaginationLayout |
| 390 |
list={dnsForwardZones} |
| 391 |
paginationData={paginationData} |
| 392 |
widgetId="pagination-options-menu-top" |
| 393 |
isCompact={true} |
| 394 |
/> |
| 395 |
), |
| 396 |
toolbarItemAlignment: { default: "alignEnd" }, |
| 397 |
}, |
| 398 |
]; |
| 399 |
|
| 400 |
// Render component |
| 401 |
return ( |
| 402 |
<div> |
| 403 |
<PageSection hasBodyWrapper={false}> |
| 404 |
<TitleLayout |
| 405 |
id="DNS forward zones page" |
| 406 |
headingLevel="h1" |
| 407 |
text="DNS forward zones" |
| 408 |
/> |
| 409 |
</PageSection> |
| 410 |
<PageSection hasBodyWrapper={false} isFilled={false}> |
| 411 |
<Flex direction={{ default: "column" }}> |
| 412 |
<FlexItem> |
| 413 |
<ToolbarLayout toolbarItems={toolbarItems} /> |
| 414 |
</FlexItem> |
| 415 |
<FlexItem style={{ flex: "0 0 auto" }}> |
| 416 |
<OuterScrollContainer> |
| 417 |
<InnerScrollContainer |
| 418 |
style={{ height: "55vh", overflow: "auto" }} |
| 419 |
> |
| 420 |
{error !== undefined && error ? ( |
| 421 |
<GlobalErrors errors={globalErrors.getAll()} /> |
| 422 |
) : ( |
| 423 |
<MainTable |
| 424 |
tableTitle="DNS forward zones table" |
| 425 |
shownElementsList={dnsForwardZones} |
| 426 |
pk="idnsname" |
| 427 |
keyNames={[ |
| 428 |
"idnsname", |
| 429 |
"idnszoneactive", |
| 430 |
"idnsforwarders", |
| 431 |
"idnsforwardpolicy", |
| 432 |
]} |
| 433 |
columnNames={[ |
| 434 |
"Zone name", |
| 435 |
"Status", |
| 436 |
"Zone Forwarders", |
| 437 |
"Forward policy", |
| 438 |
]} |
| 439 |
hasCheckboxes={true} |
| 440 |
pathname="dns-forward-zones" |
| 441 |
showTableRows={showTableRows} |
| 442 |
showLink={true} |
| 443 |
elementsData={{ |
| 444 |
isElementSelectable: isDnsForwardZoneSelectable, |
| 445 |
selectedElements, |
| 446 |
selectableElementsTable: selectableDnsZonesTable, |
| 447 |
setElementsSelected: setDnsZonesSelected, |
| 448 |
clearSelectedElements: () => setSelectedElements([]), |
| 449 |
}} |
| 450 |
buttonsData={{ |
| 451 |
updateIsDeleteButtonDisabled: (value) => |
| 452 |
setIsDeleteButtonDisabled(value), |
| 453 |
isDeletion, |
| 454 |
updateIsDeletion: (value) => setIsDeletion(value), |
| 455 |
updateIsEnableButtonDisabled: (value) => |
| 456 |
setIsEnableButtonDisabled(value), |
| 457 |
updateIsDisableButtonDisabled: (value) => |
| 458 |
setIsDisableButtonDisabled(value), |
| 459 |
isDisableEnableOp: true, |
| 460 |
}} |
| 461 |
paginationData={{ |
| 462 |
selectedPerPage, |
| 463 |
updateSelectedPerPage: setSelectedPerPage, |
| 464 |
}} |
| 465 |
statusElementName="idnszoneactive" |
| 466 |
/> |
| 467 |
)} |
| 468 |
</InnerScrollContainer> |
| 469 |
</OuterScrollContainer> |
| 470 |
</FlexItem> |
| 471 |
<FlexItem style={{ flex: "0 0 auto", position: "sticky", bottom: 0 }}> |
| 472 |
<PaginationLayout |
| 473 |
list={dnsForwardZones} |
| 474 |
paginationData={paginationData} |
| 475 |
variant={PaginationVariant.bottom} |
| 476 |
widgetId="pagination-options-menu-bottom" |
| 477 |
/> |
| 478 |
</FlexItem> |
| 479 |
</Flex> |
| 480 |
</PageSection> |
| 481 |
<AddDnsForwardZoneModal |
| 482 |
isOpen={showAddForwardZoneModal} |
| 483 |
onClose={() => setShowAddForwardZoneModal(false)} |
| 484 |
title="Add DNS forward zone" |
| 485 |
onRefresh={refreshData} |
| 486 |
/> |
| 487 |
<DeleteDnsForwardZonesModal |
| 488 |
isOpen={showDeleteForwardZonesModal} |
| 489 |
onClose={() => setShowDeleteForwardZonesModal(false)} |
| 490 |
elementsToDelete={selectedElements} |
| 491 |
clearSelectedElements={() => setSelectedElements([])} |
| 492 |
columnNames={["DNS forward zone name"]} |
| 493 |
keyNames={["idnsname"]} |
| 494 |
onRefresh={refreshData} |
| 495 |
updateIsDeleteButtonDisabled={setIsDeleteButtonDisabled} |
| 496 |
updateIsDeletion={setIsDeletion} |
| 497 |
/> |
| 498 |
<EnableDisableDnsForwardZonesModal |
| 499 |
isOpen={showEnableDisableModal} |
| 500 |
onClose={() => setShowEnableDisableModal(false)} |
| 501 |
elementsList={selectedElements.map((dnszone) => dnszone.idnsname)} |
| 502 |
setElementsList={() => {}} |
| 503 |
operation={operation} |
| 504 |
setShowTableRows={setShowTableRows} |
| 505 |
onRefresh={refreshData} |
| 506 |
/> |
| 507 |
</div> |
| 508 |
); |
| 509 |
}; |
| 510 |
|
| 511 |
export default DnsForwardZones; |