Debian Codegraph @

install/freeipa-webui/src/pages/HBACServices/HBACServicesSettings.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 "src/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 { HBACService, Metadata } from "../../utils/datatypes/globalDataTypes";
19 // RPC
20 import { ErrorResult } from "src/services/rpc";
21 import { useSaveHbacServiceMutation } from "src/services/rpcHBACServices";
22
23 interface PropsToSettings {
24 service: Partial<HBACService>;
25 originalService: Partial<HBACService>;
26 metadata: Metadata;
27 onServiceChange: (service: Partial<HBACService>) => void;
28 onRefresh: () => void;
29 isModified: boolean;
30 isDataLoading?: boolean;
31 modifiedValues: () => Partial<HBACService>;
32 onResetValues: () => void;
33 }
34
35 const HBACServicesSettings = (props: PropsToSettings) => {
36 const dispatch = useAppDispatch();
37
38 // API
39 const [saveService] = useSaveHbacServiceMutation();
40
41 // Update current route data to Redux and highlight the current page in the Nav bar
42 useUpdateRoute({ pathname: "hbac-services", noBreadcrumb: true });
43
44 // Get 'ipaObject' and 'recordOnChange' to use in 'IpaTextInput'
45 const { ipaObject, recordOnChange } = asRecord(
46 props.service,
47 props.onServiceChange
48 );
49
50 const [isSaving, setSaving] = useState(false);
51
52 // 'Save' handler method
53 const onSave = () => {
54 const modifiedValues = props.modifiedValues();
55 modifiedValues.cn = props.service.cn;
56 setSaving(true);
57
58 saveService(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: "HBAC service modified",
66 variant: "success",
67 })
68 );
69 props.onRefresh();
70 } else if (response.data?.error) {
71 // Show toast notification: error
72 const errorMessage = response.data.error as ErrorResult;
73 dispatch(
74 addAlert({
75 name: "save-error",
76 title: errorMessage.message,
77 variant: "danger",
78 })
79 );
80 // Reset values. Disable 'revert' and 'save' buttons
81 props.onResetValues();
82 }
83 setSaving(false);
84 }
85 });
86 };
87
88 // 'Revert' handler method
89 const onRevert = () => {
90 props.onServiceChange(props.originalService);
91 dispatch(
92 addAlert({
93 name: "revert-success",
94 title: "HBAC service data reverted",
95 variant: "success",
96 })
97 );
98 };
99
100 // Toolbar
101 const toolbarFields = [
102 {
103 key: 0,
104 element: (
105 <SecondaryButton
106 dataCy="hbac-services-tab-settings-button-refresh"
107 onClickHandler={props.onRefresh}
108 >
109 Refresh
110 </SecondaryButton>
111 ),
112 },
113 {
114 key: 1,
115 element: (
116 <SecondaryButton
117 dataCy="hbac-services-tab-settings-button-revert"
118 isDisabled={!props.isModified}
119 onClickHandler={onRevert}
120 >
121 Revert
122 </SecondaryButton>
123 ),
124 },
125 {
126 key: 2,
127 element: (
128 <SecondaryButton
129 dataCy="hbac-services-tab-settings-button-save"
130 isDisabled={!props.isModified || isSaving}
131 onClickHandler={onSave}
132 isLoading={isSaving}
133 spinnerAriaValueText="Saving"
134 spinnerAriaLabelledBy="Saving"
135 spinnerAriaLabel="Saving"
136 >
137 {isSaving ? "Saving" : "Save"}
138 </SecondaryButton>
139 ),
140 },
141 ];
142
143 // Render component
144 return (
145 <TabLayout id="settings-page" toolbarItems={toolbarFields}>
146 <Flex direction={{ default: "column" }} flex={{ default: "flex_1" }}>
147 <TitleLayout
148 key={0}
149 headingLevel="h1"
150 id="hbacservice-settings"
151 text="HBAC service settings"
152 />
153 <Form className="pf-v6-u-mt-sm pf-v6-u-mb-lg pf-v6-u-mr-md">
154 <FormGroup label="Description" fieldId="description">
155 <IpaTextArea
156 dataCy="hbac-services-tab-settings-textbox-description"
157 name="description"
158 ipaObject={ipaObject}
159 onChange={recordOnChange}
160 objectName="hbacsvc"
161 metadata={props.metadata}
162 />
163 </FormGroup>
164 </Form>
165 </Flex>
166 </TabLayout>
167 );
168 };
169
170 export default HBACServicesSettings;