Debian Codegraph @

install/freeipa-webui/src/pages/HostGroups/HostGroupsSettings.tsx

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