Debian Codegraph @

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