| 1 |
import React from "react"; |
| 2 |
// PatternFly |
| 3 |
import { Badge, Tab, Tabs, TabTitleText } from "@patternfly/react-core"; |
| 4 |
// Data types |
| 5 |
import { Netgroup } from "src/utils/datatypes/globalDataTypes"; |
| 6 |
// Navigation |
| 7 |
import { useNavigate } from "react-router"; |
| 8 |
// Layout |
| 9 |
import TabLayout from "src/components/layouts/TabLayout"; |
| 10 |
// Hooks |
| 11 |
import useUpdateRoute from "src/hooks/useUpdateRoute"; |
| 12 |
// RPC |
| 13 |
import { useGetNetgroupByIdQuery } from "src/services/rpcNetgroups"; |
| 14 |
// 'Is a member of' sections |
| 15 |
import MemberOfNetgroups from "src/components/MemberOf/MemberOfNetgroups"; |
| 16 |
|
| 17 |
interface PropsToMemberOf { |
| 18 |
netgroup: Netgroup; |
| 19 |
tabSection: string; |
| 20 |
} |
| 21 |
|
| 22 |
const NetgroupsMemberOf = (props: PropsToMemberOf) => { |
| 23 |
const navigate = useNavigate(); |
| 24 |
|
| 25 |
// Netgroup's full data |
| 26 |
const netgroupQuery = useGetNetgroupByIdQuery(props.netgroup.cn); |
| 27 |
const netgroupData = netgroupQuery.data || {}; |
| 28 |
|
| 29 |
const [netgroup, setNetgroup] = React.useState<Partial<Netgroup>>({}); |
| 30 |
|
| 31 |
React.useEffect(() => { |
| 32 |
if (!netgroupQuery.isFetching && netgroupData) { |
| 33 |
setNetgroup({ ...netgroupData }); |
| 34 |
} |
| 35 |
}, [netgroupData, netgroupQuery.isFetching]); |
| 36 |
|
| 37 |
const onRefreshData = () => { |
| 38 |
netgroupQuery.refetch(); |
| 39 |
}; |
| 40 |
|
| 41 |
// Update current route data to Redux and highlight the current page in the Nav bar |
| 42 |
useUpdateRoute({ pathname: "netgroups", noBreadcrumb: true }); |
| 43 |
|
| 44 |
const handleTabClick = ( |
| 45 |
_event: React.MouseEvent<HTMLElement, MouseEvent>, |
| 46 |
tabIndex: number | string |
| 47 |
) => { |
| 48 |
navigate("/netgroups/" + props.netgroup.cn + "/" + tabIndex); |
| 49 |
}; |
| 50 |
|
| 51 |
const [netgroupCount, setNetgroupCount] = React.useState(0); |
| 52 |
|
| 53 |
React.useEffect(() => { |
| 54 |
setNetgroupCount( |
| 55 |
netgroup && netgroup.memberof_netgroup |
| 56 |
? netgroup.memberof_netgroup.length |
| 57 |
: 0 |
| 58 |
); |
| 59 |
}, [netgroup]); |
| 60 |
|
| 61 |
// Render component |
| 62 |
return ( |
| 63 |
<div style={{ height: `var(--subsettings-calc)` }}> |
| 64 |
<TabLayout id="memberof"> |
| 65 |
<Tabs |
| 66 |
activeKey={props.tabSection} |
| 67 |
onSelect={handleTabClick} |
| 68 |
isBox={false} |
| 69 |
mountOnEnter |
| 70 |
unmountOnExit |
| 71 |
> |
| 72 |
<Tab |
| 73 |
eventKey={"memberof_netgroup"} |
| 74 |
name="memberof_netgroup" |
| 75 |
title={ |
| 76 |
<TabTitleText> |
| 77 |
Netgroups{" "} |
| 78 |
<Badge key={0} isRead> |
| 79 |
{netgroupCount} |
| 80 |
</Badge> |
| 81 |
</TabTitleText> |
| 82 |
} |
| 83 |
> |
| 84 |
<MemberOfNetgroups |
| 85 |
entity={netgroup} |
| 86 |
id={netgroup.cn as string} |
| 87 |
from={"netgroups"} |
| 88 |
isDataLoading={netgroupQuery.isFetching} |
| 89 |
onRefreshData={onRefreshData} |
| 90 |
/> |
| 91 |
</Tab> |
| 92 |
</Tabs> |
| 93 |
</TabLayout> |
| 94 |
</div> |
| 95 |
); |
| 96 |
}; |
| 97 |
|
| 98 |
export default NetgroupsMemberOf; |