Debian Codegraph @

install/freeipa-webui/src/pages/Trusts/TrustsSettings.tsx

raw
1 import React from "react";
2 // PatternFly
3 import {
4 Flex,
5 FlexItem,
6 Form,
7 FormGroup,
8 JumpLinks,
9 JumpLinksItem,
10 Sidebar,
11 SidebarContent,
12 SidebarPanel,
13 } from "@patternfly/react-core";
14 // Data types
15 import { Trust, Metadata } from "src/utils/datatypes/globalDataTypes";
16 // Hooks
17 import useUpdateRoute from "src/hooks/useUpdateRoute";
18 // Utils
19 import { asRecord } from "src/utils/trustsUtils";
20 // RPC
21 import { TrustModPayload, useTrustModMutation } from "src/services/rpcTrusts";
22 // Components
23 import IpaTextInput from "src/components/Form/IpaTextInput/IpaTextInput";
24 import TabLayout from "src/components/layouts/TabLayout";
25 import SecondaryButton from "src/components/layouts/SecondaryButton";
26 import HelpTextWithIconLayout from "src/components/layouts/HelpTextWithIconLayout";
27 import IpaTextboxList from "src/components/Form/IpaTextboxList";
28 import TitleLayout from "src/components/layouts/TitleLayout";
29 // Redux
30 import { addAlert } from "src/store/Global/alerts-slice";
31 import { useAppDispatch } from "src/store/hooks";
32
33 interface TrustsSettingsProps {
34 trust: Partial<Trust>;
35 originalTrust: Partial<Trust>;
36 metadata: Metadata;
37 onTrustChange: (trust: Partial<Trust>) => void;
38 onRefresh: () => void;
39 isModified: boolean;
40 isDataLoading: boolean;
41 modifiedValues: () => Partial<Trust>;
42 onResetValues: () => void;
43 pathname: string;
44 }
45
46 const TrustsSettings = (props: TrustsSettingsProps) => {
47 // Alerts to show in the UI
48 const dispatch = useAppDispatch();
49
50 // Update current route data to Redux and highlight the current page in the Nav bar
51 useUpdateRoute({ pathname: props.pathname });
52
53 // States
54 const [isDataLoading, setIsDataLoading] = React.useState(false);
55
56 // API calls
57 const [saveTrust] = useTrustModMutation();
58
59 // 'Revert' handler method
60 const onRevert = () => {
61 props.onTrustChange(props.originalTrust);
62 props.onRefresh();
63 dispatch(
64 addAlert({
65 name: "revert-success",
66 title: "Trust data reverted",
67 variant: "success",
68 })
69 );
70 };
71
72 // Helper method to build the payload based on values
73 const buildPayload = (
74 modifiedValues: Partial<Trust>,
75 keyArray: string[]
76 ): TrustModPayload => {
77 const payload: TrustModPayload = { cn: props.trust.cn || "" };
78
79 keyArray.forEach((key) => {
80 payload[key] = modifiedValues[key];
81 });
82 return payload;
83 };
84
85 // Get 'ipaObject' and 'recordOnChange' to use in 'IpaTextInput'
86 const { ipaObject, recordOnChange } = asRecord(
87 props.trust,
88 props.onTrustChange
89 );
90
91 // 'Save' handler method
92 const onSave = () => {
93 setIsDataLoading(true);
94 const modifiedValues = props.modifiedValues();
95
96 const payload = buildPayload(modifiedValues, [
97 "ipantadditionalsuffixes",
98 "ipantsidblacklistincoming",
99 "ipantsidblacklistoutgoing",
100 ]);
101
102 saveTrust(payload)
103 .then((response) => {
104 if ("data" in response) {
105 const data = response.data;
106 if (data?.error) {
107 dispatch(
108 addAlert({
109 name: "error",
110 title: (data.error as Error).message,
111 variant: "danger",
112 })
113 );
114 }
115 if (data?.result) {
116 props.onTrustChange(data.result.result);
117 dispatch(
118 addAlert({
119 name: "save-success",
120 title: "Trust data updated",
121 variant: "success",
122 })
123 );
124 props.onRefresh();
125 }
126 }
127 })
128 .finally(() => {
129 setIsDataLoading(false);
130 });
131 };
132
133 // Toolbar
134 const toolbarFields = [
135 {
136 key: 0,
137 element: (
138 <SecondaryButton
139 dataCy="trusts-tab-settings-button-refresh"
140 onClickHandler={props.onRefresh}
141 >
142 Refresh
143 </SecondaryButton>
144 ),
145 },
146 {
147 key: 1,
148 element: (
149 <SecondaryButton
150 dataCy="trusts-tab-settings-button-revert"
151 isDisabled={!props.isModified || isDataLoading}
152 onClickHandler={onRevert}
153 >
154 Revert
155 </SecondaryButton>
156 ),
157 },
158 {
159 key: 2,
160 element: (
161 <SecondaryButton
162 dataCy="trusts-tab-settings-button-save"
163 isDisabled={!props.isModified || isDataLoading}
164 onClickHandler={onSave}
165 >
166 Save
167 </SecondaryButton>
168 ),
169 },
170 ];
171
172 // Return component
173 return (
174 <TabLayout
175 id="settings-page"
176 toolbarItems={toolbarFields}
177 dataCy="trusts-settings"
178 >
179 <Sidebar isPanelRight>
180 <SidebarPanel variant="sticky">
181 <HelpTextWithIconLayout textContent="Help" />
182 <JumpLinks
183 isVertical
184 label="Jump to section"
185 scrollableSelector="#settings-page"
186 offset={220} // for masthead
187 expandable={{ default: "expandable", md: "nonExpandable" }}
188 >
189 <JumpLinksItem key={0} href="#trusts-settings">
190 Trusts settings
191 </JumpLinksItem>
192 <JumpLinksItem key={1} href="#alternative-upn-suffixes">
193 Alternative UPN suffixes
194 </JumpLinksItem>
195 <JumpLinksItem key={2} href="#sid-blocklists">
196 SID blocklists
197 </JumpLinksItem>
198 </JumpLinks>
199 </SidebarPanel>
200 <SidebarContent className="pf-v6-u-mr-xl">
201 <Flex direction={{ default: "column" }} flex={{ default: "flex_1" }}>
202 <FlexItem flex={{ default: "flex_1" }}>
203 <TitleLayout
204 key={0}
205 headingLevel="h2"
206 id="trusts-settings"
207 text="Trusts settings"
208 />
209 <Form isHorizontal>
210 <FormGroup label="Realm Name" fieldId="cn" role="group">
211 <IpaTextInput
212 dataCy="trusts-tab-settings-input-realm-name"
213 name="cn"
214 objectName="trust"
215 metadata={props.metadata}
216 ipaObject={ipaObject}
217 onChange={recordOnChange}
218 />
219 </FormGroup>
220 <FormGroup
221 label="Domain NetBIOS name"
222 fieldId="ipandomainnetbiosname"
223 role="group"
224 >
225 <IpaTextInput
226 dataCy="trusts-tab-settings-input-domain-netbios-name"
227 name="ipandomainnetbiosname"
228 objectName="trust"
229 metadata={props.metadata}
230 ipaObject={ipaObject}
231 onChange={recordOnChange}
232 />
233 </FormGroup>
234 <FormGroup label="Trust Type" fieldId="trusttype" role="group">
235 <IpaTextInput
236 dataCy="trusts-tab-settings-input-trust-type"
237 name="trusttype"
238 objectName="trust"
239 metadata={props.metadata}
240 ipaObject={ipaObject}
241 onChange={recordOnChange}
242 />
243 </FormGroup>
244 </Form>
245 </FlexItem>
246 <FlexItem flex={{ default: "flex_1" }}>
247 <TitleLayout
248 key={1}
249 headingLevel="h2"
250 id="alternative-upn-suffixes"
251 text="Alternative UPN suffixes"
252 />
253 <Form isHorizontal>
254 <FormGroup
255 label="Alternative UPN suffixes"
256 fieldId="ipantadditionalsuffixes"
257 role="group"
258 >
259 <IpaTextboxList
260 dataCy="trusts-tab-settings-textbox-alternative-upn-suffixes"
261 name="ipantadditionalsuffixes"
262 ipaObject={ipaObject}
263 setIpaObject={recordOnChange}
264 ariaLabel="Alternative UPN suffixes list"
265 />
266 </FormGroup>
267 <TitleLayout
268 key={2}
269 headingLevel="h2"
270 id="sid-blocklists"
271 text="SID blocklists"
272 />
273 <FormGroup
274 label="SID blocklists incoming"
275 fieldId="ipantsidblocklistincoming"
276 role="group"
277 >
278 <IpaTextboxList
279 dataCy="trusts-tab-settings-textbox-sid-blocklists"
280 name="ipantsidblocklistincoming"
281 ipaObject={ipaObject}
282 setIpaObject={recordOnChange}
283 ariaLabel="SID blocklists incoming list"
284 />
285 </FormGroup>
286 <FormGroup
287 label="SID blocklists outgoing"
288 fieldId="ipantsidblocklistoutgoing"
289 role="group"
290 >
291 <IpaTextboxList
292 dataCy="trusts-tab-settings-textbox-sid-blocklists-outgoing"
293 name="ipantsidblocklistoutgoing"
294 ipaObject={ipaObject}
295 setIpaObject={recordOnChange}
296 ariaLabel="SID blocklists outgoing list"
297 />
298 </FormGroup>
299 </Form>
300 </FlexItem>
301 </Flex>
302 </SidebarContent>
303 </Sidebar>
304 </TabLayout>
305 );
306 };
307
308 export default TrustsSettings;