| 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 { Trust } 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 { useAppSelector, useAppDispatch } from "src/store/hooks"; |
| 23 |
// RPC |
| 24 |
import { |
| 25 |
useGetTrustsFullDataQuery, |
| 26 |
useSearchTrustsEntriesMutation, |
| 27 |
} from "src/services/rpcTrusts"; |
| 28 |
// Utils |
| 29 |
import { apiToTrust } from "src/utils/trustsUtils"; |
| 30 |
import { isTrustSelectable } from "src/utils/utils"; |
| 31 |
// React router |
| 32 |
import { useNavigate } from "react-router"; |
| 33 |
// Components |
| 34 |
import { FetchBaseQueryError } from "@reduxjs/toolkit/query"; |
| 35 |
import { SerializedError } from "@reduxjs/toolkit"; |
| 36 |
import ToolbarLayout, { |
| 37 |
ToolbarItem, |
| 38 |
} from "src/components/layouts/ToolbarLayout"; |
| 39 |
import SearchInputLayout from "src/components/layouts/SearchInputLayout"; |
| 40 |
import SecondaryButton from "src/components/layouts/SecondaryButton"; |
| 41 |
import HelpTextWithIconLayout from "src/components/layouts/HelpTextWithIconLayout"; |
| 42 |
import PaginationLayout from "src/components/layouts/PaginationLayout"; |
| 43 |
import TitleLayout from "src/components/layouts/TitleLayout"; |
| 44 |
import GlobalErrors from "src/components/errors/GlobalErrors"; |
| 45 |
import MainTable from "src/components/tables/MainTable"; |
| 46 |
import BulkSelectorPrep from "src/components/BulkSelectorPrep"; |
| 47 |
import AddTrustModal from "./AddTrustModal"; |
| 48 |
import DeleteTrustModal from "./DeleteTrustModal"; |
| 49 |
|
| 50 |
const Trusts = () => { |
| 51 |
const navigate = useNavigate(); |
| 52 |
|
| 53 |
const dispatch = useAppDispatch(); |
| 54 |
|
| 55 |
const { browserTitle } = useUpdateRoute({ |
| 56 |
pathname: "trusts", |
| 57 |
}); |
| 58 |
|
| 59 |
// Set the page title to be shown in the browser tab |
| 60 |
React.useEffect(() => { |
| 61 |
document.title = browserTitle; |
| 62 |
}, [browserTitle]); |
| 63 |
|
| 64 |
// Retrieve API version from environment data |
| 65 |
const apiVersion = useAppSelector( |
| 66 |
(state) => state.global.environment.api_version |
| 67 |
) as string; |
| 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 |
|
| 76 |
// Page indexes |
| 77 |
const firstUserIdx = (page - 1) * perPage; |
| 78 |
const lastUserIdx = page * perPage; |
| 79 |
|
| 80 |
// States |
| 81 |
const [isSearchDisabled, setIsSearchDisabled] = React.useState(false); |
| 82 |
|
| 83 |
// API calls |
| 84 |
const trustsResponse = useGetTrustsFullDataQuery({ |
| 85 |
searchValue, |
| 86 |
apiVersion, |
| 87 |
sizelimit: 100, |
| 88 |
startIdx: firstUserIdx, |
| 89 |
stopIdx: lastUserIdx, |
| 90 |
}); |
| 91 |
|
| 92 |
const { data, isLoading, error } = trustsResponse; |
| 93 |
|
| 94 |
// Process data and update state when response changes |
| 95 |
React.useEffect(() => { |
| 96 |
if (trustsResponse.isFetching) { |
| 97 |
globalErrors.clear(); |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
// API response: Error |
| 102 |
if ( |
| 103 |
!trustsResponse.isLoading && |
| 104 |
trustsResponse.isError && |
| 105 |
trustsResponse.error !== undefined |
| 106 |
) { |
| 107 |
// This normally happens when the user is not authorized to view the data |
| 108 |
// So instead of adding an error, refresh page |
| 109 |
navigate("/login"); |
| 110 |
window.location.reload(); |
| 111 |
} |
| 112 |
}, [trustsResponse, navigate, globalErrors]); |
| 113 |
|
| 114 |
// Compute trusts data from API response |
| 115 |
const trusts = React.useMemo(() => { |
| 116 |
if (trustsResponse.isSuccess && trustsResponse.data && data !== undefined) { |
| 117 |
const listResult = data.result.results; |
| 118 |
const listSize = data.result.count; |
| 119 |
const elementsList: Trust[] = []; |
| 120 |
|
| 121 |
for (let i = 0; i < listSize; i++) { |
| 122 |
elementsList.push(apiToTrust(listResult[i].result)); |
| 123 |
} |
| 124 |
|
| 125 |
return elementsList; |
| 126 |
} |
| 127 |
return []; |
| 128 |
}, [data, trustsResponse.isSuccess, trustsResponse.data]); |
| 129 |
|
| 130 |
// Compute total count from API response |
| 131 |
const totalCount = React.useMemo(() => { |
| 132 |
if (trustsResponse.isSuccess && trustsResponse.data) { |
| 133 |
return trustsResponse.data.result.totalCount; |
| 134 |
} |
| 135 |
return 0; |
| 136 |
}, [trustsResponse.isSuccess, trustsResponse.data]); |
| 137 |
|
| 138 |
// Compute derived state for showTableRows |
| 139 |
const showTableRows = React.useMemo(() => { |
| 140 |
if (trustsResponse.isFetching) { |
| 141 |
return false; |
| 142 |
} |
| 143 |
return !isLoading; |
| 144 |
}, [trustsResponse.isFetching, isLoading]); |
| 145 |
|
| 146 |
// Selected elements |
| 147 |
const [selectedElements, setSelectedElements] = React.useState<Trust[]>([]); |
| 148 |
const [selectedPerPage, setSelectedPerPage] = React.useState<number>(0); |
| 149 |
|
| 150 |
// Refresh button handling |
| 151 |
const refreshData = () => { |
| 152 |
// Reset selected elements on refresh |
| 153 |
setSelectedElements([]); |
| 154 |
|
| 155 |
trustsResponse.refetch(); |
| 156 |
}; |
| 157 |
|
| 158 |
// 'Delete' button state |
| 159 |
const [isDeleteButtonDisabled, setIsDeleteButtonDisabled] = |
| 160 |
React.useState<boolean>(true); |
| 161 |
|
| 162 |
const [isDeletion, setIsDeletion] = React.useState<boolean>(false); |
| 163 |
|
| 164 |
// Table-related shared functionality |
| 165 |
// - Selectable checkboxes on table |
| 166 |
const selectableTrustsTable = trusts.filter(isTrustSelectable); |
| 167 |
|
| 168 |
const updateSelectedTrusts = (trusts: Trust[], isSelected: boolean) => { |
| 169 |
let newSelectedTrusts: Trust[] = []; |
| 170 |
if (isSelected) { |
| 171 |
newSelectedTrusts = JSON.parse(JSON.stringify(selectedElements)); |
| 172 |
for (let i = 0; i < trusts.length; i++) { |
| 173 |
if ( |
| 174 |
selectedElements.find( |
| 175 |
(selectedTrust) => selectedTrust.cn === trusts[i].cn |
| 176 |
) |
| 177 |
) { |
| 178 |
// Already in the list |
| 179 |
continue; |
| 180 |
} |
| 181 |
// Add element to list |
| 182 |
newSelectedTrusts.push(trusts[i]); |
| 183 |
} |
| 184 |
} else { |
| 185 |
// Remove element |
| 186 |
for (let i = 0; i < selectedElements.length; i++) { |
| 187 |
let found = false; |
| 188 |
for (let ii = 0; ii < trusts.length; ii++) { |
| 189 |
if (selectedElements[i].cn === trusts[ii].cn) { |
| 190 |
found = true; |
| 191 |
break; |
| 192 |
} |
| 193 |
} |
| 194 |
if (!found) { |
| 195 |
// Keep this valid selected entry |
| 196 |
newSelectedTrusts.push(selectedElements[i]); |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
setSelectedElements(newSelectedTrusts); |
| 201 |
setIsDeleteButtonDisabled(newSelectedTrusts.length === 0); |
| 202 |
}; |
| 203 |
|
| 204 |
// - Helper method to set the selected entries from the table |
| 205 |
const setTrustsSelected = (trust: Trust, isSelecting = true) => { |
| 206 |
if (isTrustSelectable(trust)) { |
| 207 |
updateSelectedTrusts([trust], isSelecting); |
| 208 |
} |
| 209 |
}; |
| 210 |
|
| 211 |
// Always refetch data when the component is loaded. |
| 212 |
// This ensures the data is always up-to-date. |
| 213 |
React.useEffect(() => { |
| 214 |
trustsResponse.refetch(); |
| 215 |
}, []); |
| 216 |
|
| 217 |
// Search API call |
| 218 |
const [searchEntry] = useSearchTrustsEntriesMutation(); |
| 219 |
|
| 220 |
const submitSearchValue = () => { |
| 221 |
searchEntry({ |
| 222 |
searchValue, |
| 223 |
apiVersion, |
| 224 |
sizelimit: 100, |
| 225 |
startIdx: 0, |
| 226 |
stopIdx: 100, |
| 227 |
}).then((result) => { |
| 228 |
if ("data" in result) { |
| 229 |
const searchError = result.data?.error as |
| 230 |
| FetchBaseQueryError |
| 231 |
| SerializedError; |
| 232 |
|
| 233 |
if (searchError) { |
| 234 |
// Error |
| 235 |
let error: string | undefined = ""; |
| 236 |
if ("error" in searchError) { |
| 237 |
error = searchError.error; |
| 238 |
} else if ("message" in searchError) { |
| 239 |
error = searchError.message; |
| 240 |
} |
| 241 |
dispatch( |
| 242 |
addAlert({ |
| 243 |
name: "submit-search-value-error", |
| 244 |
title: error || "Error when searching for elements", |
| 245 |
variant: "danger", |
| 246 |
}) |
| 247 |
); |
| 248 |
} else { |
| 249 |
// Success - data will be updated through the API response |
| 250 |
// No need to manually set state as it's computed from the response |
| 251 |
} |
| 252 |
setIsSearchDisabled(false); |
| 253 |
} |
| 254 |
}); |
| 255 |
}; |
| 256 |
|
| 257 |
// Data wrappers |
| 258 |
// TODO: Better separation of concerts |
| 259 |
// - 'PaginationLayout' |
| 260 |
const paginationData = { |
| 261 |
page, |
| 262 |
perPage, |
| 263 |
updatePage: setPage, |
| 264 |
updatePerPage: setPerPage, |
| 265 |
updateSelectedPerPage: setSelectedPerPage, |
| 266 |
totalCount, |
| 267 |
}; |
| 268 |
|
| 269 |
// SearchInputLayout |
| 270 |
const searchValueData = { |
| 271 |
searchValue, |
| 272 |
updateSearchValue: setSearchValue, |
| 273 |
submitSearchValue, |
| 274 |
}; |
| 275 |
|
| 276 |
// - 'BulkSelectorprep' |
| 277 |
const bulkSelectorData = { |
| 278 |
selected: selectedElements, |
| 279 |
updateSelected: updateSelectedTrusts, |
| 280 |
selectableTable: selectableTrustsTable, |
| 281 |
nameAttr: "cn", |
| 282 |
}; |
| 283 |
|
| 284 |
const selectedPerPageData = { |
| 285 |
selectedPerPage, |
| 286 |
updateSelectedPerPage: setSelectedPerPage, |
| 287 |
}; |
| 288 |
|
| 289 |
// Modals functionality |
| 290 |
const [showAddModal, setShowAddModal] = React.useState<boolean>(false); |
| 291 |
const [showDeleteModal, setShowDeleteModal] = React.useState<boolean>(false); |
| 292 |
|
| 293 |
// List of Toolbar items |
| 294 |
const toolbarItems: ToolbarItem[] = [ |
| 295 |
{ |
| 296 |
key: 0, |
| 297 |
element: ( |
| 298 |
<BulkSelectorPrep |
| 299 |
list={trusts} |
| 300 |
shownElementsList={trusts} |
| 301 |
elementData={bulkSelectorData} |
| 302 |
buttonsData={{ |
| 303 |
updateIsDeleteButtonDisabled: setIsDeleteButtonDisabled, |
| 304 |
}} |
| 305 |
selectedPerPageData={selectedPerPageData} |
| 306 |
/> |
| 307 |
), |
| 308 |
}, |
| 309 |
{ |
| 310 |
key: 1, |
| 311 |
element: ( |
| 312 |
<SearchInputLayout |
| 313 |
dataCy="search" |
| 314 |
name="search" |
| 315 |
ariaLabel="Search trusts" |
| 316 |
placeholder="Search" |
| 317 |
searchValueData={searchValueData} |
| 318 |
isDisabled={isSearchDisabled} |
| 319 |
/> |
| 320 |
), |
| 321 |
toolbarItemVariant: ToolbarItemVariant.label, |
| 322 |
toolbarItemGap: { default: "gapMd" }, |
| 323 |
}, |
| 324 |
{ |
| 325 |
key: 2, |
| 326 |
toolbarItemVariant: ToolbarItemVariant.separator, |
| 327 |
}, |
| 328 |
{ |
| 329 |
key: 3, |
| 330 |
element: ( |
| 331 |
<SecondaryButton |
| 332 |
dataCy="trusts-button-refresh" |
| 333 |
onClickHandler={refreshData} |
| 334 |
isDisabled={!showTableRows} |
| 335 |
> |
| 336 |
Refresh |
| 337 |
</SecondaryButton> |
| 338 |
), |
| 339 |
}, |
| 340 |
{ |
| 341 |
key: 4, |
| 342 |
element: ( |
| 343 |
<SecondaryButton |
| 344 |
isDisabled={isDeleteButtonDisabled || !showTableRows} |
| 345 |
dataCy="trusts-button-delete" |
| 346 |
onClickHandler={() => setShowDeleteModal(true)} |
| 347 |
> |
| 348 |
Delete |
| 349 |
</SecondaryButton> |
| 350 |
), |
| 351 |
}, |
| 352 |
{ |
| 353 |
key: 5, |
| 354 |
element: ( |
| 355 |
<SecondaryButton |
| 356 |
isDisabled={!showTableRows} |
| 357 |
dataCy="trusts-button-add" |
| 358 |
onClickHandler={() => setShowAddModal(true)} |
| 359 |
> |
| 360 |
Add |
| 361 |
</SecondaryButton> |
| 362 |
), |
| 363 |
}, |
| 364 |
{ |
| 365 |
key: 6, |
| 366 |
toolbarItemVariant: ToolbarItemVariant.separator, |
| 367 |
}, |
| 368 |
{ |
| 369 |
key: 7, |
| 370 |
element: <HelpTextWithIconLayout textContent="Help" />, |
| 371 |
}, |
| 372 |
{ |
| 373 |
key: 8, |
| 374 |
element: ( |
| 375 |
<PaginationLayout |
| 376 |
list={trusts} |
| 377 |
paginationData={paginationData} |
| 378 |
widgetId="pagination-options-menu-top" |
| 379 |
isCompact={true} |
| 380 |
/> |
| 381 |
), |
| 382 |
toolbarItemAlignment: { default: "alignEnd" }, |
| 383 |
}, |
| 384 |
]; |
| 385 |
|
| 386 |
// Render component |
| 387 |
return ( |
| 388 |
<div> |
| 389 |
<PageSection hasBodyWrapper={false}> |
| 390 |
<TitleLayout id="Trusts page" headingLevel="h1" text="Trusts" /> |
| 391 |
</PageSection> |
| 392 |
<PageSection hasBodyWrapper={false} isFilled={false}> |
| 393 |
<Flex direction={{ default: "column" }}> |
| 394 |
<FlexItem> |
| 395 |
<ToolbarLayout toolbarItems={toolbarItems} /> |
| 396 |
</FlexItem> |
| 397 |
<FlexItem style={{ flex: "0 0 auto" }}> |
| 398 |
<OuterScrollContainer> |
| 399 |
<InnerScrollContainer |
| 400 |
style={{ height: "55vh", overflow: "auto" }} |
| 401 |
> |
| 402 |
{error !== undefined && error ? ( |
| 403 |
<GlobalErrors errors={globalErrors.getAll()} /> |
| 404 |
) : ( |
| 405 |
<MainTable |
| 406 |
tableTitle="Trusts table" |
| 407 |
shownElementsList={trusts} |
| 408 |
pk="cn" |
| 409 |
keyNames={["cn"]} |
| 410 |
columnNames={["Realm name"]} |
| 411 |
hasCheckboxes={true} |
| 412 |
pathname="trusts" |
| 413 |
showTableRows={showTableRows} |
| 414 |
showLink={true} |
| 415 |
elementsData={{ |
| 416 |
isElementSelectable: isTrustSelectable, |
| 417 |
selectedElements, |
| 418 |
selectableElementsTable: selectableTrustsTable, |
| 419 |
setElementsSelected: setTrustsSelected, |
| 420 |
clearSelectedElements: () => setSelectedElements([]), |
| 421 |
}} |
| 422 |
buttonsData={{ |
| 423 |
updateIsDeleteButtonDisabled: (value) => |
| 424 |
setIsDeleteButtonDisabled(value), |
| 425 |
isDeletion, |
| 426 |
updateIsDeletion: (value) => setIsDeletion(value), |
| 427 |
isDisableEnableOp: true, |
| 428 |
}} |
| 429 |
paginationData={{ |
| 430 |
selectedPerPage, |
| 431 |
updateSelectedPerPage: setSelectedPerPage, |
| 432 |
}} |
| 433 |
/> |
| 434 |
)} |
| 435 |
</InnerScrollContainer> |
| 436 |
</OuterScrollContainer> |
| 437 |
</FlexItem> |
| 438 |
<FlexItem style={{ flex: "0 0 auto", position: "sticky", bottom: 0 }}> |
| 439 |
<PaginationLayout |
| 440 |
list={trusts} |
| 441 |
paginationData={paginationData} |
| 442 |
variant={PaginationVariant.bottom} |
| 443 |
widgetId="pagination-options-menu-bottom" |
| 444 |
/> |
| 445 |
</FlexItem> |
| 446 |
</Flex> |
| 447 |
</PageSection> |
| 448 |
<AddTrustModal |
| 449 |
isOpen={showAddModal} |
| 450 |
onClose={() => setShowAddModal(false)} |
| 451 |
title="Add trust" |
| 452 |
onRefresh={refreshData} |
| 453 |
/> |
| 454 |
<DeleteTrustModal |
| 455 |
isOpen={showDeleteModal} |
| 456 |
onClose={() => setShowDeleteModal(false)} |
| 457 |
elementsToDelete={selectedElements} |
| 458 |
clearSelectedElements={() => setSelectedElements([])} |
| 459 |
columnNames={["Realm name"]} |
| 460 |
keyNames={["cn"]} |
| 461 |
onRefresh={refreshData} |
| 462 |
updateIsDeleteButtonDisabled={setIsDeleteButtonDisabled} |
| 463 |
updateIsDeletion={setIsDeletion} |
| 464 |
/> |
| 465 |
</div> |
| 466 |
); |
| 467 |
}; |
| 468 |
|
| 469 |
export default Trusts; |