Debian Codegraph @

install/freeipa-webui/src/pages/DNSZones/DnsResourceRecordsSettings.tsx

raw
1 import React from "react";
2 // PatternFly
3 import {
4 Form,
5 FormGroup,
6 Sidebar,
7 SidebarContent,
8 SidebarPanel,
9 TextInput,
10 } from "@patternfly/react-core";
11 // Data types
12 import { DNSRecord, Host, Metadata } from "src/utils/datatypes/globalDataTypes";
13 // Redux
14 import { useAppDispatch } from "src/store/hooks";
15 // Hooks
16 import useUpdateRoute from "src/hooks/useUpdateRoute";
17 import { addAlert } from "src/store/Global/alerts-slice";
18 // Icons
19 import { OutlinedQuestionCircleIcon } from "@patternfly/react-icons";
20 // RPC
21 import {
22 useModDnsRecordMutation,
23 ModDnsRecordPayload,
24 } from "src/services/rpcDnsZones";
25 // Utils
26 import { dnsRecordAsRecord } from "src/utils/dnsRecordUtils";
27 // Components
28 import SecondaryButton from "src/components/layouts/SecondaryButton";
29 import HelpTextWithIconLayout from "src/components/layouts/HelpTextWithIconLayout";
30 import { BreadCrumbItem } from "src/components/layouts/BreadCrumb";
31 import TitleLayout from "src/components/layouts/TitleLayout";
32 import PageWithGrayBorderLayout from "src/components/layouts/PageWithGrayBorderLayout";
33 import IpaNumberInput from "src/components/Form/IpaNumberInput/IpaNumberInput";
34 import StandardRecordTypes from "src/components/DnsRecordsSections/StandardRecordTypes";
35 import OtherRecordTypes from "src/components/DnsRecordsSections/OtherRecordTypes";
36
37 interface DnsResourceRecordsSettingsProps {
38 idnsname: string;
39 recordName: string;
40 dnsRecord: Partial<DNSRecord>;
41 originalDnsRecord: Partial<DNSRecord>;
42 host: Partial<Host>;
43 originalHost: Partial<Host>;
44 metadata: Metadata;
45 onDnsRecordChange: (dnsRecord: Partial<DNSRecord>) => void;
46 onHostChange: (host: Partial<Host>) => void;
47 isModified: boolean;
48 isDataLoading: boolean;
49 modifiedValues: () => Partial<DNSRecord>;
50 onResetValues: () => void;
51 pathname: string;
52 onRefresh: () => void;
53 breadcrumbItems: BreadCrumbItem[];
54 }
55
56 const DnsResourceRecordsSettings = (props: DnsResourceRecordsSettingsProps) => {
57 const dispatch = useAppDispatch();
58
59 // Update current route data to Redux and highlight the current page in the Nav bar
60 useUpdateRoute({ pathname: props.pathname });
61
62 // RPC calls
63 const [saveDnsRecord] = useModDnsRecordMutation();
64
65 // Get 'ipaObject' and 'recordOnChange' to use in 'IpaTextInput'
66 const { ipaObject, recordOnChange } = dnsRecordAsRecord(
67 props.dnsRecord,
68 props.onDnsRecordChange
69 );
70
71 // 'Revert' handler method
72 const onRevert = () => {
73 props.onDnsRecordChange(props.originalDnsRecord);
74 props.onHostChange(props.originalHost);
75 props.onResetValues();
76 props.onRefresh();
77 dispatch(
78 addAlert({
79 name: "revert-success",
80 title: "DNS record data reverted",
81 variant: "success",
82 })
83 );
84 };
85
86 // Refresh handler - let parent handle the loading state
87 const onRefresh = () => {
88 props.onRefresh();
89 };
90
91 // on save handler method
92 const onSave = () => {
93 const modifiedValues = props.modifiedValues();
94
95 const payload: ModDnsRecordPayload = {
96 dnsZoneId: props.idnsname as string,
97 recordName: props.recordName as string,
98 dnsttl: modifiedValues.dnsttl as number,
99 };
100 saveDnsRecord(payload).then((response) => {
101 if ("data" in response) {
102 const data = response.data;
103 if (data?.error) {
104 dispatch(
105 addAlert({
106 name: "error",
107 title: (data.error as Error).message,
108 variant: "danger",
109 })
110 );
111 }
112 if (data?.result) {
113 dispatch(
114 addAlert({
115 name: "success",
116 title: "DNS record data saved",
117 variant: "success",
118 })
119 );
120 // Update local state and trigger parent refresh to get latest data
121 props.onDnsRecordChange(data.result.result);
122 props.onRefresh();
123 }
124 }
125 });
126 };
127
128 // Toolbar
129 const toolbarFields = [
130 {
131 key: 0,
132 element: (
133 <SecondaryButton
134 dataCy="dns-zones-tab-settings-button-refresh"
135 onClickHandler={onRefresh}
136 >
137 Refresh
138 </SecondaryButton>
139 ),
140 },
141 {
142 key: 1,
143 element: (
144 <SecondaryButton
145 dataCy="dns-zones-tab-settings-button-revert"
146 isDisabled={!props.isModified || props.isDataLoading}
147 onClickHandler={onRevert}
148 >
149 Revert
150 </SecondaryButton>
151 ),
152 },
153 {
154 key: 2,
155 element: (
156 <SecondaryButton
157 dataCy="dns-zones-tab-settings-button-save"
158 isDisabled={!props.isModified || props.isDataLoading}
159 onClickHandler={onSave}
160 >
161 Save
162 </SecondaryButton>
163 ),
164 },
165 ];
166
167 // Render component
168 return (
169 <>
170 <PageWithGrayBorderLayout
171 id="dns-resource-records-settings-page"
172 pageTitle="DNS Resource Record Settings"
173 toolbarItems={toolbarFields}
174 breadcrumbItems={props.breadcrumbItems}
175 >
176 <Sidebar isPanelRight>
177 <SidebarPanel variant="sticky">
178 <HelpTextWithIconLayout
179 textContent="Help"
180 icon={
181 <OutlinedQuestionCircleIcon className="pf-v5-u-primary-color-100 pf-v5-u-mr-sm" />
182 }
183 />
184 </SidebarPanel>
185 <SidebarContent className="pf-v5-u-mr-xl">
186 <TitleLayout
187 id="identity-settings"
188 text="Identity Settings"
189 headingLevel="h1"
190 className="pf-v5-u-mb-lg"
191 />
192 <Form className="pf-v5-u-mb-lg" isHorizontal>
193 <FormGroup label="Record name" role="idnsname">
194 <TextInput
195 data-cy="dns-zones-tab-settings-textbox-idnsname"
196 name={"idnsname"}
197 aria-label={"Record name text input"}
198 value={props.recordName}
199 readOnlyVariant="plain"
200 />
201 </FormGroup>
202 </Form>
203 <TitleLayout
204 id="record-settings"
205 text="Record Settings"
206 headingLevel="h1"
207 className="pf-v5-u-mb-lg"
208 />
209 <Form className="pf-v5-u-mb-xl" isHorizontal>
210 <FormGroup label="Time to live (seconds)" role="dnsttl">
211 <IpaNumberInput
212 dataCy="dns-zones-tab-settings-textbox-dnsttl"
213 name={"dnsttl"}
214 aria-label={"Time to live in seconds text input"}
215 ipaObject={ipaObject}
216 onChange={recordOnChange}
217 objectName="dnsrecord"
218 metadata={props.metadata}
219 numCharsShown={10}
220 maxValue={2147483647}
221 />
222 </FormGroup>
223 </Form>
224 <TitleLayout
225 id="standard-record-types"
226 text="Standard Record Types"
227 headingLevel="h1"
228 className="pf-v5-u-mb-lg"
229 />
230 <StandardRecordTypes
231 idnsname={props.idnsname}
232 recordName={props.recordName}
233 isDataLoading={props.isDataLoading}
234 onRefresh={onRefresh}
235 dnsRecords={props.dnsRecord.dnsrecords || []}
236 />
237 <TitleLayout
238 id="other-record-types"
239 text="Other Record Types"
240 headingLevel="h1"
241 className="pf-v5-u-mb-lg pf-v5-u-mt-xl"
242 />
243 <OtherRecordTypes
244 idnsname={props.idnsname}
245 recordName={props.recordName}
246 isDataLoading={props.isDataLoading}
247 onRefresh={onRefresh}
248 dnsRecords={props.dnsRecord.dnsrecords || []}
249 />
250 </SidebarContent>
251 </Sidebar>
252 </PageWithGrayBorderLayout>
253 </>
254 );
255 };
256
257 export default DnsResourceRecordsSettings;