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