| 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 { SubId } 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 |
// Redux |
| 21 |
import { useAppSelector, useAppDispatch } from "src/store/hooks"; |
| 22 |
// Components |
| 23 |
import TitleLayout from "src/components/layouts/TitleLayout"; |
| 24 |
import ToolbarLayout, { |
| 25 |
ToolbarItem, |
| 26 |
} from "src/components/layouts/ToolbarLayout"; |
| 27 |
import PaginationLayout from "src/components/layouts/PaginationLayout"; |
| 28 |
import SearchInputLayout from "src/components/layouts/SearchInputLayout"; |
| 29 |
import SecondaryButton from "src/components/layouts/SecondaryButton"; |
| 30 |
import HelpTextWithIconLayout from "src/components/layouts/HelpTextWithIconLayout"; |
| 31 |
import MainTable from "src/components/tables/MainTable"; |
| 32 |
// Errors |
| 33 |
import { FetchBaseQueryError } from "@reduxjs/toolkit/query"; |
| 34 |
import { SerializedError } from "@reduxjs/toolkit"; |
| 35 |
import useApiError from "src/hooks/useApiError"; |
| 36 |
import GlobalErrors from "src/components/errors/GlobalErrors"; |
| 37 |
// RPC |
| 38 |
import { |
| 39 |
SubIdDataPayload, |
| 40 |
useGetSubIdEntriesQuery, |
| 41 |
useSearchSubIdEntriesMutation, |
| 42 |
} from "src/services/rpcSubIds"; |
| 43 |
// Modals |
| 44 |
import AddModal from "src/components/modals/SubIdsModals/AddModal"; |
| 45 |
|
| 46 |
const SubordinateIDs = () => { |
| 47 |
const dispatch = useAppDispatch(); |
| 48 |
|
| 49 |
// Update current route data to Redux and highlight the current page in the Nav bar |
| 50 |
const { browserTitle } = useUpdateRoute({ pathname: "subordinate-ids" }); |
| 51 |
|
| 52 |
// Set the page title to be shown in the browser tab |
| 53 |
React.useEffect(() => { |
| 54 |
document.title = browserTitle; |
| 55 |
}, [browserTitle]); |
| 56 |
|
| 57 |
// Retrieve API version from environment data |
| 58 |
const apiVersion = useAppSelector( |
| 59 |
(state) => state.global.environment.api_version |
| 60 |
) as string; |
| 61 |
|
| 62 |
// URL parameters: page number, page size, search value |
| 63 |
const { page, setPage, perPage, setPerPage, searchValue, setSearchValue } = |
| 64 |
useListPageSearchParams(); |
| 65 |
|
| 66 |
// Handle API calls errors |
| 67 |
const globalErrors = useApiError([]); |
| 68 |
|
| 69 |
// Page indexes |
| 70 |
const firstUserIdx = (page - 1) * perPage; |
| 71 |
const lastUserIdx = page * perPage; |
| 72 |
|
| 73 |
// States |
| 74 |
const [subIds, setSubIds] = React.useState<SubId[]>([]); |
| 75 |
const [searchDisabled, setSearchIsDisabled] = React.useState<boolean>(false); |
| 76 |
const [totalCount, setTotalCount] = React.useState<number>(0); |
| 77 |
|
| 78 |
// API calls |
| 79 |
const subIdsDataResponse = useGetSubIdEntriesQuery({ |
| 80 |
searchValue: searchValue, |
| 81 |
apiVersion, |
| 82 |
sizelimit: 100, |
| 83 |
startIdx: firstUserIdx, |
| 84 |
stopIdx: lastUserIdx, |
| 85 |
} as SubIdDataPayload); |
| 86 |
|
| 87 |
const { |
| 88 |
data: batchResponse, |
| 89 |
isLoading: isBatchLoading, |
| 90 |
error: batchError, |
| 91 |
} = subIdsDataResponse; |
| 92 |
|
| 93 |
// Handle data when the API call is finished |
| 94 |
React.useEffect(() => { |
| 95 |
if (subIdsDataResponse.isFetching) { |
| 96 |
setShowTableRows(false); |
| 97 |
// Reset selected elements on refresh |
| 98 |
setTotalCount(0); |
| 99 |
globalErrors.clear(); |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
// API response: Success |
| 104 |
if ( |
| 105 |
subIdsDataResponse.isSuccess && |
| 106 |
subIdsDataResponse.data && |
| 107 |
batchResponse !== undefined |
| 108 |
) { |
| 109 |
const listResult = batchResponse.result.results; |
| 110 |
const totalCount = batchResponse.result.totalCount; |
| 111 |
const listSize = batchResponse.result.count; |
| 112 |
const elementsList: SubId[] = []; |
| 113 |
|
| 114 |
for (let i = 0; i < listSize; i++) { |
| 115 |
elementsList.push(listResult[i].result); |
| 116 |
} |
| 117 |
|
| 118 |
setTotalCount(totalCount); |
| 119 |
// Update the list of elements |
| 120 |
setSubIds(elementsList); |
| 121 |
// Show table elements |
| 122 |
setShowTableRows(true); |
| 123 |
} |
| 124 |
|
| 125 |
// API response: Error |
| 126 |
if ( |
| 127 |
!subIdsDataResponse.isLoading && |
| 128 |
subIdsDataResponse.isError && |
| 129 |
subIdsDataResponse.error !== undefined |
| 130 |
) { |
| 131 |
// This normally happens when the user is not authorized to view the data |
| 132 |
// So instead of adding an error, refresh page |
| 133 |
window.location.reload(); |
| 134 |
} |
| 135 |
}, [batchResponse]); |
| 136 |
|
| 137 |
// Refresh button handling |
| 138 |
const refreshData = () => { |
| 139 |
// Hide table |
| 140 |
setShowTableRows(false); |
| 141 |
|
| 142 |
// Reset selected elements on refresh |
| 143 |
setTotalCount(0); |
| 144 |
|
| 145 |
subIdsDataResponse.refetch().then(() => { |
| 146 |
setShowTableRows(true); |
| 147 |
}); |
| 148 |
}; |
| 149 |
|
| 150 |
// Always refetch data when the component is loaded. |
| 151 |
// This ensures the data is always up-to-date. |
| 152 |
React.useEffect(() => { |
| 153 |
subIdsDataResponse.refetch(); |
| 154 |
}, []); |
| 155 |
|
| 156 |
// Show table rows |
| 157 |
const [showTableRows, setShowTableRows] = React.useState(!isBatchLoading); |
| 158 |
|
| 159 |
// Show table rows only when data is fully retrieved |
| 160 |
React.useEffect(() => { |
| 161 |
if (showTableRows !== !isBatchLoading) { |
| 162 |
setShowTableRows(!isBatchLoading); |
| 163 |
} |
| 164 |
}, [isBatchLoading]); |
| 165 |
|
| 166 |
// Pagination |
| 167 |
const updatePage = (newPage: number) => { |
| 168 |
setPage(newPage); |
| 169 |
}; |
| 170 |
|
| 171 |
const updatePerPage = (newSetPerPage: number) => { |
| 172 |
setPerPage(newSetPerPage); |
| 173 |
}; |
| 174 |
|
| 175 |
// Elements displayed on the first page |
| 176 |
const updateShownElementsList = (newShownElementsList: SubId[]) => { |
| 177 |
setSubIds(newShownElementsList); |
| 178 |
}; |
| 179 |
|
| 180 |
// Update search input valie |
| 181 |
const updateSearchValue = (value: string) => { |
| 182 |
setSearchValue(value); |
| 183 |
}; |
| 184 |
|
| 185 |
// Search API call |
| 186 |
const [searchEntry] = useSearchSubIdEntriesMutation(); |
| 187 |
|
| 188 |
const submitSearchValue = () => { |
| 189 |
searchEntry({ |
| 190 |
searchValue: searchValue, |
| 191 |
apiVersion, |
| 192 |
sizelimit: 100, |
| 193 |
startIdx: 0, |
| 194 |
stopIdx: 200, // Search will consider a max. of elements |
| 195 |
}).then((result) => { |
| 196 |
if ("data" in result) { |
| 197 |
const searchError = result.data?.error as |
| 198 |
| FetchBaseQueryError |
| 199 |
| SerializedError; |
| 200 |
|
| 201 |
if (searchError) { |
| 202 |
// Error |
| 203 |
let error: string | undefined = ""; |
| 204 |
if ("error" in searchError) { |
| 205 |
error = searchError.error; |
| 206 |
} else if ("message" in searchError) { |
| 207 |
error = searchError.message; |
| 208 |
} |
| 209 |
dispatch( |
| 210 |
addAlert({ |
| 211 |
name: "submit-search-value-error", |
| 212 |
title: error || "Error when searching for subordinate IDs", |
| 213 |
variant: "danger", |
| 214 |
}) |
| 215 |
); |
| 216 |
} else { |
| 217 |
// Success |
| 218 |
const listResult = result.data?.result.results || []; |
| 219 |
const listSize = result.data?.result.count || 0; |
| 220 |
const totalCount = result.data?.result.totalCount || 0; |
| 221 |
const elementsList: SubId[] = []; |
| 222 |
|
| 223 |
for (let i = 0; i < listSize; i++) { |
| 224 |
elementsList.push(listResult[i].result); |
| 225 |
} |
| 226 |
|
| 227 |
setTotalCount(totalCount); |
| 228 |
setSubIds(elementsList); |
| 229 |
setShowTableRows(true); |
| 230 |
} |
| 231 |
setSearchIsDisabled(false); |
| 232 |
} |
| 233 |
}); |
| 234 |
}; |
| 235 |
|
| 236 |
// Data wrappers |
| 237 |
// TODO: Better separation of concerts |
| 238 |
// - 'PaginationLayout' |
| 239 |
const paginationData = { |
| 240 |
page, |
| 241 |
perPage, |
| 242 |
updatePage, |
| 243 |
updatePerPage, |
| 244 |
updateSelectedPerPage: () => {}, |
| 245 |
updateShownElementsList: updateShownElementsList, |
| 246 |
totalCount, |
| 247 |
}; |
| 248 |
|
| 249 |
// SearchInputLayout |
| 250 |
const searchValueData = { |
| 251 |
searchValue, |
| 252 |
updateSearchValue, |
| 253 |
submitSearchValue, |
| 254 |
}; |
| 255 |
|
| 256 |
// Modals functionality |
| 257 |
const [showAddModal, setShowAddModal] = React.useState(false); |
| 258 |
|
| 259 |
const onOpenAddModal = () => { |
| 260 |
setShowAddModal(true); |
| 261 |
}; |
| 262 |
|
| 263 |
const onCloseAddModal = () => { |
| 264 |
setShowAddModal(false); |
| 265 |
}; |
| 266 |
|
| 267 |
// List of Toolbar items |
| 268 |
const toolbarItems: ToolbarItem[] = [ |
| 269 |
{ |
| 270 |
key: 0, |
| 271 |
element: ( |
| 272 |
<SearchInputLayout |
| 273 |
dataCy="search" |
| 274 |
name="search" |
| 275 |
ariaLabel="Search subIds" |
| 276 |
placeholder="Search" |
| 277 |
searchValueData={searchValueData} |
| 278 |
isDisabled={searchDisabled} |
| 279 |
/> |
| 280 |
), |
| 281 |
toolbarItemVariant: ToolbarItemVariant.label, |
| 282 |
toolbarItemGap: { default: "gapMd" }, |
| 283 |
}, |
| 284 |
{ |
| 285 |
key: 1, |
| 286 |
toolbarItemVariant: ToolbarItemVariant.separator, |
| 287 |
}, |
| 288 |
{ |
| 289 |
key: 2, |
| 290 |
element: ( |
| 291 |
<SecondaryButton |
| 292 |
dataCy="subids-button-refresh" |
| 293 |
onClickHandler={refreshData} |
| 294 |
isDisabled={!showTableRows} |
| 295 |
> |
| 296 |
Refresh |
| 297 |
</SecondaryButton> |
| 298 |
), |
| 299 |
}, |
| 300 |
{ |
| 301 |
key: 3, |
| 302 |
element: ( |
| 303 |
<SecondaryButton |
| 304 |
dataCy="subids-button-add" |
| 305 |
isDisabled={!showTableRows} |
| 306 |
onClickHandler={onOpenAddModal} |
| 307 |
> |
| 308 |
Add |
| 309 |
</SecondaryButton> |
| 310 |
), |
| 311 |
}, |
| 312 |
{ |
| 313 |
key: 4, |
| 314 |
toolbarItemVariant: ToolbarItemVariant.separator, |
| 315 |
}, |
| 316 |
{ |
| 317 |
key: 5, |
| 318 |
element: <HelpTextWithIconLayout textContent="Help" />, |
| 319 |
}, |
| 320 |
{ |
| 321 |
key: 6, |
| 322 |
element: ( |
| 323 |
<PaginationLayout |
| 324 |
list={subIds} |
| 325 |
paginationData={paginationData} |
| 326 |
widgetId="pagination-options-menu-top" |
| 327 |
isCompact={true} |
| 328 |
/> |
| 329 |
), |
| 330 |
toolbarItemAlignment: { default: "alignEnd" }, |
| 331 |
}, |
| 332 |
]; |
| 333 |
|
| 334 |
// Render component |
| 335 |
return ( |
| 336 |
<div> |
| 337 |
<PageSection hasBodyWrapper={false}> |
| 338 |
<TitleLayout |
| 339 |
id="Subordinate IDs page" |
| 340 |
headingLevel="h1" |
| 341 |
text="Subordinate IDs" |
| 342 |
/> |
| 343 |
</PageSection> |
| 344 |
<PageSection hasBodyWrapper={false} isFilled={false}> |
| 345 |
<Flex direction={{ default: "column" }}> |
| 346 |
<FlexItem> |
| 347 |
<ToolbarLayout toolbarItems={toolbarItems} /> |
| 348 |
</FlexItem> |
| 349 |
<FlexItem style={{ flex: "0 0 auto" }}> |
| 350 |
<OuterScrollContainer> |
| 351 |
<InnerScrollContainer |
| 352 |
style={{ height: "60vh", overflow: "auto" }} |
| 353 |
> |
| 354 |
{batchError !== undefined && batchError ? ( |
| 355 |
<GlobalErrors errors={globalErrors.getAll()} /> |
| 356 |
) : ( |
| 357 |
<MainTable |
| 358 |
tableTitle="Subordinate IDs table" |
| 359 |
shownElementsList={subIds} |
| 360 |
pk="ipauniqueid" |
| 361 |
keyNames={[ |
| 362 |
"ipauniqueid", |
| 363 |
"ipaowner", |
| 364 |
"ipasubgidnumber", |
| 365 |
"ipasubuidnumber", |
| 366 |
]} |
| 367 |
columnNames={[ |
| 368 |
"Unique ID", |
| 369 |
"Owner", |
| 370 |
"SubGID range start", |
| 371 |
"SubUID range start", |
| 372 |
]} |
| 373 |
hasCheckboxes={false} |
| 374 |
pathname="subordinate-ids" |
| 375 |
showTableRows={showTableRows} |
| 376 |
showLink={true} |
| 377 |
/> |
| 378 |
)} |
| 379 |
</InnerScrollContainer> |
| 380 |
</OuterScrollContainer> |
| 381 |
</FlexItem> |
| 382 |
<FlexItem style={{ flex: "0 0 auto", position: "sticky", bottom: 0 }}> |
| 383 |
<PaginationLayout |
| 384 |
list={subIds} |
| 385 |
paginationData={paginationData} |
| 386 |
variant={PaginationVariant.bottom} |
| 387 |
widgetId="pagination-options-menu-bottom" |
| 388 |
/> |
| 389 |
</FlexItem> |
| 390 |
</Flex> |
| 391 |
</PageSection> |
| 392 |
<AddModal |
| 393 |
isOpen={showAddModal} |
| 394 |
onCloseModal={onCloseAddModal} |
| 395 |
onRefresh={refreshData} |
| 396 |
title="Add Subordinate ID" |
| 397 |
/> |
| 398 |
</div> |
| 399 |
); |
| 400 |
}; |
| 401 |
|
| 402 |
export default SubordinateIDs; |