| 1 |
import React, { useEffect, useState } 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 |
// Layouts |
| 15 |
import TitleLayout from "src/components/layouts/TitleLayout"; |
| 16 |
import ToolbarLayout, { |
| 17 |
ToolbarItem, |
| 18 |
} from "src/components/layouts/ToolbarLayout"; |
| 19 |
import SearchInputLayout from "src/components/layouts/SearchInputLayout"; |
| 20 |
import SecondaryButton from "src/components/layouts/SecondaryButton"; |
| 21 |
import HelpTextWithIconLayout from "src/components/layouts/HelpTextWithIconLayout"; |
| 22 |
// Components |
| 23 |
import BulkSelectorPrep from "src/components/BulkSelectorPrep"; |
| 24 |
import PaginationLayout from "src/components/layouts/PaginationLayout"; |
| 25 |
// Tables |
| 26 |
import NetgroupsTable from "src/pages/Netgroups/NetgroupsTable"; |
| 27 |
// Modal |
| 28 |
import AddNetgroup from "src/components/modals/AddNetgroup"; |
| 29 |
import DeleteNetgroups from "src/components/modals/DeleteNetgroups"; |
| 30 |
// Redux |
| 31 |
import { useAppDispatch, useAppSelector } from "src/store/hooks"; |
| 32 |
// Data types |
| 33 |
import { Netgroup } from "src/utils/datatypes/globalDataTypes"; |
| 34 |
// Utils |
| 35 |
import { API_VERSION_BACKUP, isNetgroupSelectable } from "src/utils/utils"; |
| 36 |
// Hooks |
| 37 |
import { addAlert } from "src/store/Global/alerts-slice"; |
| 38 |
import useUpdateRoute from "src/hooks/useUpdateRoute"; |
| 39 |
import useListPageSearchParams from "src/hooks/useListPageSearchParams"; |
| 40 |
// Errors |
| 41 |
import { FetchBaseQueryError } from "@reduxjs/toolkit/query"; |
| 42 |
import { SerializedError } from "@reduxjs/toolkit"; |
| 43 |
import useApiError from "src/hooks/useApiError"; |
| 44 |
import GlobalErrors from "src/components/errors/GlobalErrors"; |
| 45 |
import ModalErrors from "src/components/errors/ModalErrors"; |
| 46 |
// RPC client |
| 47 |
import { GenericPayload, useSearchEntriesMutation } from "../../services/rpc"; |
| 48 |
import { useGettingNetgroupsQuery } from "../../services/rpcNetgroups"; |
| 49 |
|
| 50 |
const Netgroups = () => { |
| 51 |
const dispatch = useAppDispatch(); |
| 52 |
|
| 53 |
// Update current route data to Redux and highlight the current page in the Nav bar |
| 54 |
const { browserTitle } = useUpdateRoute({ pathname: "netgroups" }); |
| 55 |
|
| 56 |
// Set the page title to be shown in the browser tab |
| 57 |
React.useEffect(() => { |
| 58 |
document.title = browserTitle; |
| 59 |
}, [browserTitle]); |
| 60 |
|
| 61 |
// Retrieve API version from environment data |
| 62 |
const apiVersion = useAppSelector( |
| 63 |
(state) => state.global.environment.api_version |
| 64 |
) as string; |
| 65 |
|
| 66 |
// Initialize groups list (Redux) |
| 67 |
const [groupsList, setGroupsList] = useState<Netgroup[]>([]); |
| 68 |
|
| 69 |
// URL parameters: page number, page size, search value |
| 70 |
const { page, setPage, perPage, setPerPage, searchValue, setSearchValue } = |
| 71 |
useListPageSearchParams(); |
| 72 |
|
| 73 |
// Handle API calls errors |
| 74 |
const globalErrors = useApiError([]); |
| 75 |
const modalErrors = useApiError([]); |
| 76 |
|
| 77 |
// Table comps |
| 78 |
const [selectedPerPage, setSelectedPerPage] = useState<number>(0); |
| 79 |
const [totalCount, setGroupsTotalCount] = useState<number>(0); |
| 80 |
const [searchDisabled, setSearchIsDisabled] = useState<boolean>(false); |
| 81 |
|
| 82 |
const updateSelectedPerPage = (selected: number) => { |
| 83 |
setSelectedPerPage(selected); |
| 84 |
}; |
| 85 |
const updatePage = (newPage: number) => { |
| 86 |
setPage(newPage); |
| 87 |
}; |
| 88 |
const updatePerPage = (newSetPerPage: number) => { |
| 89 |
setPerPage(newSetPerPage); |
| 90 |
}; |
| 91 |
|
| 92 |
// 'Delete' button state |
| 93 |
const [isDeleteButtonDisabled, setIsDeleteButtonDisabled] = |
| 94 |
useState<boolean>(true); |
| 95 |
|
| 96 |
const updateIsDeleteButtonDisabled = (value: boolean) => { |
| 97 |
setIsDeleteButtonDisabled(value); |
| 98 |
}; |
| 99 |
|
| 100 |
// If some entries have been deleted, restore the selectedGroups list |
| 101 |
const [isDeletion, setIsDeletion] = useState(false); |
| 102 |
|
| 103 |
const updateIsDeletion = (value: boolean) => { |
| 104 |
setIsDeletion(value); |
| 105 |
}; |
| 106 |
|
| 107 |
const updateShownGroupsList = (newShownGroupsList: Netgroup[]) => { |
| 108 |
setGroupsList(newShownGroupsList); |
| 109 |
}; |
| 110 |
|
| 111 |
// Page indexes |
| 112 |
const firstIdx = (page - 1) * perPage; |
| 113 |
const lastIdx = page * perPage; |
| 114 |
|
| 115 |
// Derived states - what we get from API |
| 116 |
const groupDataResponse = useGettingNetgroupsQuery({ |
| 117 |
searchValue: searchValue, |
| 118 |
sizeLimit: 0, |
| 119 |
apiVersion: apiVersion || API_VERSION_BACKUP, |
| 120 |
startIdx: firstIdx, |
| 121 |
stopIdx: lastIdx, |
| 122 |
} as GenericPayload); |
| 123 |
|
| 124 |
const { |
| 125 |
data: batchResponse, |
| 126 |
isLoading: isBatchLoading, |
| 127 |
error: batchError, |
| 128 |
} = groupDataResponse; |
| 129 |
|
| 130 |
// Handle data when the API call is finished |
| 131 |
useEffect(() => { |
| 132 |
if (groupDataResponse.isFetching) { |
| 133 |
setShowTableRows(false); |
| 134 |
// Reset selected user groups on refresh |
| 135 |
setGroupsTotalCount(0); |
| 136 |
globalErrors.clear(); |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
// API response: Success |
| 141 |
if ( |
| 142 |
groupDataResponse.isSuccess && |
| 143 |
groupDataResponse.data && |
| 144 |
batchResponse !== undefined |
| 145 |
) { |
| 146 |
const groupsListResult = batchResponse.result.results; |
| 147 |
const totalCount = batchResponse.result.totalCount; |
| 148 |
const groupsListSize = batchResponse.result.count; |
| 149 |
const groupsList: Netgroup[] = []; |
| 150 |
|
| 151 |
for (let i = 0; i < groupsListSize; i++) { |
| 152 |
groupsList.push(groupsListResult[i].result); |
| 153 |
} |
| 154 |
|
| 155 |
setGroupsList(groupsList); |
| 156 |
setGroupsTotalCount(totalCount); |
| 157 |
// Show table elements |
| 158 |
setShowTableRows(true); |
| 159 |
} |
| 160 |
|
| 161 |
// API response: Error |
| 162 |
if ( |
| 163 |
!groupDataResponse.isLoading && |
| 164 |
groupDataResponse.isError && |
| 165 |
groupDataResponse.error !== undefined |
| 166 |
) { |
| 167 |
// This normally happens when the user is not authorized to view the data |
| 168 |
// So instead of adding an error, refresh page |
| 169 |
window.location.reload(); |
| 170 |
} |
| 171 |
}, [groupDataResponse]); |
| 172 |
|
| 173 |
// Always refetch data when the component is loaded. |
| 174 |
// This ensures the data is always up-to-date. |
| 175 |
useEffect(() => { |
| 176 |
groupDataResponse.refetch(); |
| 177 |
}, [page, perPage]); |
| 178 |
|
| 179 |
// Refresh button handling |
| 180 |
const refreshGroupsData = () => { |
| 181 |
// Hide table |
| 182 |
setShowTableRows(false); |
| 183 |
|
| 184 |
// Reset selected netgroups on refresh |
| 185 |
setGroupsTotalCount(0); |
| 186 |
clearSelectedGroups(); |
| 187 |
setPage(1); |
| 188 |
groupDataResponse.refetch(); |
| 189 |
}; |
| 190 |
|
| 191 |
const updateSearchValue = (value: string) => { |
| 192 |
setSearchValue(value); |
| 193 |
}; |
| 194 |
|
| 195 |
const [selectedGroups, setSelectedGroupsList] = useState<Netgroup[]>([]); |
| 196 |
const clearSelectedGroups = () => { |
| 197 |
const emptyList: Netgroup[] = []; |
| 198 |
setSelectedGroupsList(emptyList); |
| 199 |
}; |
| 200 |
|
| 201 |
const [retrieveGroup] = useSearchEntriesMutation({}); |
| 202 |
|
| 203 |
// Issue a search using a specific search value |
| 204 |
const submitSearchValue = () => { |
| 205 |
setShowTableRows(false); |
| 206 |
setGroupsTotalCount(0); |
| 207 |
setSearchIsDisabled(true); |
| 208 |
retrieveGroup({ |
| 209 |
searchValue: searchValue, |
| 210 |
sizeLimit: 0, |
| 211 |
apiVersion: apiVersion || API_VERSION_BACKUP, |
| 212 |
startIdx: firstIdx, |
| 213 |
stopIdx: lastIdx, |
| 214 |
entryType: "netgroup", |
| 215 |
} as GenericPayload).then((result) => { |
| 216 |
// Manage new response here |
| 217 |
if ("data" in result) { |
| 218 |
const searchError = result.data?.error as |
| 219 |
| FetchBaseQueryError |
| 220 |
| SerializedError; |
| 221 |
|
| 222 |
if (searchError) { |
| 223 |
// Error |
| 224 |
let error: string | undefined = ""; |
| 225 |
if ("error" in searchError) { |
| 226 |
error = searchError.error; |
| 227 |
} else if ("message" in searchError) { |
| 228 |
error = searchError.message; |
| 229 |
} |
| 230 |
dispatch( |
| 231 |
addAlert({ |
| 232 |
name: "submit-search-value-error", |
| 233 |
title: error || "Error when searching for netgroups", |
| 234 |
variant: "danger", |
| 235 |
}) |
| 236 |
); |
| 237 |
} else { |
| 238 |
// Success |
| 239 |
const groupsListResult = result.data?.result.results || []; |
| 240 |
const groupsListSize = result.data?.result.count || 0; |
| 241 |
const totalCount = result.data?.result.totalCount || 0; |
| 242 |
const groupsList: Netgroup[] = []; |
| 243 |
|
| 244 |
for (let i = 0; i < groupsListSize; i++) { |
| 245 |
groupsList.push(groupsListResult[i].result); |
| 246 |
} |
| 247 |
|
| 248 |
setPage(1); |
| 249 |
setGroupsList(groupsList); |
| 250 |
setGroupsTotalCount(totalCount); |
| 251 |
// Show table elements |
| 252 |
setShowTableRows(true); |
| 253 |
} |
| 254 |
setSearchIsDisabled(false); |
| 255 |
} |
| 256 |
}); |
| 257 |
}; |
| 258 |
|
| 259 |
// Show table rows |
| 260 |
const [showTableRows, setShowTableRows] = useState(!isBatchLoading); |
| 261 |
|
| 262 |
const updateSelectedGroups = (groups: Netgroup[], isSelected: boolean) => { |
| 263 |
let newSelectedGroups: Netgroup[] = []; |
| 264 |
if (isSelected) { |
| 265 |
newSelectedGroups = JSON.parse(JSON.stringify(selectedGroups)); |
| 266 |
for (let i = 0; i < groups.length; i++) { |
| 267 |
if ( |
| 268 |
selectedGroups.find( |
| 269 |
(selectedGroup) => selectedGroup.cn[0] === groups[i].cn[0] |
| 270 |
) |
| 271 |
) { |
| 272 |
// Already in the list |
| 273 |
continue; |
| 274 |
} |
| 275 |
// Add group to list |
| 276 |
newSelectedGroups.push(groups[i]); |
| 277 |
} |
| 278 |
} else { |
| 279 |
// Remove group |
| 280 |
for (let i = 0; i < selectedGroups.length; i++) { |
| 281 |
let found = false; |
| 282 |
for (let ii = 0; ii < groups.length; ii++) { |
| 283 |
if (selectedGroups[i].cn[0] === groups[ii].cn[0]) { |
| 284 |
found = true; |
| 285 |
break; |
| 286 |
} |
| 287 |
} |
| 288 |
if (!found) { |
| 289 |
// Keep this valid selected entry |
| 290 |
newSelectedGroups.push(selectedGroups[i]); |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
setSelectedGroupsList(newSelectedGroups); |
| 295 |
setIsDeleteButtonDisabled(newSelectedGroups.length === 0); |
| 296 |
}; |
| 297 |
|
| 298 |
// - Helper method to set the selected groups from the table |
| 299 |
const setGroupSelected = (group: Netgroup, isSelecting = true) => { |
| 300 |
if (isNetgroupSelectable(group)) { |
| 301 |
updateSelectedGroups([group], isSelecting); |
| 302 |
} |
| 303 |
}; |
| 304 |
|
| 305 |
// Show table rows only when data is fully retrieved |
| 306 |
useEffect(() => { |
| 307 |
if (showTableRows !== !isBatchLoading) { |
| 308 |
setShowTableRows(!isBatchLoading); |
| 309 |
} |
| 310 |
}, [isBatchLoading]); |
| 311 |
|
| 312 |
// Modals functionality |
| 313 |
const [showAddModal, setShowAddModal] = useState(false); |
| 314 |
const [showDeleteModal, setShowDeleteModal] = useState(false); |
| 315 |
|
| 316 |
const onAddClickHandler = () => { |
| 317 |
setShowAddModal(true); |
| 318 |
}; |
| 319 |
const onCloseAddModal = () => { |
| 320 |
setShowAddModal(false); |
| 321 |
}; |
| 322 |
const onAddModalToggle = () => { |
| 323 |
setShowAddModal(!showAddModal); |
| 324 |
}; |
| 325 |
|
| 326 |
const onDeleteHandler = () => { |
| 327 |
setShowDeleteModal(true); |
| 328 |
}; |
| 329 |
const onDeleteModalToggle = () => { |
| 330 |
setShowDeleteModal(!showDeleteModal); |
| 331 |
}; |
| 332 |
|
| 333 |
// Table-related shared functionality |
| 334 |
// - Selectable checkboxes on table |
| 335 |
const selectableGroupsTable = groupsList.filter(isNetgroupSelectable); // elements per Table |
| 336 |
|
| 337 |
// Data wrappers |
| 338 |
// - 'PaginationLayout' |
| 339 |
const paginationData = { |
| 340 |
page, |
| 341 |
perPage, |
| 342 |
updatePage, |
| 343 |
updatePerPage, |
| 344 |
updateSelectedPerPage, |
| 345 |
updateShownElementsList: updateShownGroupsList, |
| 346 |
totalCount, |
| 347 |
}; |
| 348 |
|
| 349 |
// - 'BulkSelectorPrep' |
| 350 |
const groupsBulkSelectorData = { |
| 351 |
selected: selectedGroups, |
| 352 |
updateSelected: updateSelectedGroups, |
| 353 |
selectableTable: selectableGroupsTable, |
| 354 |
nameAttr: "cn", |
| 355 |
}; |
| 356 |
|
| 357 |
const buttonsData = { |
| 358 |
updateIsDeleteButtonDisabled, |
| 359 |
}; |
| 360 |
|
| 361 |
const selectedPerPageData = { |
| 362 |
selectedPerPage, |
| 363 |
updateSelectedPerPage, |
| 364 |
}; |
| 365 |
|
| 366 |
// - 'DeleteGroups' |
| 367 |
const deleteGroupsButtonsData = { |
| 368 |
updateIsDeleteButtonDisabled, |
| 369 |
updateIsDeletion, |
| 370 |
}; |
| 371 |
|
| 372 |
const selectedGroupsData = { |
| 373 |
selectedGroups, |
| 374 |
clearSelectedGroups, |
| 375 |
}; |
| 376 |
|
| 377 |
// - 'NetroupsTable' |
| 378 |
const groupsTableData = { |
| 379 |
isNetgroupSelectable, |
| 380 |
selectedGroups, |
| 381 |
selectableGroupsTable, |
| 382 |
setGroupSelected, |
| 383 |
clearSelectedGroups, |
| 384 |
}; |
| 385 |
|
| 386 |
const groupsTableButtonsData = { |
| 387 |
updateIsDeleteButtonDisabled, |
| 388 |
isDeletion, |
| 389 |
updateIsDeletion, |
| 390 |
}; |
| 391 |
|
| 392 |
// - 'SearchInputLayout' |
| 393 |
const searchValueData = { |
| 394 |
searchValue, |
| 395 |
updateSearchValue, |
| 396 |
submitSearchValue, |
| 397 |
}; |
| 398 |
|
| 399 |
// List of toolbar items |
| 400 |
const toolbarItems: ToolbarItem[] = [ |
| 401 |
{ |
| 402 |
key: 0, |
| 403 |
element: ( |
| 404 |
<BulkSelectorPrep |
| 405 |
list={groupsList} |
| 406 |
shownElementsList={groupsList} |
| 407 |
elementData={groupsBulkSelectorData} |
| 408 |
buttonsData={buttonsData} |
| 409 |
selectedPerPageData={selectedPerPageData} |
| 410 |
/> |
| 411 |
), |
| 412 |
}, |
| 413 |
{ |
| 414 |
key: 1, |
| 415 |
element: ( |
| 416 |
<SearchInputLayout |
| 417 |
dataCy="search" |
| 418 |
name="search" |
| 419 |
ariaLabel="Search netgroups" |
| 420 |
placeholder="Search" |
| 421 |
searchValueData={searchValueData} |
| 422 |
isDisabled={searchDisabled} |
| 423 |
/> |
| 424 |
), |
| 425 |
toolbarItemVariant: ToolbarItemVariant.label, |
| 426 |
toolbarItemGap: { default: "gapMd" }, |
| 427 |
}, |
| 428 |
{ |
| 429 |
key: 2, |
| 430 |
toolbarItemVariant: ToolbarItemVariant.separator, |
| 431 |
}, |
| 432 |
{ |
| 433 |
key: 3, |
| 434 |
element: ( |
| 435 |
<SecondaryButton |
| 436 |
onClickHandler={refreshGroupsData} |
| 437 |
isDisabled={!showTableRows} |
| 438 |
dataCy="netgroups-button-refresh" |
| 439 |
> |
| 440 |
Refresh |
| 441 |
</SecondaryButton> |
| 442 |
), |
| 443 |
}, |
| 444 |
{ |
| 445 |
key: 4, |
| 446 |
element: ( |
| 447 |
<SecondaryButton |
| 448 |
isDisabled={isDeleteButtonDisabled || !showTableRows} |
| 449 |
onClickHandler={onDeleteHandler} |
| 450 |
dataCy="netgroups-button-delete" |
| 451 |
> |
| 452 |
Delete |
| 453 |
</SecondaryButton> |
| 454 |
), |
| 455 |
}, |
| 456 |
{ |
| 457 |
key: 5, |
| 458 |
element: ( |
| 459 |
<SecondaryButton |
| 460 |
onClickHandler={onAddClickHandler} |
| 461 |
isDisabled={!showTableRows} |
| 462 |
dataCy="netgroups-button-add" |
| 463 |
> |
| 464 |
Add |
| 465 |
</SecondaryButton> |
| 466 |
), |
| 467 |
}, |
| 468 |
{ |
| 469 |
key: 6, |
| 470 |
toolbarItemVariant: ToolbarItemVariant.separator, |
| 471 |
}, |
| 472 |
{ |
| 473 |
key: 7, |
| 474 |
element: <HelpTextWithIconLayout textContent="Help" />, |
| 475 |
}, |
| 476 |
{ |
| 477 |
key: 8, |
| 478 |
element: ( |
| 479 |
<PaginationLayout |
| 480 |
list={groupsList} |
| 481 |
paginationData={paginationData} |
| 482 |
widgetId="pagination-options-menu-top" |
| 483 |
isCompact={true} |
| 484 |
/> |
| 485 |
), |
| 486 |
toolbarItemAlignment: { default: "alignEnd" }, |
| 487 |
}, |
| 488 |
]; |
| 489 |
|
| 490 |
return ( |
| 491 |
<div> |
| 492 |
<PageSection hasBodyWrapper={false}> |
| 493 |
<TitleLayout id="Netgroups title" headingLevel="h1" text="Netgroups" /> |
| 494 |
</PageSection> |
| 495 |
<PageSection hasBodyWrapper={false} isFilled={false}> |
| 496 |
<Flex direction={{ default: "column" }}> |
| 497 |
<FlexItem> |
| 498 |
<ToolbarLayout toolbarItems={toolbarItems} /> |
| 499 |
</FlexItem> |
| 500 |
<FlexItem style={{ flex: "0 0 auto" }}> |
| 501 |
<OuterScrollContainer> |
| 502 |
<InnerScrollContainer |
| 503 |
style={{ height: "60vh", overflow: "auto" }} |
| 504 |
> |
| 505 |
{batchError !== undefined && batchError ? ( |
| 506 |
<GlobalErrors errors={globalErrors.getAll()} /> |
| 507 |
) : ( |
| 508 |
<NetgroupsTable |
| 509 |
elementsList={groupsList} |
| 510 |
shownElementsList={groupsList} |
| 511 |
showTableRows={showTableRows} |
| 512 |
groupsData={groupsTableData} |
| 513 |
buttonsData={groupsTableButtonsData} |
| 514 |
paginationData={selectedPerPageData} |
| 515 |
searchValue={searchValue} |
| 516 |
/> |
| 517 |
)} |
| 518 |
</InnerScrollContainer> |
| 519 |
</OuterScrollContainer> |
| 520 |
</FlexItem> |
| 521 |
<FlexItem style={{ flex: "0 0 auto", position: "sticky", bottom: 0 }}> |
| 522 |
<PaginationLayout |
| 523 |
list={groupsList} |
| 524 |
paginationData={paginationData} |
| 525 |
variant={PaginationVariant.bottom} |
| 526 |
widgetId="pagination-options-menu-bottom" |
| 527 |
/> |
| 528 |
</FlexItem> |
| 529 |
</Flex> |
| 530 |
</PageSection> |
| 531 |
<ModalErrors |
| 532 |
errors={modalErrors.getAll()} |
| 533 |
dataCy="netgroups-modal-error" |
| 534 |
/> |
| 535 |
<AddNetgroup |
| 536 |
show={showAddModal} |
| 537 |
handleModalToggle={onAddModalToggle} |
| 538 |
onOpenAddModal={onAddClickHandler} |
| 539 |
onCloseAddModal={onCloseAddModal} |
| 540 |
onRefresh={refreshGroupsData} |
| 541 |
/> |
| 542 |
<DeleteNetgroups |
| 543 |
show={showDeleteModal} |
| 544 |
handleModalToggle={onDeleteModalToggle} |
| 545 |
selectedGroupsData={selectedGroupsData} |
| 546 |
buttonsData={deleteGroupsButtonsData} |
| 547 |
onRefresh={refreshGroupsData} |
| 548 |
/> |
| 549 |
</div> |
| 550 |
); |
| 551 |
}; |
| 552 |
|
| 553 |
export default Netgroups; |