-
Notifications
You must be signed in to change notification settings - Fork 12
/
Hostname.jsx
183 lines (167 loc) · 6.59 KB
/
Hostname.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Copyright (C) 2022 Red Hat, Inc.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with This program; If not, see <http://www.gnu.org/licenses/>.
*/
import cockpit from "cockpit";
import React, { useContext, useEffect, useState } from "react";
import {
Button,
DescriptionListDescription,
DescriptionListGroup,
DescriptionListTerm,
Flex,
FlexItem,
Form,
FormGroup,
FormHelperText,
HelperText,
HelperTextItem,
Modal,
TextInput
} from "@patternfly/react-core";
import { setHostname } from "../../apis/network.js";
import { NetworkContext } from "../Common.jsx";
const _ = cockpit.gettext;
const idPrefix = "installation-review";
const ChangeHostname = ({ initHostname }) => {
const [currentHostname, setCurrentHostname] = useState(initHostname);
const [error, setError] = useState([]);
const [isModalOpen, setIsModalOpen] = useState(false);
const validateHostname = (value) => {
const validationError = [];
if (value.length > 64) {
validationError.push(_("Real hostname must be 64 characters or less"));
}
if (!value.match(/^$|^(?!-)[A-Za-z0-9-]{1,63}(?<!-)(?:\.(?!-)[A-Za-z0-9-]{1,63}(?<!-))*\.?$/)) {
validationError.push(_("Real hostname can only contain characters, digits, dashes (not starting or ending a character) and dots"));
}
setError(validationError);
};
const onHostnameChanged = (value) => {
validateHostname(value);
setCurrentHostname(value);
};
const onSubmit = (event) => {
setHostname({ hostname: currentHostname })
.then(() => {
handleModalToggle();
})
.catch(() => {
setError(_("This hostname can't be submitted"));
});
if (event) {
event.preventDefault();
}
return false;
};
const handleModalToggle = () => {
setIsModalOpen(!isModalOpen);
};
const onClose = () => {
setCurrentHostname(initHostname);
setError([]);
handleModalToggle();
};
const disabled = error.length || (initHostname === currentHostname);
return (
<>
<Button
id="system_information_hostname_button" variant="link"
onClick={handleModalToggle}
isInline aria-label="edit hostname">
{_("edit")}
</Button>
<Modal
footer={
<>
<Button
variant="primary" isDisabled={disabled}
onClick={onSubmit}>{initHostname === "" ? _("Save") : _("Change")}
</Button>
<Button variant="link" onClick={onClose}>{_("Cancel")}</Button>
</>
}
id="system_information_change_hostname"
isOpen={isModalOpen}
onClose={onClose}
position="top"
title={initHostname === "" ? _("Set custom hostname") : _("Change hostname")}
variant="small"
>
<Form isHorizontal onSubmit={onSubmit}>
<FormGroup fieldId="review-handle-hostname-hostname" label={_("Hostname")}>
<TextInput
id="review-handle-hostname-hostname" value={currentHostname}
onChange={(_event, value) => onHostnameChanged(value)}
validated={error.length ? "error" : "default"} />
{error.length > 0
? (
<FormHelperText>
<HelperText>
{error.map((err, i) =>
<HelperTextItem key={i} variant="error">
{err}
</HelperTextItem>
)}
</HelperText>
</FormHelperText>)
: (
<FormHelperText>
<HelperText>
<HelperTextItem>
{_("May contain letters, numbers, and dashes.")}
</HelperTextItem>
<HelperTextItem>
{_("If empty, hostname will be transient and set by network information.")}
</HelperTextItem>
</HelperText>
</FormHelperText>
)}
</FormGroup>
</Form>
</Modal>
</>
);
};
export const HostnameRow = () => {
const network = useContext(NetworkContext);
const [initHostname, setInitHostname] = useState(network.hostname || "");
useEffect(() => {
setInitHostname(network.hostname);
}, [network.hostname]);
return (
<DescriptionListGroup>
<DescriptionListTerm>
{_("Hostname")}
</DescriptionListTerm>
<DescriptionListDescription id={idPrefix + "-target-system-hostname"}>
<Flex
spaceItems={{ default: "spaceItemsMd" }}
alignItems={{ default: "alignItemsCenter" }}>
<FlexItem>
{initHostname !== ""
? initHostname
: <div className="pf-v5-u-color-400">transient, will use DHCP</div>}
</FlexItem>
<FlexItem>
<ChangeHostname
initHostname={initHostname}
setInitHostname={setInitHostname} />
</FlexItem>
</Flex>
</DescriptionListDescription>
</DescriptionListGroup>
);
};