| 1 |
import React, { useEffect } from "react"; |
| 2 |
// PatternFly |
| 3 |
import { Td, Th, Tr } from "@patternfly/react-table"; |
| 4 |
// Tables |
| 5 |
import TableLayout from "../../components/layouts/TableLayout"; |
| 6 |
// Data types |
| 7 |
import { IDViewOverrideUser } from "src/utils/datatypes/globalDataTypes"; |
| 8 |
// Layouts |
| 9 |
import SkeletonOnTableLayout from "../../components/layouts/Skeleton/SkeletonOnTableLayout"; |
| 10 |
// Hooks |
| 11 |
import useShifting from "src/hooks/useShifting"; |
| 12 |
|
| 13 |
interface IDViewsOverrideData { |
| 14 |
isSelectable: (view: IDViewOverrideUser) => boolean; |
| 15 |
selected: string[]; |
| 16 |
selectableTable: IDViewOverrideUser[]; |
| 17 |
setSelectedUsers: (users: string[]) => void; |
| 18 |
clearSelected: () => void; |
| 19 |
} |
| 20 |
|
| 21 |
interface ButtonsData { |
| 22 |
updateIsDeleteButtonDisabled: (value: boolean) => void; |
| 23 |
isDeletion: boolean; |
| 24 |
updateIsDeletion: (value: boolean) => void; |
| 25 |
} |
| 26 |
|
| 27 |
interface PaginationData { |
| 28 |
selectedPerPage: number; |
| 29 |
updateSelectedPerPage: (selected: number) => void; |
| 30 |
} |
| 31 |
|
| 32 |
interface PropsToTable { |
| 33 |
elementsList: IDViewOverrideUser[]; |
| 34 |
shownElementsList: IDViewOverrideUser[]; |
| 35 |
showTableRows: boolean; |
| 36 |
overrideEntryData: IDViewsOverrideData; |
| 37 |
buttonsData: ButtonsData; |
| 38 |
paginationData: PaginationData; |
| 39 |
} |
| 40 |
|
| 41 |
const IDViewsOverrideUsersTable = (props: PropsToTable) => { |
| 42 |
// Retrieve views data from props |
| 43 |
const shownUsersList = [...props.shownElementsList]; |
| 44 |
|
| 45 |
// Column names |
| 46 |
const userColumnNames = { |
| 47 |
ipaanchoruuid: "User to override", |
| 48 |
uid: "User login", |
| 49 |
uidnumber: "UID", |
| 50 |
homedirectory: "Home directory", |
| 51 |
description: "Description", |
| 52 |
}; |
| 53 |
// - Returns 'True' if a specific table user has been selected |
| 54 |
const isOverrideSelected = (user: IDViewOverrideUser) => |
| 55 |
props.overrideEntryData.selected.includes(user.ipaanchoruuid[0]); |
| 56 |
|
| 57 |
// On selecting one single row |
| 58 |
const onSelect = useShifting( |
| 59 |
shownUsersList, |
| 60 |
props.overrideEntryData.setSelectedUsers |
| 61 |
); |
| 62 |
|
| 63 |
// Reset 'selectedViewIds' array if a delete operation has been done |
| 64 |
useEffect(() => { |
| 65 |
if (props.buttonsData.isDeletion) { |
| 66 |
props.overrideEntryData.clearSelected(); |
| 67 |
props.buttonsData.updateIsDeletion(false); |
| 68 |
} |
| 69 |
}, [props.buttonsData.isDeletion]); |
| 70 |
|
| 71 |
// Enable 'Delete' button (if any host selected) |
| 72 |
useEffect(() => { |
| 73 |
if (props.overrideEntryData.selected.length > 0) { |
| 74 |
props.buttonsData.updateIsDeleteButtonDisabled(false); |
| 75 |
} |
| 76 |
|
| 77 |
if (props.overrideEntryData.selected.length === 0) { |
| 78 |
props.buttonsData.updateIsDeleteButtonDisabled(true); |
| 79 |
} |
| 80 |
}, [props.overrideEntryData.selected]); |
| 81 |
|
| 82 |
// Defining table header and body from here to avoid passing specific names to the Table Layout |
| 83 |
const header = ( |
| 84 |
<Tr> |
| 85 |
<Th modifier="wrap"></Th> |
| 86 |
<Th modifier="wrap">{userColumnNames.ipaanchoruuid}</Th> |
| 87 |
<Th modifier="wrap">{userColumnNames.uid}</Th> |
| 88 |
<Th modifier="wrap">{userColumnNames.uidnumber}</Th> |
| 89 |
<Th modifier="wrap">{userColumnNames.homedirectory}</Th> |
| 90 |
<Th modifier="wrap">{userColumnNames.description}</Th> |
| 91 |
</Tr> |
| 92 |
); |
| 93 |
|
| 94 |
const body = shownUsersList.map((user, rowIndex) => ( |
| 95 |
<Tr key={user.ipaanchoruuid} id={user.ipaanchoruuid}> |
| 96 |
<Td |
| 97 |
dataLabel="checkbox" |
| 98 |
select={{ |
| 99 |
rowIndex, |
| 100 |
onSelect: (_event, isSelecting) => |
| 101 |
onSelect(user.ipaanchoruuid[0], rowIndex, isSelecting), |
| 102 |
isSelected: isOverrideSelected(user), |
| 103 |
isDisabled: !props.overrideEntryData.isSelectable(user), |
| 104 |
}} |
| 105 |
/> |
| 106 |
<Td dataLabel={userColumnNames.ipaanchoruuid}>{user.ipaanchoruuid}</Td> |
| 107 |
<Td dataLabel={userColumnNames.uid}>{user.uid}</Td> |
| 108 |
<Td dataLabel={userColumnNames.uidnumber}>{user.uidnumber}</Td> |
| 109 |
<Td dataLabel={userColumnNames.homedirectory}>{user.homedirectory}</Td> |
| 110 |
<Td dataLabel={userColumnNames.description}>{user.description}</Td> |
| 111 |
</Tr> |
| 112 |
)); |
| 113 |
|
| 114 |
const skeleton = ( |
| 115 |
<SkeletonOnTableLayout rows={4} colSpan={9} screenreaderText={"No users"} /> |
| 116 |
); |
| 117 |
|
| 118 |
return ( |
| 119 |
<TableLayout |
| 120 |
ariaLabel={"ID views override user table"} |
| 121 |
variant={"compact"} |
| 122 |
hasBorders={true} |
| 123 |
tableId={"id-views-override-user-table"} |
| 124 |
isStickyHeader={true} |
| 125 |
tableHeader={header} |
| 126 |
tableBody={!props.showTableRows ? skeleton : body} |
| 127 |
/> |
| 128 |
); |
| 129 |
}; |
| 130 |
|
| 131 |
export default IDViewsOverrideUsersTable; |