| 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 |
// Hooks |
| 15 |
import { addAlert } from "src/store/Global/alerts-slice"; |
| 16 |
import useUpdateRoute from "src/hooks/useUpdateRoute"; |
| 17 |
import useApiError from "src/hooks/useApiError"; |
| 18 |
// Redux |
| 19 |
import { useAppSelector, useAppDispatch } from "src/store/hooks"; |
| 20 |
// RPC |
| 21 |
import { |
| 22 |
useGetIdRangeEntriesQuery, |
| 23 |
useSearchIdRangesEntriesMutation, |
| 24 |
} from "src/services/rpcIdRanges"; |
| 25 |
import { IdRange } from "src/utils/datatypes/globalDataTypes"; |
| 26 |
// React router |
| 27 |
import { useNavigate } from "react-router"; |
| 28 |
// Components |
| 29 |
import { FetchBaseQueryError } from "@reduxjs/toolkit/query"; |
| 30 |
import { SerializedError } from "@reduxjs/toolkit"; |
| 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 useListPageSearchParams from "src/hooks/useListPageSearchParams"; |
| 42 |
import BulkSelectorPrep from "src/components/BulkSelectorPrep"; |
| 43 |
import { isIdRangeSelectable } from "src/utils/utils"; |
| 44 |
import AddIdRangeModal from "src/components/modals/IdRanges/AddIdRangeModal"; |
| 45 |
import DeleteModal from "src/components/modals/IdRanges/DeleteModal"; |
| 46 |
|
| 47 |
const IdRanges = () => { |
| 48 |
const dispatch = useAppDispatch(); |
| 49 |
const navigate = useNavigate(); |
| 50 |
|
| 51 |
// Update current route data to Redux and highlight the current page in the Nav bar |
| 52 |
const { browserTitle } = useUpdateRoute({ pathname: "id-ranges" }); |
| 53 |
|
| 54 |
// Set the page title to be shown in the browser tab |
| 55 |
React.useEffect(() => { |
| 56 |
document.title = browserTitle; |
| 57 |
}, [browserTitle]); |
| 58 |
|
| 59 |
// Retrieve API version from environment data |
| 60 |
const apiVersion = useAppSelector( |
| 61 |
(state) => state.global.environment.api_version |
| 62 |
) as string; |
| 63 |
|
| 64 |
// URL parameters: page number, page size, search value |
| 65 |
const { page, setPage, perPage, setPerPage, searchValue, setSearchValue } = |
| 66 |
useListPageSearchParams(); |
| 67 |
|
| 68 |
const [selectedPerPage, setSelectedPerPage] = React.useState<number>(0); |
| 69 |
const [showAddModal, setShowAddModal] = React.useState<boolean>(false); |
| 70 |
|
| 71 |
// Handle API calls errors |
| 72 |
const globalErrors = useApiError([]); |
| 73 |
|
| 74 |
// Page indexes |
| 75 |
const firstIdx = (page - 1) * perPage; |
| 76 |
const lastIdx = page * perPage; |
| 77 |
|
| 78 |
const selectedPerPageData = { |
| 79 |
selectedPerPage, |
| 80 |
updateSelectedPerPage: setSelectedPerPage, |
| 81 |
}; |
| 82 |
|
| 83 |
// Selection state for checkboxes |
| 84 |
const [selectedElements, setSelectedElements] = React.useState<IdRange[]>([]); |
| 85 |
const [isDeleteButtonDisabled, setIsDeleteButtonDisabled] = |
| 86 |
React.useState<boolean>(true); |
| 87 |
const [isDeletion, setIsDeletion] = React.useState(false); |
| 88 |
const [showDeleteModal, setShowDeleteModal] = React.useState(false); |
| 89 |
|
| 90 |
const updateSelectedIdRanges = (idRange: IdRange[], isSelected: boolean) => { |
| 91 |
let newSelectedIdRanges: IdRange[] = [...selectedElements]; |
| 92 |
|
| 93 |
if (isSelected) { |
| 94 |
for (let i = 0; i < idRange.length; i++) { |
| 95 |
const isAlreadySelected = newSelectedIdRanges.some( |
| 96 |
(selectedIdRange) => selectedIdRange.cn === idRange[i].cn |
| 97 |
); |
| 98 |
if (!isAlreadySelected) { |
| 99 |
newSelectedIdRanges.push(idRange[i]); |
| 100 |
} |
| 101 |
} |
| 102 |
} else { |
| 103 |
const idsToRemove = new Set(idRange.map((r) => r.cn)); |
| 104 |
newSelectedIdRanges = newSelectedIdRanges.filter( |
| 105 |
(selectedIdRange) => !idsToRemove.has(selectedIdRange.cn) |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
setSelectedElements(newSelectedIdRanges); |
| 110 |
setIsDeleteButtonDisabled(newSelectedIdRanges.length === 0); |
| 111 |
}; |
| 112 |
|
| 113 |
// API calls (batch detailed list) |
| 114 |
const idRangesDataResponse = useGetIdRangeEntriesQuery({ |
| 115 |
searchValue, |
| 116 |
apiVersion, |
| 117 |
sizelimit: 100, |
| 118 |
startIdx: firstIdx, |
| 119 |
stopIdx: lastIdx, |
| 120 |
}); |
| 121 |
|
| 122 |
const { |
| 123 |
data: batchResponse, |
| 124 |
isLoading, |
| 125 |
isFetching, |
| 126 |
isSuccess, |
| 127 |
isError, |
| 128 |
error: batchError, |
| 129 |
} = idRangesDataResponse; |
| 130 |
|
| 131 |
// Derived data from query |
| 132 |
const queryDerived = React.useMemo(() => { |
| 133 |
if (!isSuccess || !batchResponse) { |
| 134 |
return { list: [] as IdRange[], total: 0, ready: false }; |
| 135 |
} |
| 136 |
const listResult = batchResponse.result.results; |
| 137 |
const total = batchResponse.result.totalCount; |
| 138 |
const listSize = batchResponse.result.count; |
| 139 |
const elementsList: IdRange[] = []; |
| 140 |
for (let i = 0; i < listSize; i++) { |
| 141 |
elementsList.push(listResult[i].result as IdRange); |
| 142 |
} |
| 143 |
return { list: elementsList, total, ready: true }; |
| 144 |
}, [isSuccess, batchResponse]); |
| 145 |
|
| 146 |
// Track search override results (when using the Search input) |
| 147 |
const [searchOverride, setSearchOverride] = React.useState<{ |
| 148 |
list: IdRange[]; |
| 149 |
total: number; |
| 150 |
} | null>(null); |
| 151 |
|
| 152 |
// Clear alerts while fetching |
| 153 |
React.useEffect(() => { |
| 154 |
if (isFetching) { |
| 155 |
globalErrors.clear(); |
| 156 |
} |
| 157 |
}, [isFetching]); |
| 158 |
|
| 159 |
// Redirect on auth errors |
| 160 |
React.useEffect(() => { |
| 161 |
if (!isLoading && isError && batchError !== undefined) { |
| 162 |
navigate("/login"); |
| 163 |
window.location.reload(); |
| 164 |
} |
| 165 |
}, [isLoading, isError, batchError]); |
| 166 |
|
| 167 |
// Refresh button handling |
| 168 |
const refreshData = () => { |
| 169 |
setSearchOverride(null); |
| 170 |
idRangesDataResponse.refetch(); |
| 171 |
}; |
| 172 |
|
| 173 |
// Show table rows |
| 174 |
const showTableRows = |
| 175 |
!isFetching && (searchOverride !== null || queryDerived.ready); |
| 176 |
|
| 177 |
// Search API call (batch) |
| 178 |
const [searchEntry] = useSearchIdRangesEntriesMutation(); |
| 179 |
|
| 180 |
const [isSearchDisabled, setIsSearchDisabled] = |
| 181 |
React.useState<boolean>(false); |
| 182 |
|
| 183 |
const submitSearchValue = () => { |
| 184 |
setSearchOverride(null); |
| 185 |
searchEntry({ |
| 186 |
searchValue: searchValue, |
| 187 |
apiVersion, |
| 188 |
sizelimit: 100, |
| 189 |
startIdx: 0, |
| 190 |
stopIdx: 200, |
| 191 |
}).then((result) => { |
| 192 |
if ("data" in result && result.data !== undefined) { |
| 193 |
const searchError = result.data?.error as |
| 194 |
| FetchBaseQueryError |
| 195 |
| SerializedError; |
| 196 |
|
| 197 |
if (searchError) { |
| 198 |
// Error |
| 199 |
let errMsg: string | undefined = ""; |
| 200 |
if ("error" in searchError) { |
| 201 |
errMsg = searchError.error; |
| 202 |
} |
| 203 |
dispatch( |
| 204 |
addAlert({ |
| 205 |
name: "submit-search-value-error", |
| 206 |
title: errMsg || "Error when searching for elements", |
| 207 |
variant: "danger", |
| 208 |
}) |
| 209 |
); |
| 210 |
} else { |
| 211 |
// Success |
| 212 |
const listResult = result.data?.result.results || []; |
| 213 |
const listSize = result.data?.result.count || 0; |
| 214 |
const total = result.data?.result.totalCount || 0; |
| 215 |
const elementsList: IdRange[] = []; |
| 216 |
|
| 217 |
for (let i = 0; i < listSize; i++) { |
| 218 |
elementsList.push(listResult[i].result as IdRange); |
| 219 |
} |
| 220 |
|
| 221 |
setSearchOverride({ list: elementsList, total }); |
| 222 |
} |
| 223 |
setIsSearchDisabled(false); |
| 224 |
} |
| 225 |
}); |
| 226 |
}; |
| 227 |
|
| 228 |
// Compute shown list and total |
| 229 |
const shownElementsList = searchOverride |
| 230 |
? searchOverride.list |
| 231 |
: queryDerived.list; |
| 232 |
const totalCount = searchOverride ? searchOverride.total : queryDerived.total; |
| 233 |
|
| 234 |
// Selection helpers |
| 235 |
const selectableIdRangesTable = shownElementsList.filter(isIdRangeSelectable); |
| 236 |
const setIdRangesSelected = (idRange: IdRange, isSelecting = true) => { |
| 237 |
if (isIdRangeSelectable(idRange)) { |
| 238 |
updateSelectedIdRanges([idRange], isSelecting); |
| 239 |
} |
| 240 |
}; |
| 241 |
|
| 242 |
const bulkSelectorData = { |
| 243 |
selected: selectedElements, |
| 244 |
updateSelected: updateSelectedIdRanges, |
| 245 |
selectableTable: selectableIdRangesTable, |
| 246 |
nameAttr: "cn", |
| 247 |
}; |
| 248 |
|
| 249 |
// Data wrappers |
| 250 |
const paginationData = { |
| 251 |
page, |
| 252 |
perPage, |
| 253 |
updatePage: setPage, |
| 254 |
updatePerPage: setPerPage, |
| 255 |
totalCount, |
| 256 |
}; |
| 257 |
|
| 258 |
// SearchInputLayout |
| 259 |
const searchValueData = { |
| 260 |
searchValue, |
| 261 |
updateSearchValue: (v: string) => { |
| 262 |
setSearchOverride(null); |
| 263 |
setSearchValue(v); |
| 264 |
}, |
| 265 |
submitSearchValue, |
| 266 |
}; |
| 267 |
|
| 268 |
// List of Toolbar items |
| 269 |
const toolbarItems: ToolbarItem[] = [ |
| 270 |
{ |
| 271 |
key: 0, |
| 272 |
element: ( |
| 273 |
<BulkSelectorPrep |
| 274 |
list={shownElementsList} |
| 275 |
shownElementsList={shownElementsList} |
| 276 |
elementData={bulkSelectorData} |
| 277 |
buttonsData={{ |
| 278 |
updateIsDeleteButtonDisabled: (disabled: boolean) => { |
| 279 |
void disabled; |
| 280 |
}, |
| 281 |
}} |
| 282 |
selectedPerPageData={selectedPerPageData} |
| 283 |
/> |
| 284 |
), |
| 285 |
}, |
| 286 |
{ |
| 287 |
key: 1, |
| 288 |
element: ( |
| 289 |
<SearchInputLayout |
| 290 |
dataCy="search" |
| 291 |
name="search" |
| 292 |
ariaLabel="Search ID ranges" |
| 293 |
placeholder="Search" |
| 294 |
searchValueData={searchValueData} |
| 295 |
isDisabled={isSearchDisabled} |
| 296 |
/> |
| 297 |
), |
| 298 |
toolbarItemVariant: ToolbarItemVariant.label, |
| 299 |
toolbarItemGap: { default: "gapMd" }, |
| 300 |
}, |
| 301 |
{ |
| 302 |
key: 2, |
| 303 |
toolbarItemVariant: ToolbarItemVariant.separator, |
| 304 |
}, |
| 305 |
{ |
| 306 |
key: 3, |
| 307 |
element: ( |
| 308 |
<SecondaryButton |
| 309 |
dataCy="id-ranges-button-refresh" |
| 310 |
onClickHandler={refreshData} |
| 311 |
isDisabled={!showTableRows} |
| 312 |
> |
| 313 |
Refresh |
| 314 |
</SecondaryButton> |
| 315 |
), |
| 316 |
}, |
| 317 |
{ |
| 318 |
key: 4, |
| 319 |
element: ( |
| 320 |
<SecondaryButton |
| 321 |
isDisabled={isDeleteButtonDisabled || !showTableRows} |
| 322 |
dataCy="id-ranges-button-delete" |
| 323 |
onClickHandler={() => setShowDeleteModal(true)} |
| 324 |
> |
| 325 |
Delete |
| 326 |
</SecondaryButton> |
| 327 |
), |
| 328 |
}, |
| 329 |
{ |
| 330 |
key: 5, |
| 331 |
element: ( |
| 332 |
<SecondaryButton |
| 333 |
isDisabled={!showTableRows} |
| 334 |
dataCy="id-ranges-button-add" |
| 335 |
onClickHandler={() => setShowAddModal(true)} |
| 336 |
> |
| 337 |
Add |
| 338 |
</SecondaryButton> |
| 339 |
), |
| 340 |
}, |
| 341 |
{ |
| 342 |
key: 6, |
| 343 |
toolbarItemVariant: ToolbarItemVariant.separator, |
| 344 |
}, |
| 345 |
{ |
| 346 |
key: 7, |
| 347 |
element: <HelpTextWithIconLayout textContent="Help" />, |
| 348 |
}, |
| 349 |
{ |
| 350 |
key: 8, |
| 351 |
element: ( |
| 352 |
<PaginationLayout |
| 353 |
list={shownElementsList} |
| 354 |
paginationData={paginationData} |
| 355 |
widgetId="pagination-options-menu-top" |
| 356 |
isCompact={true} |
| 357 |
/> |
| 358 |
), |
| 359 |
toolbarItemAlignment: { default: "alignEnd" }, |
| 360 |
}, |
| 361 |
]; |
| 362 |
|
| 363 |
// Render component |
| 364 |
return ( |
| 365 |
<div> |
| 366 |
<PageSection hasBodyWrapper={false}> |
| 367 |
<TitleLayout id="ID ranges page" headingLevel="h1" text="ID ranges" /> |
| 368 |
</PageSection> |
| 369 |
<PageSection hasBodyWrapper={false} isFilled={false}> |
| 370 |
<Flex direction={{ default: "column" }}> |
| 371 |
<FlexItem> |
| 372 |
<ToolbarLayout toolbarItems={toolbarItems} /> |
| 373 |
</FlexItem> |
| 374 |
<FlexItem style={{ flex: "1 1 auto", overflow: "hidden" }}> |
| 375 |
<OuterScrollContainer |
| 376 |
style={{ height: "100%", overflow: "hidden" }} |
| 377 |
> |
| 378 |
<InnerScrollContainer |
| 379 |
style={{ height: "100%", overflow: "auto" }} |
| 380 |
> |
| 381 |
{batchError !== undefined && batchError ? ( |
| 382 |
<GlobalErrors errors={globalErrors.getAll()} /> |
| 383 |
) : ( |
| 384 |
<MainTable |
| 385 |
tableTitle="ID ranges table" |
| 386 |
shownElementsList={shownElementsList} |
| 387 |
pk="cn" |
| 388 |
keyNames={[ |
| 389 |
"cn", |
| 390 |
"ipabaseid", |
| 391 |
"ipaidrangesize", |
| 392 |
"iparangetype", |
| 393 |
]} |
| 394 |
columnNames={[ |
| 395 |
"Range name", |
| 396 |
"First Posix ID of the range", |
| 397 |
"Number of IDs in the range", |
| 398 |
"Range type", |
| 399 |
]} |
| 400 |
hasCheckboxes={true} |
| 401 |
pathname="id-ranges" |
| 402 |
showTableRows={showTableRows} |
| 403 |
showLink={false} |
| 404 |
elementsData={{ |
| 405 |
isElementSelectable: isIdRangeSelectable, |
| 406 |
selectedElements, |
| 407 |
selectableElementsTable: |
| 408 |
shownElementsList.filter(isIdRangeSelectable), |
| 409 |
setElementsSelected: setIdRangesSelected, |
| 410 |
clearSelectedElements: () => setSelectedElements([]), |
| 411 |
}} |
| 412 |
buttonsData={{ |
| 413 |
updateIsDeleteButtonDisabled: setIsDeleteButtonDisabled, |
| 414 |
isDeletion, |
| 415 |
updateIsDeletion: setIsDeletion, |
| 416 |
}} |
| 417 |
paginationData={{ |
| 418 |
selectedPerPage, |
| 419 |
updateSelectedPerPage: setSelectedPerPage, |
| 420 |
}} |
| 421 |
/> |
| 422 |
)} |
| 423 |
</InnerScrollContainer> |
| 424 |
</OuterScrollContainer> |
| 425 |
</FlexItem> |
| 426 |
<FlexItem style={{ flex: "0 0 auto", position: "sticky", bottom: 0 }}> |
| 427 |
<PaginationLayout |
| 428 |
list={shownElementsList} |
| 429 |
paginationData={paginationData} |
| 430 |
variant={PaginationVariant.bottom} |
| 431 |
widgetId="pagination-options-menu-bottom" |
| 432 |
/> |
| 433 |
</FlexItem> |
| 434 |
</Flex> |
| 435 |
</PageSection> |
| 436 |
<AddIdRangeModal |
| 437 |
isOpen={showAddModal} |
| 438 |
onClose={() => setShowAddModal(false)} |
| 439 |
title="Add ID range" |
| 440 |
onRefresh={refreshData} |
| 441 |
/> |
| 442 |
<DeleteModal |
| 443 |
show={showDeleteModal} |
| 444 |
onClose={() => setShowDeleteModal(false)} |
| 445 |
selectedData={{ |
| 446 |
selectedElements, |
| 447 |
clearSelectedElements: () => setSelectedElements([]), |
| 448 |
}} |
| 449 |
buttonsData={{ |
| 450 |
updateIsDeleteButtonDisabled: setIsDeleteButtonDisabled, |
| 451 |
updateIsDeletion: setIsDeletion, |
| 452 |
}} |
| 453 |
columnNames={[ |
| 454 |
"Range name", |
| 455 |
"First Posix ID of the range", |
| 456 |
"Number of IDs in the range", |
| 457 |
"Range type", |
| 458 |
]} |
| 459 |
keyNames={["cn", "ipabaseid", "ipaidrangesize", "iparangetype"]} |
| 460 |
onRefresh={refreshData} |
| 461 |
/> |
| 462 |
</div> |
| 463 |
); |
| 464 |
}; |
| 465 |
|
| 466 |
export default IdRanges; |