Debian Codegraph @

install/freeipa-webui/src/pages/IDViews/IDViewsSettings.tsx

raw
1 import React, { useState } from "react";
2 // PatternFly
3 import { Flex, Form, FormGroup, Icon, Tooltip } from "@patternfly/react-core";
4 // Forms
5 import IpaTextArea from "../../components/Form/IpaTextArea";
6 import IpaTextInput from "src/components/Form/IpaTextInput";
7 // Layouts
8 import TitleLayout from "src/components/layouts/TitleLayout";
9 import SecondaryButton from "src/components/layouts/SecondaryButton";
10 import TabLayout from "src/components/layouts/TabLayout";
11 // Utils
12 import { asRecord } from "../../utils/hostUtils";
13 // Redux
14 import { useAppDispatch } from "src/store/hooks";
15 // Hooks
16 import { addAlert } from "src/store/Global/alerts-slice";
17 import useUpdateRoute from "src/hooks/useUpdateRoute";
18 // Icons
19 import { HelpIcon } from "@patternfly/react-icons";
20 // Data types
21 import { IDView, Metadata } from "../../utils/datatypes/globalDataTypes";
22 // RPC
23 import { ErrorResult } from "src/services/rpc";
24 import { useSaveIDViewMutation } from "src/services/rpcIDViews";
25
26 interface PropsToIDViewSettings {
27 idView: Partial<IDView>;
28 originalIDView: Partial<IDView>;
29 metadata: Metadata;
30 onIDViewChange: (hostGroup: Partial<IDView>) => void;
31 onRefresh: () => void;
32 isModified: boolean;
33 isDataLoading?: boolean;
34 modifiedValues: () => Partial<IDView>;
35 onResetValues: () => void;
36 }
37
38 // WIP - placeholder until settings page is actually fully implemented
39
40 const IDViewSettings = (props: PropsToIDViewSettings) => {
41 const dispatch = useAppDispatch();
42
43 // API
44 const [saveView] = useSaveIDViewMutation();
45
46 // Update current route data to Redux and highlight the current page in the Nav bar
47 useUpdateRoute({ pathname: "id-view", noBreadcrumb: true });
48
49 const [isSaving, setSaving] = useState(false);
50
51 // Get 'ipaObject' and 'recordOnChange' to use in 'IpaTextInput'
52 const { ipaObject, recordOnChange } = asRecord(
53 props.idView,
54 props.onIDViewChange
55 );
56
57 // 'Save' handler method
58 const onSave = () => {
59 const modifiedValues = props.modifiedValues();
60 modifiedValues.cn = props.idView.cn;
61 setSaving(true);
62
63 saveView(modifiedValues).then((response) => {
64 if ("data" in response) {
65 if (response.data?.result) {
66 // Show toast notification: success
67 dispatch(
68 addAlert({
69 name: "save-success",
70 title: "ID view modified",
71 variant: "success",
72 })
73 );
74 } else if (response.data?.error) {
75 // Show toast notification: error
76 const errorMessage = response.data.error as ErrorResult;
77 dispatch(
78 addAlert({
79 name: "save-error",
80 title: errorMessage.message,
81 variant: "danger",
82 })
83 );
84 }
85 // Reset values. Disable 'revert' and 'save' buttons
86 props.onResetValues();
87 setSaving(false);
88 }
89 });
90 };
91
92 // 'Revert' handler method
93 const onRevert = () => {
94 props.onIDViewChange(props.originalIDView);
95 dispatch(
96 addAlert({
97 name: "revert-success",
98 title: "ID view data reverted",
99 variant: "success",
100 })
101 );
102 };
103
104 // Toolbar
105 const toolbarFields = [
106 {
107 key: 0,
108 element: (
109 <SecondaryButton
110 dataCy="id-views-tab-settings-button-refresh"
111 onClickHandler={props.onRefresh}
112 >
113 Refresh
114 </SecondaryButton>
115 ),
116 },
117 {
118 key: 1,
119 element: (
120 <SecondaryButton
121 dataCy="id-views-tab-settings-button-revert"
122 isDisabled={!props.isModified}
123 onClickHandler={onRevert}
124 >
125 Revert
126 </SecondaryButton>
127 ),
128 },
129 {
130 key: 2,
131 element: (
132 <SecondaryButton
133 dataCy="id-views-tab-settings-button-save"
134 isDisabled={!props.isModified || isSaving}
135 onClickHandler={onSave}
136 isLoading={isSaving}
137 spinnerAriaValueText="Saving"
138 spinnerAriaLabelledBy="Saving"
139 spinnerAriaLabel="Saving"
140 >
141 {isSaving ? "Saving" : "Save"}
142 </SecondaryButton>
143 ),
144 },
145 ];
146
147 // Render component
148 return (
149 <TabLayout id="settings-page" toolbarItems={toolbarFields}>
150 <Flex direction={{ default: "column" }} flex={{ default: "flex_1" }}>
151 <TitleLayout
152 key={0}
153 headingLevel="h2"
154 id="id-view-settings"
155 text="ID view settings"
156 />
157 <Form
158 className="pf-v6-u-mt-sm pf-v6-u-mb-lg pf-v6-u-mr-md"
159 isHorizontal
160 >
161 <FormGroup
162 label="Domain resolution order"
163 fieldId="ipadomainresolutionorder"
164 labelHelp={
165 <Tooltip
166 content={
167 <div>
168 Colon-separated list of domains used for short name
169 qualification
170 </div>
171 }
172 >
173 <Icon iconSize="sm">
174 <HelpIcon />
175 </Icon>
176 </Tooltip>
177 }
178 >
179 <IpaTextInput
180 dataCy="id-views-tab-settings-textbox-ipadomainresolutionorder"
181 name="ipadomainresolutionorder"
182 ariaLabel={"Domain resolution order"}
183 ipaObject={ipaObject}
184 onChange={recordOnChange}
185 objectName="idview"
186 metadata={props.metadata}
187 />
188 </FormGroup>
189 <FormGroup label="Description" fieldId="description">
190 <IpaTextArea
191 dataCy="id-views-tab-settings-textbox-description"
192 name="description"
193 ipaObject={ipaObject}
194 onChange={recordOnChange}
195 objectName="idview"
196 metadata={props.metadata}
197 />
198 </FormGroup>
199 </Form>
200 </Flex>
201 </TabLayout>
202 );
203 };
204
205 export default IDViewSettings;