| 1 |
import React 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 |
// Navigation |
| 7 |
import { URL_PREFIX } from "src/navigation/NavRoutes"; |
| 8 |
import { NotFound } from "src/components/errors/PageErrors"; |
| 9 |
// Hooks |
| 10 |
import { useSubidSettings } from "src/hooks/useSubidSettingsData"; |
| 11 |
// Components |
| 12 |
import TitleLayout from "src/components/layouts/TitleLayout"; |
| 13 |
import DataSpinner from "src/components/layouts/DataSpinner"; |
| 14 |
import BreadCrumb, { BreadCrumbItem } from "src/components/layouts/BreadCrumb"; |
| 15 |
import SubidSettings from "./SubidsSettings"; |
| 16 |
import { useSafeParams } from "src/utils/paramsUtils"; |
| 17 |
|
| 18 |
type SubIdParams = { |
| 19 |
ipauniqueid: string; |
| 20 |
}; |
| 21 |
|
| 22 |
// eslint-disable-next-line react/prop-types |
| 23 |
const SubIdTabs = ({ section }) => { |
| 24 |
const { ipauniqueid } = useSafeParams<SubIdParams>(["ipauniqueid"]); |
| 25 |
const navigate = useNavigate(); |
| 26 |
const pathname = "subordinate-ids"; |
| 27 |
|
| 28 |
const [breadcrumbItems, setBreadcrumbItems] = React.useState< |
| 29 |
BreadCrumbItem[] |
| 30 |
>([]); |
| 31 |
|
| 32 |
const [id, setId] = React.useState(""); |
| 33 |
|
| 34 |
// Data loaded from the API |
| 35 |
const subidSettingsData = useSubidSettings(ipauniqueid); |
| 36 |
|
| 37 |
// Tab |
| 38 |
const [activeTabKey, setActiveTabKey] = React.useState(section); |
| 39 |
|
| 40 |
const handleTabClick = ( |
| 41 |
_event: React.MouseEvent<HTMLElement, MouseEvent>, |
| 42 |
tabIndex: number | string |
| 43 |
) => { |
| 44 |
setActiveTabKey(tabIndex as string); |
| 45 |
|
| 46 |
if (tabIndex === "settings") { |
| 47 |
navigate("/" + pathname + "/" + id); |
| 48 |
} |
| 49 |
}; |
| 50 |
|
| 51 |
React.useEffect(() => { |
| 52 |
setId(ipauniqueid); |
| 53 |
// Update breadcrumb route |
| 54 |
const currentPath: BreadCrumbItem[] = [ |
| 55 |
{ |
| 56 |
name: "Subordinate IDs", |
| 57 |
url: URL_PREFIX + "/" + pathname, |
| 58 |
}, |
| 59 |
{ |
| 60 |
name: ipauniqueid, |
| 61 |
url: URL_PREFIX + "/" + pathname + "/" + ipauniqueid, |
| 62 |
isActive: true, |
| 63 |
}, |
| 64 |
]; |
| 65 |
setBreadcrumbItems(currentPath); |
| 66 |
setActiveTabKey("settings"); |
| 67 |
}, [ipauniqueid]); |
| 68 |
|
| 69 |
if (subidSettingsData.isLoading || !subidSettingsData.subid) { |
| 70 |
return <DataSpinner />; |
| 71 |
} |
| 72 |
|
| 73 |
// Show the 'NotFound' page if the host is not found |
| 74 |
if ( |
| 75 |
!subidSettingsData.isLoading && |
| 76 |
Object.keys(subidSettingsData.subid).length === 0 |
| 77 |
) { |
| 78 |
return <NotFound />; |
| 79 |
} |
| 80 |
|
| 81 |
// Return component |
| 82 |
return ( |
| 83 |
<> |
| 84 |
<PageSection hasBodyWrapper={false}> |
| 85 |
<BreadCrumb |
| 86 |
className="pf-v6-u-mb-sm" |
| 87 |
breadcrumbItems={breadcrumbItems} |
| 88 |
/> |
| 89 |
<TitleLayout |
| 90 |
id={id} |
| 91 |
preText="Subordinate IDs:" |
| 92 |
text={id} |
| 93 |
headingLevel="h1" |
| 94 |
/> |
| 95 |
</PageSection> |
| 96 |
<PageSection hasBodyWrapper={false} type="tabs" isFilled> |
| 97 |
<Tabs |
| 98 |
activeKey={activeTabKey} |
| 99 |
onSelect={handleTabClick} |
| 100 |
variant="secondary" |
| 101 |
isBox |
| 102 |
className="pf-v6-u-ml-lg" |
| 103 |
mountOnEnter |
| 104 |
unmountOnExit |
| 105 |
> |
| 106 |
<Tab |
| 107 |
eventKey={"settings"} |
| 108 |
name="settings-details" |
| 109 |
title={<TabTitleText>Settings</TabTitleText>} |
| 110 |
> |
| 111 |
<SubidSettings |
| 112 |
subId={subidSettingsData.subid} |
| 113 |
originalSubId={subidSettingsData.originalSubid} |
| 114 |
metadata={subidSettingsData.metadata} |
| 115 |
onSubIdChange={subidSettingsData.setSubid} |
| 116 |
onRefresh={subidSettingsData.refetch} |
| 117 |
isModified={subidSettingsData.modified} |
| 118 |
isDataLoading={subidSettingsData.isLoading} |
| 119 |
modifiedValues={subidSettingsData.modifiedValues} |
| 120 |
onResetValues={subidSettingsData.resetValues} |
| 121 |
/> |
| 122 |
</Tab> |
| 123 |
</Tabs> |
| 124 |
</PageSection> |
| 125 |
</> |
| 126 |
); |
| 127 |
}; |
| 128 |
|
| 129 |
export default SubIdTabs; |