| 1 |
import React, { useState } from "react"; |
| 2 |
// PatternFly |
| 3 |
import { PageSection, Tabs, Tab, TabTitleText } from "@patternfly/react-core"; |
| 4 |
// React Router DOM |
| 5 |
import { useNavigate } from "react-router"; |
| 6 |
import { URL_PREFIX } from "src/navigation/NavRoutes"; |
| 7 |
// Layouts |
| 8 |
import BreadCrumb, { BreadCrumbItem } from "src/components/layouts/BreadCrumb"; |
| 9 |
import TitleLayout from "src/components/layouts/TitleLayout"; |
| 10 |
import DataSpinner from "src/components/layouts/DataSpinner"; |
| 11 |
// Hooks |
| 12 |
import { useHostGroupSettings } from "src/hooks/useHostGroupSettingsData"; |
| 13 |
// Utils |
| 14 |
import { partialGroupToGroup } from "src/utils/hostGroupUtils"; |
| 15 |
// Redux |
| 16 |
import { useAppDispatch } from "src/store/hooks"; |
| 17 |
import { updateBreadCrumbPath } from "src/store/Global/routes-slice"; |
| 18 |
import { NotFound } from "src/components/errors/PageErrors"; |
| 19 |
import HostGroupsSettings from "./HostGroupsSettings"; |
| 20 |
import HostGroupsMembers from "./HostGroupsMembers"; |
| 21 |
import HostGroupsMemberOf from "./HostGroupsMemberOf"; |
| 22 |
import HostGroupsMemberManagers from "./HostGroupsMemberManagers"; |
| 23 |
import { CnParams, useSafeParams } from "src/utils/paramsUtils"; |
| 24 |
|
| 25 |
// eslint-disable-next-line react/prop-types |
| 26 |
const HostGroupsTabs = ({ section }) => { |
| 27 |
// Get location (React Router DOM) and get state data |
| 28 |
const { cn } = useSafeParams<CnParams>(["cn"]); |
| 29 |
const navigate = useNavigate(); |
| 30 |
const dispatch = useAppDispatch(); |
| 31 |
const hostGroupSettingsData = useHostGroupSettings(cn); |
| 32 |
const [breadcrumbItems, setBreadcrumbItems] = React.useState< |
| 33 |
BreadCrumbItem[] |
| 34 |
>([]); |
| 35 |
const [groupId, setGroupId] = useState(""); |
| 36 |
|
| 37 |
// Tab |
| 38 |
const [activeTabKey, setActiveTabKey] = useState(section); |
| 39 |
|
| 40 |
const handleTabClick = ( |
| 41 |
_event: React.MouseEvent<HTMLElement, MouseEvent>, |
| 42 |
tabIndex: number | string |
| 43 |
) => { |
| 44 |
if (tabIndex === "settings") { |
| 45 |
navigate("/host-groups/" + cn); |
| 46 |
} else if (tabIndex === "member") { |
| 47 |
navigate("/host-groups/" + cn + "/member_host"); |
| 48 |
} else if (tabIndex === "memberof") { |
| 49 |
navigate("/host-groups/" + cn + "/memberof_hostgroup"); |
| 50 |
} else if (tabIndex === "manager") { |
| 51 |
navigate("/host-groups/" + cn + "/manager_user"); |
| 52 |
} |
| 53 |
}; |
| 54 |
|
| 55 |
React.useEffect(() => { |
| 56 |
setGroupId(cn); |
| 57 |
// Update breadcrumb route |
| 58 |
const currentPath: BreadCrumbItem[] = [ |
| 59 |
{ |
| 60 |
name: "Host groups", |
| 61 |
url: URL_PREFIX + "/host-groups", |
| 62 |
}, |
| 63 |
{ |
| 64 |
name: cn, |
| 65 |
url: URL_PREFIX + "/host-groups/" + cn, |
| 66 |
isActive: true, |
| 67 |
}, |
| 68 |
]; |
| 69 |
setBreadcrumbItems(currentPath); |
| 70 |
setActiveTabKey("settings"); |
| 71 |
dispatch(updateBreadCrumbPath(currentPath)); |
| 72 |
}, [cn]); |
| 73 |
|
| 74 |
// Redirect to the settings page if the section is not defined |
| 75 |
React.useEffect(() => { |
| 76 |
if (!section) { |
| 77 |
navigate(URL_PREFIX + "/host-groups/" + groupId); |
| 78 |
} |
| 79 |
const section_string = section as string; |
| 80 |
if (section_string === "settings") { |
| 81 |
setActiveTabKey("settings"); |
| 82 |
} else if (section_string.startsWith("memberof_")) { |
| 83 |
setActiveTabKey("memberof"); |
| 84 |
} else if (section_string.startsWith("member_")) { |
| 85 |
setActiveTabKey("member"); |
| 86 |
} else if (section_string.startsWith("manager_")) { |
| 87 |
setActiveTabKey("manager"); |
| 88 |
} |
| 89 |
}, [section]); |
| 90 |
|
| 91 |
if ( |
| 92 |
hostGroupSettingsData.isLoading || |
| 93 |
hostGroupSettingsData.hostGroup.cn === undefined |
| 94 |
) { |
| 95 |
return <DataSpinner />; |
| 96 |
} |
| 97 |
|
| 98 |
// Show the 'NotFound' page if the hostGroup is not found |
| 99 |
if ( |
| 100 |
!hostGroupSettingsData.isLoading && |
| 101 |
Object.keys(hostGroupSettingsData.hostGroup).length === 0 |
| 102 |
) { |
| 103 |
return <NotFound />; |
| 104 |
} |
| 105 |
|
| 106 |
const hostgroup = partialGroupToGroup(hostGroupSettingsData.hostGroup); |
| 107 |
|
| 108 |
return ( |
| 109 |
<> |
| 110 |
<PageSection hasBodyWrapper={false}> |
| 111 |
<BreadCrumb |
| 112 |
breadcrumbItems={breadcrumbItems} |
| 113 |
className="pf-v6-u-mb-sm" |
| 114 |
/> |
| 115 |
<TitleLayout |
| 116 |
id={hostgroup.cn} |
| 117 |
preText="Host group:" |
| 118 |
text={hostgroup.cn} |
| 119 |
headingLevel="h1" |
| 120 |
/> |
| 121 |
</PageSection> |
| 122 |
<PageSection hasBodyWrapper={false} type="tabs" isFilled> |
| 123 |
<Tabs |
| 124 |
activeKey={activeTabKey} |
| 125 |
onSelect={handleTabClick} |
| 126 |
variant="secondary" |
| 127 |
isBox |
| 128 |
className="pf-v6-u-ml-lg" |
| 129 |
mountOnEnter |
| 130 |
unmountOnExit |
| 131 |
> |
| 132 |
<Tab |
| 133 |
eventKey={"settings"} |
| 134 |
name="settings-details" |
| 135 |
title={<TabTitleText>Settings</TabTitleText>} |
| 136 |
> |
| 137 |
<HostGroupsSettings |
| 138 |
hostGroup={hostGroupSettingsData.hostGroup} |
| 139 |
originalGroup={hostGroupSettingsData.originalGroup} |
| 140 |
metadata={hostGroupSettingsData.metadata} |
| 141 |
onGroupChange={hostGroupSettingsData.setHostGroup} |
| 142 |
isDataLoading={hostGroupSettingsData.isFetching} |
| 143 |
onRefresh={hostGroupSettingsData.refetch} |
| 144 |
isModified={hostGroupSettingsData.modified} |
| 145 |
onResetValues={hostGroupSettingsData.resetValues} |
| 146 |
modifiedValues={hostGroupSettingsData.modifiedValues} |
| 147 |
/> |
| 148 |
</Tab> |
| 149 |
<Tab |
| 150 |
eventKey={"member"} |
| 151 |
name="member-details" |
| 152 |
title={<TabTitleText>Members</TabTitleText>} |
| 153 |
> |
| 154 |
<HostGroupsMembers hostGroup={hostgroup} tabSection={section} /> |
| 155 |
</Tab> |
| 156 |
<Tab |
| 157 |
eventKey={"memberof"} |
| 158 |
name="memberof-details" |
| 159 |
title={<TabTitleText>Is a member of</TabTitleText>} |
| 160 |
> |
| 161 |
<HostGroupsMemberOf hostGroup={hostgroup} tabSection={section} /> |
| 162 |
</Tab> |
| 163 |
<Tab |
| 164 |
eventKey={"manager"} |
| 165 |
name="manager-details" |
| 166 |
title={<TabTitleText>Member managers</TabTitleText>} |
| 167 |
> |
| 168 |
<HostGroupsMemberManagers |
| 169 |
hostGroup={hostgroup} |
| 170 |
tabSection={section} |
| 171 |
/> |
| 172 |
</Tab> |
| 173 |
</Tabs> |
| 174 |
</PageSection> |
| 175 |
</> |
| 176 |
); |
| 177 |
}; |
| 178 |
|
| 179 |
export default HostGroupsTabs; |