| 1 |
import React, { useEffect, useState } 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 { UserGroup } from "../../utils/datatypes/globalDataTypes"; |
| 8 |
// Layouts |
| 9 |
import SkeletonOnTableLayout from "../../components/layouts/Skeleton/SkeletonOnTableLayout"; |
| 10 |
// React Router DOM |
| 11 |
import { Link } from "react-router"; |
| 12 |
|
| 13 |
interface GroupsData { |
| 14 |
isUserGroupSelectable: (group: UserGroup) => boolean; |
| 15 |
selectedGroups: UserGroup[]; |
| 16 |
selectableGroupsTable: UserGroup[]; |
| 17 |
setGroupSelected: (group: UserGroup, isSelecting?: boolean) => void; |
| 18 |
clearSelectedGroups: () => 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: UserGroup[]; |
| 34 |
shownElementsList: UserGroup[]; |
| 35 |
showTableRows: boolean; |
| 36 |
groupsData: GroupsData; |
| 37 |
buttonsData: ButtonsData; |
| 38 |
paginationData: PaginationData; |
| 39 |
searchValue: string; |
| 40 |
} |
| 41 |
|
| 42 |
const UserGroupsTable = (props: PropsToTable) => { |
| 43 |
// Retrieve groups data from props |
| 44 |
const shownGroupsList = [...props.shownElementsList]; |
| 45 |
|
| 46 |
// Column names |
| 47 |
const columnNames = { |
| 48 |
cn: "Group Name", |
| 49 |
gidnumber: "GID", |
| 50 |
description: "Description", |
| 51 |
}; |
| 52 |
|
| 53 |
const isGroupSelected = (group: UserGroup) => { |
| 54 |
if ( |
| 55 |
props.groupsData.selectedGroups.find( |
| 56 |
(selectedGroup) => selectedGroup.cn[0] === group.cn[0] |
| 57 |
) |
| 58 |
) { |
| 59 |
return true; |
| 60 |
} else { |
| 61 |
return false; |
| 62 |
} |
| 63 |
}; |
| 64 |
|
| 65 |
// To allow shift+click to select/deselect multiple rows |
| 66 |
const [recentSelectedRowIndex, setRecentSelectedRowIndex] = useState< |
| 67 |
number | null |
| 68 |
>(null); |
| 69 |
const [shifting, setShifting] = useState(false); |
| 70 |
|
| 71 |
// On selecting one single row |
| 72 |
const onSelectGroup = ( |
| 73 |
group: UserGroup, |
| 74 |
rowIndex: number, |
| 75 |
isSelecting: boolean |
| 76 |
) => { |
| 77 |
// If the host is shift + selecting the checkboxes, then all intermediate checkboxes should be selected |
| 78 |
if (shifting && recentSelectedRowIndex !== null) { |
| 79 |
const numberSelected = rowIndex - recentSelectedRowIndex; |
| 80 |
const intermediateIndexes = |
| 81 |
numberSelected > 0 |
| 82 |
? Array.from( |
| 83 |
new Array(numberSelected + 1), |
| 84 |
(_x, i) => i + recentSelectedRowIndex |
| 85 |
) |
| 86 |
: Array.from( |
| 87 |
new Array(Math.abs(numberSelected) + 1), |
| 88 |
(_x, i) => i + rowIndex |
| 89 |
); |
| 90 |
intermediateIndexes.forEach((index) => |
| 91 |
props.groupsData.setGroupSelected(shownGroupsList[index], isSelecting) |
| 92 |
); |
| 93 |
} else { |
| 94 |
props.groupsData.setGroupSelected(group, isSelecting); |
| 95 |
} |
| 96 |
setRecentSelectedRowIndex(rowIndex); |
| 97 |
|
| 98 |
// Resetting 'isDisableEnableOp' |
| 99 |
props.buttonsData.updateIsDeleteButtonDisabled(false); |
| 100 |
|
| 101 |
// Update hostIdsSelected array |
| 102 |
if (isSelecting) { |
| 103 |
// Increment the elements selected per page (++) |
| 104 |
props.paginationData.updateSelectedPerPage( |
| 105 |
props.paginationData.selectedPerPage + 1 |
| 106 |
); |
| 107 |
} else { |
| 108 |
// Decrement the elements selected per page (--) |
| 109 |
props.paginationData.updateSelectedPerPage( |
| 110 |
props.paginationData.selectedPerPage - 1 |
| 111 |
); |
| 112 |
} |
| 113 |
}; |
| 114 |
|
| 115 |
// Reset 'selectedGroupIds' array if a delete operation has been done |
| 116 |
useEffect(() => { |
| 117 |
if (props.buttonsData.isDeletion) { |
| 118 |
props.groupsData.clearSelectedGroups(); |
| 119 |
props.buttonsData.updateIsDeletion(false); |
| 120 |
} |
| 121 |
}, [props.buttonsData.isDeletion]); |
| 122 |
|
| 123 |
// Enable 'Delete' button (if any host selected) |
| 124 |
useEffect(() => { |
| 125 |
if (props.groupsData.selectedGroups.length > 0) { |
| 126 |
props.buttonsData.updateIsDeleteButtonDisabled(false); |
| 127 |
} |
| 128 |
|
| 129 |
if (props.groupsData.selectedGroups.length === 0) { |
| 130 |
props.buttonsData.updateIsDeleteButtonDisabled(true); |
| 131 |
} |
| 132 |
}, [props.groupsData.selectedGroups]); |
| 133 |
|
| 134 |
// Keyboard event |
| 135 |
useEffect(() => { |
| 136 |
const onKeyDown = (e: KeyboardEvent) => { |
| 137 |
if (e.key === "Shift") { |
| 138 |
setShifting(true); |
| 139 |
} |
| 140 |
}; |
| 141 |
const onKeyUp = (e: KeyboardEvent) => { |
| 142 |
if (e.key === "Shift") { |
| 143 |
setShifting(false); |
| 144 |
} |
| 145 |
}; |
| 146 |
|
| 147 |
document.addEventListener("keydown", onKeyDown); |
| 148 |
document.addEventListener("keyup", onKeyUp); |
| 149 |
|
| 150 |
return () => { |
| 151 |
document.removeEventListener("keydown", onKeyDown); |
| 152 |
document.removeEventListener("keyup", onKeyUp); |
| 153 |
}; |
| 154 |
}, []); |
| 155 |
|
| 156 |
// Defining table header and body from here to avoid passing specific names to the Table Layout |
| 157 |
const header = ( |
| 158 |
<Tr> |
| 159 |
<Th modifier="wrap" aria-label="Select rows"></Th> |
| 160 |
<Th modifier="wrap">{columnNames.cn}</Th> |
| 161 |
<Th modifier="wrap">{columnNames.gidnumber}</Th> |
| 162 |
<Th modifier="wrap">{columnNames.description}</Th> |
| 163 |
</Tr> |
| 164 |
); |
| 165 |
|
| 166 |
const body = shownGroupsList.map((group, rowIndex) => ( |
| 167 |
<Tr key={group.cn} id={group.cn}> |
| 168 |
<Td |
| 169 |
dataLabel="checkbox" |
| 170 |
select={{ |
| 171 |
rowIndex, |
| 172 |
onSelect: (_event, isSelecting) => |
| 173 |
onSelectGroup(group, rowIndex, isSelecting), |
| 174 |
isSelected: isGroupSelected(group), |
| 175 |
isDisabled: !props.groupsData.isUserGroupSelectable(group), |
| 176 |
}} |
| 177 |
/> |
| 178 |
<Td dataLabel={columnNames.cn}> |
| 179 |
<Link to={"/user-groups/" + group.cn} state={group}> |
| 180 |
{group.cn} |
| 181 |
</Link> |
| 182 |
</Td> |
| 183 |
<Td dataLabel={columnNames.gidnumber}>{group.gidnumber}</Td> |
| 184 |
<Td dataLabel={columnNames.description}>{group.description}</Td> |
| 185 |
</Tr> |
| 186 |
)); |
| 187 |
|
| 188 |
const skeleton = ( |
| 189 |
<SkeletonOnTableLayout |
| 190 |
rows={4} |
| 191 |
colSpan={9} |
| 192 |
screenreaderText={"Loading table rows"} |
| 193 |
/> |
| 194 |
); |
| 195 |
|
| 196 |
return ( |
| 197 |
<TableLayout |
| 198 |
ariaLabel={"User groups table"} |
| 199 |
variant={"compact"} |
| 200 |
hasBorders={true} |
| 201 |
classes={"pf-v6-u-mt-md"} |
| 202 |
tableId={"hosts-table"} |
| 203 |
isStickyHeader={true} |
| 204 |
tableHeader={header} |
| 205 |
tableBody={!props.showTableRows ? skeleton : body} |
| 206 |
/> |
| 207 |
); |
| 208 |
}; |
| 209 |
|
| 210 |
export default UserGroupsTable; |