| 1 |
import React from "react"; |
| 2 |
// PatternFly |
| 3 |
import { |
| 4 |
Flex, |
| 5 |
FlexItem, |
| 6 |
Form, |
| 7 |
FormGroup, |
| 8 |
Sidebar, |
| 9 |
SidebarContent, |
| 10 |
SidebarPanel, |
| 11 |
} from "@patternfly/react-core"; |
| 12 |
// Data types |
| 13 |
import { SubId, Metadata } from "src/utils/datatypes/globalDataTypes"; |
| 14 |
// Hooks |
| 15 |
import useUpdateRoute from "src/hooks/useUpdateRoute"; |
| 16 |
import { addAlert } from "src/store/Global/alerts-slice"; |
| 17 |
// Redux |
| 18 |
import { useAppDispatch } from "src/store/hooks"; |
| 19 |
// Utils |
| 20 |
import { asRecord } from "src/utils/subIdUtils"; |
| 21 |
// Icons |
| 22 |
import { OutlinedQuestionCircleIcon } from "@patternfly/react-icons"; |
| 23 |
// RPC |
| 24 |
import { SubidModPayload, useSubidModMutation } from "src/services/rpcSubIds"; |
| 25 |
// Components |
| 26 |
import IpaTextInput from "src/components/Form/IpaTextInput"; |
| 27 |
import TabLayout from "src/components/layouts/TabLayout"; |
| 28 |
import SecondaryButton from "src/components/layouts/SecondaryButton"; |
| 29 |
import HelpTextWithIconLayout from "src/components/layouts/HelpTextWithIconLayout"; |
| 30 |
import IpaTextContent from "src/components/Form/IpaTextContent"; |
| 31 |
|
| 32 |
interface PropsToSubidSettings { |
| 33 |
subId: Partial<SubId>; |
| 34 |
originalSubId: Partial<SubId>; |
| 35 |
metadata: Metadata; |
| 36 |
onSubIdChange: (subid: Partial<SubId>) => void; |
| 37 |
onRefresh: () => void; |
| 38 |
isModified: boolean; |
| 39 |
isDataLoading?: boolean; |
| 40 |
modifiedValues: () => Partial<SubId>; |
| 41 |
onResetValues: () => void; |
| 42 |
} |
| 43 |
|
| 44 |
const SubidSettings = (props: PropsToSubidSettings) => { |
| 45 |
const dispatch = useAppDispatch(); |
| 46 |
|
| 47 |
// Update current route data to Redux and highlight the current page in the Nav bar |
| 48 |
useUpdateRoute({ pathname: "subordinate-ids" }); |
| 49 |
|
| 50 |
// States |
| 51 |
const [isDataLoading, setIsDataLoading] = React.useState(false); |
| 52 |
|
| 53 |
// Get 'ipaObject' and 'recordOnChange' to use in 'IpaTextInput' |
| 54 |
const { ipaObject, recordOnChange } = asRecord( |
| 55 |
props.subId, |
| 56 |
props.onSubIdChange |
| 57 |
); |
| 58 |
|
| 59 |
// API calls |
| 60 |
const [subidMod] = useSubidModMutation(); |
| 61 |
|
| 62 |
// 'Revert' handler method |
| 63 |
const onRevert = () => { |
| 64 |
props.onSubIdChange(props.originalSubId); |
| 65 |
props.onRefresh(); |
| 66 |
dispatch( |
| 67 |
addAlert({ |
| 68 |
name: "revert-success", |
| 69 |
title: "Subordinate ID data reverted", |
| 70 |
variant: "success", |
| 71 |
}) |
| 72 |
); |
| 73 |
}; |
| 74 |
|
| 75 |
// on Save handler method |
| 76 |
const onSave = () => { |
| 77 |
setIsDataLoading(true); |
| 78 |
const modifiedValues = props.modifiedValues(); |
| 79 |
|
| 80 |
const payload: SubidModPayload = { |
| 81 |
ipauniqueid: props.subId.ipauniqueid?.toString() as string, |
| 82 |
description: modifiedValues.description?.toString() as string, |
| 83 |
}; |
| 84 |
|
| 85 |
subidMod(payload).then((response) => { |
| 86 |
if ("data" in response) { |
| 87 |
const data = response.data; |
| 88 |
if (data?.error) { |
| 89 |
dispatch( |
| 90 |
addAlert({ |
| 91 |
name: "error", |
| 92 |
title: (data.error as Error).message, |
| 93 |
variant: "danger", |
| 94 |
}) |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
if (data?.result) { |
| 99 |
props.onSubIdChange(data.result.result); |
| 100 |
dispatch( |
| 101 |
addAlert({ |
| 102 |
name: "success", |
| 103 |
title: "Subordinate ID " + props.subId.ipauniqueid + " updated", |
| 104 |
variant: "success", |
| 105 |
}) |
| 106 |
); |
| 107 |
// Reset values. Disable 'revert' and 'save' buttons |
| 108 |
props.onResetValues(); |
| 109 |
} |
| 110 |
} |
| 111 |
setIsDataLoading(false); |
| 112 |
}); |
| 113 |
}; |
| 114 |
|
| 115 |
// Toolbar |
| 116 |
const toolbarFields = [ |
| 117 |
{ |
| 118 |
key: 0, |
| 119 |
element: ( |
| 120 |
<SecondaryButton |
| 121 |
dataCy="subids-tab-settings-button-refresh" |
| 122 |
onClickHandler={props.onRefresh} |
| 123 |
> |
| 124 |
Refresh |
| 125 |
</SecondaryButton> |
| 126 |
), |
| 127 |
}, |
| 128 |
{ |
| 129 |
key: 1, |
| 130 |
element: ( |
| 131 |
<SecondaryButton |
| 132 |
dataCy="subids-tab-settings-button-revert" |
| 133 |
isDisabled={!props.isModified || isDataLoading} |
| 134 |
onClickHandler={onRevert} |
| 135 |
> |
| 136 |
Revert |
| 137 |
</SecondaryButton> |
| 138 |
), |
| 139 |
}, |
| 140 |
{ |
| 141 |
key: 2, |
| 142 |
element: ( |
| 143 |
<SecondaryButton |
| 144 |
dataCy="subids-tab-settings-button-save" |
| 145 |
isDisabled={!props.isModified || isDataLoading} |
| 146 |
onClickHandler={onSave} |
| 147 |
> |
| 148 |
Save |
| 149 |
</SecondaryButton> |
| 150 |
), |
| 151 |
}, |
| 152 |
]; |
| 153 |
|
| 154 |
return ( |
| 155 |
<> |
| 156 |
<TabLayout id="settings-page" toolbarItems={toolbarFields}> |
| 157 |
<Sidebar isPanelRight> |
| 158 |
<SidebarPanel variant="sticky"> |
| 159 |
<HelpTextWithIconLayout |
| 160 |
textContent="Help" |
| 161 |
icon={ |
| 162 |
<OutlinedQuestionCircleIcon className="pf-v6-u-primary-color-100 pf-v6-u-mr-sm" /> |
| 163 |
} |
| 164 |
/> |
| 165 |
</SidebarPanel> |
| 166 |
<SidebarContent className="pf-v6-u-mr-xl"> |
| 167 |
<Flex direction={{ default: "column", lg: "row" }}> |
| 168 |
<FlexItem flex={{ default: "flex_1" }}> |
| 169 |
<Form className="pf-v6-u-mb-lg"> |
| 170 |
<FormGroup label="Unique ID" fieldId="ipauniqueid"> |
| 171 |
<IpaTextInput |
| 172 |
dataCy="subids-tab-settings-textbox-unique-id" |
| 173 |
name={"ipauniqueid"} |
| 174 |
ariaLabel={"Unique ID"} |
| 175 |
ipaObject={ipaObject} |
| 176 |
onChange={recordOnChange} |
| 177 |
objectName="subid" |
| 178 |
metadata={props.metadata} |
| 179 |
/> |
| 180 |
</FormGroup> |
| 181 |
<FormGroup label="Description" fieldId="description"> |
| 182 |
<IpaTextInput |
| 183 |
dataCy="subids-tab-settings-textbox-description" |
| 184 |
name={"description"} |
| 185 |
ariaLabel={"Description"} |
| 186 |
ipaObject={ipaObject} |
| 187 |
onChange={recordOnChange} |
| 188 |
objectName="subid" |
| 189 |
metadata={props.metadata} |
| 190 |
/> |
| 191 |
</FormGroup> |
| 192 |
<FormGroup label="Owner" fieldId="ipaowner" role="group"> |
| 193 |
<IpaTextContent |
| 194 |
dataCy="subids-tab-settings-textbox-owner" |
| 195 |
name={"ipaowner"} |
| 196 |
ariaLabel={"Owner"} |
| 197 |
ipaObject={ipaObject} |
| 198 |
onChange={recordOnChange} |
| 199 |
objectName="subid" |
| 200 |
metadata={props.metadata} |
| 201 |
linkTo={ |
| 202 |
"/active-users/" + props.subId.ipaowner?.toString() |
| 203 |
} |
| 204 |
/> |
| 205 |
</FormGroup> |
| 206 |
<FormGroup |
| 207 |
label="SubGID range start" |
| 208 |
fieldId="ipasubgidnumber" |
| 209 |
> |
| 210 |
<IpaTextInput |
| 211 |
dataCy="subids-tab-settings-textbox-subgid-range-start" |
| 212 |
name={"ipasubgidnumber"} |
| 213 |
ariaLabel={"SubGID range start"} |
| 214 |
ipaObject={ipaObject} |
| 215 |
onChange={recordOnChange} |
| 216 |
objectName="subid" |
| 217 |
metadata={props.metadata} |
| 218 |
/> |
| 219 |
</FormGroup> |
| 220 |
<FormGroup label="SubGID range size" fieldId="ipasubgidcount"> |
| 221 |
<IpaTextInput |
| 222 |
dataCy="subids-tab-settings-textbox-subgid-range-size" |
| 223 |
name={"ipasubgidcount"} |
| 224 |
ariaLabel={"SubGID range size"} |
| 225 |
ipaObject={ipaObject} |
| 226 |
onChange={recordOnChange} |
| 227 |
objectName="subid" |
| 228 |
metadata={props.metadata} |
| 229 |
/> |
| 230 |
</FormGroup> |
| 231 |
<FormGroup |
| 232 |
label="SubUID range start" |
| 233 |
fieldId="ipasubuidnumber" |
| 234 |
> |
| 235 |
<IpaTextInput |
| 236 |
dataCy="subids-tab-settings-textbox-subuid-range-start" |
| 237 |
name={"ipasubuidnumber"} |
| 238 |
ariaLabel={"SubUID range start"} |
| 239 |
ipaObject={ipaObject} |
| 240 |
onChange={recordOnChange} |
| 241 |
objectName="subid" |
| 242 |
metadata={props.metadata} |
| 243 |
/> |
| 244 |
</FormGroup> |
| 245 |
<FormGroup label="SubUID range size" fieldId="ipasubuidcount"> |
| 246 |
<IpaTextInput |
| 247 |
dataCy="subids-tab-settings-textbox-subuid-range-size" |
| 248 |
name={"ipasubuidcount"} |
| 249 |
ariaLabel={"SubUID range size"} |
| 250 |
ipaObject={ipaObject} |
| 251 |
onChange={recordOnChange} |
| 252 |
objectName="subid" |
| 253 |
metadata={props.metadata} |
| 254 |
/> |
| 255 |
</FormGroup> |
| 256 |
</Form> |
| 257 |
</FlexItem> |
| 258 |
</Flex> |
| 259 |
</SidebarContent> |
| 260 |
</Sidebar> |
| 261 |
</TabLayout> |
| 262 |
</> |
| 263 |
); |
| 264 |
}; |
| 265 |
|
| 266 |
export default SubidSettings; |