| 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 { IDViewOverrideGroup } 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: IDViewOverrideGroup) => boolean; |
| 15 |
selected: string[]; |
| 16 |
selectableTable: IDViewOverrideGroup[]; |
| 17 |
setSelectedGroups: (group: 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: IDViewOverrideGroup[]; |
| 34 |
shownElementsList: IDViewOverrideGroup[]; |
| 35 |
showTableRows: boolean; |
| 36 |
overrideEntryData: IDViewsOverrideData; |
| 37 |
buttonsData: ButtonsData; |
| 38 |
paginationData: PaginationData; |
| 39 |
} |
| 40 |
|
| 41 |
const IDViewsOverrideGroupsTable = (props: PropsToTable) => { |
| 42 |
// Retrieve views data from props |
| 43 |
const shownGroupsList = [...props.shownElementsList]; |
| 44 |
|
| 45 |
// Column names |
| 46 |
const groupColumnNames = { |
| 47 |
ipaanchoruuid: "Group to override", |
| 48 |
cn: "Group name", |
| 49 |
gidnumber: "GID", |
| 50 |
description: "Description", |
| 51 |
}; |
| 52 |
// - Returns 'True' if a specific table group has been selected |
| 53 |
const isOverrideSelected = (group: IDViewOverrideGroup) => |
| 54 |
props.overrideEntryData.selected.includes(group.ipaanchoruuid[0]); |
| 55 |
|
| 56 |
// On selecting one single row |
| 57 |
const onSelect = useShifting( |
| 58 |
shownGroupsList, |
| 59 |
props.overrideEntryData.setSelectedGroups |
| 60 |
); |
| 61 |
|
| 62 |
// Reset 'selectedViewIds' array if a delete operation has been done |
| 63 |
useEffect(() => { |
| 64 |
if (props.buttonsData.isDeletion) { |
| 65 |
props.overrideEntryData.clearSelected(); |
| 66 |
props.buttonsData.updateIsDeletion(false); |
| 67 |
} |
| 68 |
}, [props.buttonsData.isDeletion]); |
| 69 |
|
| 70 |
// Enable 'Delete' button (if any host selected) |
| 71 |
useEffect(() => { |
| 72 |
if (props.overrideEntryData.selected.length > 0) { |
| 73 |
props.buttonsData.updateIsDeleteButtonDisabled(false); |
| 74 |
} |
| 75 |
|
| 76 |
if (props.overrideEntryData.selected.length === 0) { |
| 77 |
props.buttonsData.updateIsDeleteButtonDisabled(true); |
| 78 |
} |
| 79 |
}, [props.overrideEntryData.selected]); |
| 80 |
|
| 81 |
// Defining table header and body from here to avoid passing specific names to the Table Layout |
| 82 |
const header = ( |
| 83 |
<Tr> |
| 84 |
<Th modifier="wrap"></Th> |
| 85 |
<Th modifier="wrap">{groupColumnNames.ipaanchoruuid}</Th> |
| 86 |
<Th modifier="wrap">{groupColumnNames.cn}</Th> |
| 87 |
<Th modifier="wrap">{groupColumnNames.gidnumber}</Th> |
| 88 |
<Th modifier="wrap">{groupColumnNames.description}</Th> |
| 89 |
</Tr> |
| 90 |
); |
| 91 |
|
| 92 |
const body = shownGroupsList.map((group, rowIndex) => ( |
| 93 |
<Tr key={group.ipaanchoruuid} id={group.ipaanchoruuid}> |
| 94 |
<Td |
| 95 |
dataLabel="checkbox" |
| 96 |
select={{ |
| 97 |
rowIndex, |
| 98 |
onSelect: (_event, isSelecting) => |
| 99 |
onSelect(group.ipaanchoruuid[0], rowIndex, isSelecting), |
| 100 |
isSelected: isOverrideSelected(group), |
| 101 |
isDisabled: !props.overrideEntryData.isSelectable(group), |
| 102 |
}} |
| 103 |
/> |
| 104 |
<Td dataLabel={groupColumnNames.ipaanchoruuid}>{group.ipaanchoruuid}</Td> |
| 105 |
<Td dataLabel={groupColumnNames.cn}>{group.cn}</Td> |
| 106 |
<Td dataLabel={groupColumnNames.gidnumber}>{group.gidnumber}</Td> |
| 107 |
<Td dataLabel={groupColumnNames.description}>{group.description}</Td> |
| 108 |
</Tr> |
| 109 |
)); |
| 110 |
|
| 111 |
const skeleton = ( |
| 112 |
<SkeletonOnTableLayout |
| 113 |
rows={4} |
| 114 |
colSpan={9} |
| 115 |
screenreaderText={"No groups"} |
| 116 |
/> |
| 117 |
); |
| 118 |
|
| 119 |
return ( |
| 120 |
<TableLayout |
| 121 |
ariaLabel={"ID views override group table"} |
| 122 |
variant={"compact"} |
| 123 |
hasBorders={true} |
| 124 |
tableId={"id-views-override-group-table"} |
| 125 |
isStickyHeader={true} |
| 126 |
tableHeader={header} |
| 127 |
tableBody={!props.showTableRows ? skeleton : body} |
| 128 |
/> |
| 129 |
); |
| 130 |
}; |
| 131 |
|
| 132 |
export default IDViewsOverrideGroupsTable; |