| 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 |
// Layouts |
| 7 |
import SkeletonOnTableLayout from "../../components/layouts/Skeleton/SkeletonOnTableLayout"; |
| 8 |
// Data types |
| 9 |
import { AutomemberEntry } from "src/utils/datatypes/globalDataTypes"; |
| 10 |
import { Link } from "react-router"; |
| 11 |
|
| 12 |
interface ElementData { |
| 13 |
isElementSelectable: (element: AutomemberEntry) => boolean; |
| 14 |
selectedElements: AutomemberEntry[]; |
| 15 |
selectableElementsTable: AutomemberEntry[]; |
| 16 |
setElementsSelected: (rule: AutomemberEntry, isSelecting?: boolean) => void; |
| 17 |
clearSelectedElements: () => void; |
| 18 |
} |
| 19 |
|
| 20 |
interface ButtonsData { |
| 21 |
updateIsDeleteButtonDisabled: (value: boolean) => void; |
| 22 |
isDeletion: boolean; |
| 23 |
updateIsDeletion: (value: boolean) => void; |
| 24 |
updateIsEnableButtonDisabled?: (value: boolean) => void; |
| 25 |
updateIsDisableButtonDisabled?: (value: boolean) => void; |
| 26 |
isDisableEnableOp?: boolean; |
| 27 |
updateIsDisableEnableOp?: (value: boolean) => void; |
| 28 |
} |
| 29 |
|
| 30 |
interface PaginationData { |
| 31 |
selectedPerPage: number; |
| 32 |
updateSelectedPerPage: (selected: number) => void; |
| 33 |
} |
| 34 |
|
| 35 |
interface PropsToTable { |
| 36 |
shownElementsList: AutomemberEntry[]; |
| 37 |
showTableRows: boolean; |
| 38 |
elementsData: ElementData; |
| 39 |
buttonsData: ButtonsData; |
| 40 |
paginationData: PaginationData; |
| 41 |
searchValue: string; |
| 42 |
automemberType: "user-group" | "host-group"; |
| 43 |
} |
| 44 |
|
| 45 |
const MainTable = (props: PropsToTable) => { |
| 46 |
// Retrieve elements data from props |
| 47 |
const shownElementsList = [...props.shownElementsList]; |
| 48 |
|
| 49 |
// Column names |
| 50 |
const columnNames = { |
| 51 |
automemberRule: "Automember rule", |
| 52 |
description: "Description", |
| 53 |
}; |
| 54 |
|
| 55 |
// When user status is updated, unselect selected rows |
| 56 |
useEffect(() => { |
| 57 |
if (props.buttonsData.isDisableEnableOp) { |
| 58 |
props.elementsData.clearSelectedElements(); |
| 59 |
} |
| 60 |
}, [props.buttonsData.isDisableEnableOp]); |
| 61 |
|
| 62 |
const isElementSelected = (element: AutomemberEntry) => { |
| 63 |
if ( |
| 64 |
props.elementsData.selectedElements.find( |
| 65 |
(selectedElement) => |
| 66 |
selectedElement.automemberRule === element.automemberRule |
| 67 |
) |
| 68 |
) { |
| 69 |
return true; |
| 70 |
} else { |
| 71 |
return false; |
| 72 |
} |
| 73 |
}; |
| 74 |
|
| 75 |
// To allow shift+click to select/deselect multiple rows |
| 76 |
const [recentSelectedRowIndex, setRecentSelectedRowIndex] = useState< |
| 77 |
number | null |
| 78 |
>(null); |
| 79 |
const [shifting, setShifting] = useState(false); |
| 80 |
|
| 81 |
// On selecting one single row |
| 82 |
const onSelectElement = ( |
| 83 |
element: AutomemberEntry, |
| 84 |
rowIndex: number, |
| 85 |
isSelecting: boolean |
| 86 |
) => { |
| 87 |
// If the element is shift + selecting the checkboxes, then all intermediate checkboxes should be selected |
| 88 |
if (shifting && recentSelectedRowIndex !== null) { |
| 89 |
const numberSelected = rowIndex - recentSelectedRowIndex; |
| 90 |
const intermediateIndexes = |
| 91 |
numberSelected > 0 |
| 92 |
? Array.from( |
| 93 |
new Array(numberSelected + 1), |
| 94 |
(_x, i) => i + recentSelectedRowIndex |
| 95 |
) |
| 96 |
: Array.from( |
| 97 |
new Array(Math.abs(numberSelected) + 1), |
| 98 |
(_x, i) => i + rowIndex |
| 99 |
); |
| 100 |
intermediateIndexes.forEach((index) => |
| 101 |
props.elementsData.setElementsSelected( |
| 102 |
shownElementsList[index], |
| 103 |
isSelecting |
| 104 |
) |
| 105 |
); |
| 106 |
} else { |
| 107 |
props.elementsData.setElementsSelected(element, isSelecting); |
| 108 |
} |
| 109 |
setRecentSelectedRowIndex(rowIndex); |
| 110 |
|
| 111 |
// Resetting 'isDisableEnableOp' |
| 112 |
props.buttonsData.updateIsDeleteButtonDisabled(false); |
| 113 |
|
| 114 |
// Update elementSelected array |
| 115 |
if (isSelecting) { |
| 116 |
// Increment the elements selected per page (++) |
| 117 |
props.paginationData.updateSelectedPerPage( |
| 118 |
props.paginationData.selectedPerPage + 1 |
| 119 |
); |
| 120 |
} else { |
| 121 |
// Decrement the elements selected per page (--) |
| 122 |
props.paginationData.updateSelectedPerPage( |
| 123 |
props.paginationData.selectedPerPage - 1 |
| 124 |
); |
| 125 |
} |
| 126 |
}; |
| 127 |
|
| 128 |
// Reset 'selectedElements array if a delete operation has been done |
| 129 |
useEffect(() => { |
| 130 |
if (props.buttonsData.isDeletion) { |
| 131 |
props.elementsData.clearSelectedElements(); |
| 132 |
props.buttonsData.updateIsDeletion(false); |
| 133 |
} |
| 134 |
}, [props.buttonsData.isDeletion]); |
| 135 |
|
| 136 |
// Enable 'Delete' button (if any element selected) |
| 137 |
useEffect(() => { |
| 138 |
if (props.elementsData.selectedElements.length > 0) { |
| 139 |
props.buttonsData.updateIsDeleteButtonDisabled(false); |
| 140 |
} |
| 141 |
|
| 142 |
if (props.elementsData.selectedElements.length === 0) { |
| 143 |
props.buttonsData.updateIsDeleteButtonDisabled(true); |
| 144 |
} |
| 145 |
}, [props.elementsData.selectedElements]); |
| 146 |
|
| 147 |
// Enable 'Delete' and 'Enable|Disable' option buttons (if any element selected) |
| 148 |
useEffect(() => { |
| 149 |
if (props.elementsData.selectedElements.length > 0) { |
| 150 |
props.buttonsData.updateIsDeleteButtonDisabled(false); |
| 151 |
} |
| 152 |
|
| 153 |
if (props.elementsData.selectedElements.length === 0) { |
| 154 |
props.buttonsData.updateIsDeleteButtonDisabled(true); |
| 155 |
if ( |
| 156 |
props.buttonsData.updateIsEnableButtonDisabled !== undefined && |
| 157 |
props.buttonsData.updateIsDisableButtonDisabled !== undefined |
| 158 |
) { |
| 159 |
props.buttonsData.updateIsDisableButtonDisabled(true); |
| 160 |
props.buttonsData.updateIsEnableButtonDisabled(true); |
| 161 |
} |
| 162 |
} |
| 163 |
}, [props.elementsData.selectedElements]); |
| 164 |
|
| 165 |
// Keyboard event |
| 166 |
useEffect(() => { |
| 167 |
const onKeyDown = (e: KeyboardEvent) => { |
| 168 |
if (e.key === "Shift") { |
| 169 |
setShifting(true); |
| 170 |
} |
| 171 |
}; |
| 172 |
const onKeyUp = (e: KeyboardEvent) => { |
| 173 |
if (e.key === "Shift") { |
| 174 |
setShifting(false); |
| 175 |
} |
| 176 |
}; |
| 177 |
|
| 178 |
document.addEventListener("keydown", onKeyDown); |
| 179 |
document.addEventListener("keyup", onKeyUp); |
| 180 |
|
| 181 |
return () => { |
| 182 |
document.removeEventListener("keydown", onKeyDown); |
| 183 |
document.removeEventListener("keyup", onKeyUp); |
| 184 |
}; |
| 185 |
}, []); |
| 186 |
|
| 187 |
// Defining table header and body from here to avoid passing specific names to the Table Layout |
| 188 |
const header = ( |
| 189 |
<Tr> |
| 190 |
<Th modifier="wrap" aria-label="Select rows"></Th> |
| 191 |
<Th modifier="wrap">{columnNames.automemberRule}</Th> |
| 192 |
<Th modifier="wrap">{columnNames.description}</Th> |
| 193 |
</Tr> |
| 194 |
); |
| 195 |
|
| 196 |
const body = shownElementsList.map((element, rowIndex) => ( |
| 197 |
<Tr key={element.automemberRule} id={element.automemberRule}> |
| 198 |
<Td |
| 199 |
dataLabel="checkbox" |
| 200 |
select={{ |
| 201 |
rowIndex, |
| 202 |
onSelect: (_event, isSelecting) => |
| 203 |
onSelectElement(element, rowIndex, isSelecting), |
| 204 |
isSelected: isElementSelected(element), |
| 205 |
isDisabled: !props.elementsData.isElementSelectable(element), |
| 206 |
}} |
| 207 |
/> |
| 208 |
|
| 209 |
<Td dataLabel={columnNames.automemberRule}> |
| 210 |
<Link |
| 211 |
to={"/" + props.automemberType + "-rules/" + element.automemberRule} |
| 212 |
state={element} |
| 213 |
> |
| 214 |
{element.automemberRule} |
| 215 |
</Link> |
| 216 |
</Td> |
| 217 |
<Td dataLabel={columnNames.description}>{element.description}</Td> |
| 218 |
</Tr> |
| 219 |
)); |
| 220 |
|
| 221 |
const skeleton = ( |
| 222 |
<SkeletonOnTableLayout |
| 223 |
rows={4} |
| 224 |
colSpan={9} |
| 225 |
screenreaderText={"Loading table rows"} |
| 226 |
/> |
| 227 |
); |
| 228 |
|
| 229 |
return ( |
| 230 |
<TableLayout |
| 231 |
ariaLabel={"Automember user groups table"} |
| 232 |
variant={"compact"} |
| 233 |
hasBorders={true} |
| 234 |
classes={"pf-v6-u-mt-md"} |
| 235 |
tableId={"automember-user-groups-table"} |
| 236 |
isStickyHeader={true} |
| 237 |
tableHeader={header} |
| 238 |
tableBody={!props.showTableRows ? skeleton : body} |
| 239 |
/> |
| 240 |
); |
| 241 |
}; |
| 242 |
|
| 243 |
export default MainTable; |