1
0
mirror of https://gitlab.nic.cz/turris/reforis/foris-js.git synced 2024-06-17 18:33:59 +00:00
foris-js/src/common/WiFiSettings/WiFiForm.js

307 lines
11 KiB
JavaScript
Raw Normal View History

2020-01-09 10:25:29 +00:00
/*
* Copyright (C) 2019-2022 CZ.NIC z.s.p.o. (https://www.nic.cz/)
2020-01-09 10:25:29 +00:00
*
* This is free software, licensed under the GNU General Public License v3.
* See /LICENSE for more information.
*/
import React from "react";
import PropTypes from "prop-types";
import { Switch } from "../../bootstrap/Switch";
2020-01-09 10:25:29 +00:00
import { PasswordInput } from "../../bootstrap/PasswordInput";
import { RadioSet } from "../../bootstrap/RadioSet";
import { Select } from "../../bootstrap/Select";
import { TextInput } from "../../bootstrap/TextInput";
import WiFiQRCode from "./WiFiQRCode";
import WifiGuestForm from "./WiFiGuestForm";
2021-10-01 15:27:48 +00:00
import { HELP_TEXTS, HTMODES, HWMODES, ENCRYPTIONMODES } from "./constants";
2020-01-09 10:25:29 +00:00
WiFiForm.propTypes = {
formData: PropTypes.shape({ devices: PropTypes.arrayOf(PropTypes.object) })
.isRequired,
formErrors: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
2020-01-09 10:25:29 +00:00
setFormValue: PropTypes.func.isRequired,
hasGuestNetwork: PropTypes.bool,
};
WiFiForm.defaultProps = {
formData: { devices: [] },
setFormValue: () => {},
2020-01-09 10:25:29 +00:00
hasGuestNetwork: true,
};
export default function WiFiForm({
formData,
formErrors,
setFormValue,
hasGuestNetwork,
disabled,
2020-01-09 10:25:29 +00:00
}) {
return formData.devices.map((device, index) => (
2020-01-09 10:25:29 +00:00
<DeviceForm
key={device.id}
formData={device}
deviceIndex={index}
formErrors={(formErrors || [])[index]}
2020-01-09 10:25:29 +00:00
setFormValue={setFormValue}
hasGuestNetwork={hasGuestNetwork}
disabled={disabled}
divider={index + 1 !== formData.devices.length}
2020-01-09 10:25:29 +00:00
/>
));
}
DeviceForm.propTypes = {
formData: PropTypes.shape({
id: PropTypes.number.isRequired,
enabled: PropTypes.bool.isRequired,
SSID: PropTypes.string.isRequired,
password: PropTypes.string.isRequired,
hidden: PropTypes.bool.isRequired,
hwmode: PropTypes.string.isRequired,
htmode: PropTypes.string.isRequired,
channel: PropTypes.string.isRequired,
guest_wifi: PropTypes.object.isRequired,
2021-10-01 15:27:48 +00:00
encryption: PropTypes.string.isRequired,
available_bands: PropTypes.array.isRequired,
ieee80211w_disabled: PropTypes.bool,
2020-01-09 10:25:29 +00:00
}),
formErrors: PropTypes.object.isRequired,
setFormValue: PropTypes.func.isRequired,
hasGuestNetwork: PropTypes.bool,
deviceIndex: PropTypes.number,
divider: PropTypes.bool,
2020-01-09 10:25:29 +00:00
};
DeviceForm.defaultProps = {
formErrors: {},
hasGuestNetwork: true,
};
function DeviceForm({
formData,
formErrors,
setFormValue,
hasGuestNetwork,
deviceIndex,
divider,
...props
2020-01-09 10:25:29 +00:00
}) {
const deviceID = formData.id;
const bnds = formData.available_bands;
2020-01-09 10:25:29 +00:00
return (
<>
<Switch
label={<h2>{_(`Wi-Fi ${deviceID + 1}`)}</h2>}
2020-01-09 10:25:29 +00:00
checked={formData.enabled}
onChange={setFormValue((value) => ({
devices: {
[deviceIndex]: { enabled: { $set: value } },
},
}))}
switchHeading
2020-01-09 10:25:29 +00:00
{...props}
/>
{formData.enabled && (
<>
<TextInput
label="SSID"
value={formData.SSID}
error={formErrors.SSID || null}
2020-09-11 15:12:40 +00:00
helpText={HELP_TEXTS.ssid}
required
onChange={setFormValue((value) => ({
devices: {
[deviceIndex]: {
SSID: { $set: value },
},
},
}))}
{...props}
>
<div className="input-group-append">
<WiFiQRCode
SSID={formData.SSID}
password={formData.password}
/>
</div>
</TextInput>
<PasswordInput
withEye
2021-02-18 09:54:46 +00:00
label={_("Password")}
value={formData.password}
error={formErrors.password}
helpText={HELP_TEXTS.password}
required
onChange={setFormValue((value) => ({
devices: {
[deviceIndex]: { password: { $set: value } },
},
}))}
{...props}
/>
<Switch
2021-02-18 09:54:46 +00:00
label={_("Hide SSID")}
helpText={HELP_TEXTS.hidden}
checked={formData.hidden}
onChange={setFormValue((value) => ({
devices: {
[deviceIndex]: { hidden: { $set: value } },
},
}))}
{...props}
/>
<RadioSet
name={`hwmode-${deviceID}`}
label="GHz"
choices={getHwmodeChoices(formData)}
value={formData.hwmode}
helpText={HELP_TEXTS.hwmode}
2020-09-14 10:08:53 +00:00
inline
onChange={setFormValue((value) => {
// Get the last item in an array of available HT modes
const [best2] = bnds[0].available_htmodes.slice(-1);
const [best5] = bnds[1].available_htmodes.slice(-1);
return {
devices: {
[deviceIndex]: {
hwmode: { $set: value },
channel: { $set: "0" },
htmode: {
$set:
// Set HT mode depending on checked frequency
value === "11a" ? best5 : best2,
},
},
},
};
})}
{...props}
/>
<Select
label={_("802.11n/ac/ax mode")}
choices={getHtmodeChoices(formData)}
value={formData.htmode}
helpText={HELP_TEXTS.htmode}
onChange={setFormValue((value) => ({
devices: {
[deviceIndex]: { htmode: { $set: value } },
},
}))}
{...props}
/>
<Select
2021-02-18 09:54:46 +00:00
label={_("Channel")}
choices={getChannelChoices(formData)}
value={formData.channel}
onChange={setFormValue((value) => ({
devices: {
[deviceIndex]: { channel: { $set: value } },
},
}))}
{...props}
/>
2021-10-01 15:27:48 +00:00
<Select
label={_("Encryption")}
choices={getEncryptionChoices(formData)}
helpText={HELP_TEXTS.wpa3}
value={formData.encryption}
onChange={setFormValue((value) => ({
devices: {
[deviceIndex]: { encryption: { $set: value } },
},
}))}
{...props}
/>
{(formData.encryption === "WPA3" ||
formData.encryption === "WPA2/3") && (
<Switch
label={_("Disable Management Frame Protection")}
helpText={_(
"In case you have trouble connecting to WiFi Access Point, try disabling Management Frame Protection."
)}
checked={formData.ieee80211w_disabled}
onChange={setFormValue((value) => ({
devices: {
[deviceIndex]: {
ieee80211w_disabled: { $set: value },
},
},
}))}
{...props}
/>
)}
{hasGuestNetwork && (
<WifiGuestForm
formData={{
id: deviceIndex,
...formData.guest_wifi,
}}
formErrors={formErrors.guest_wifi || {}}
setFormValue={setFormValue}
2020-01-09 10:25:29 +00:00
{...props}
/>
)}
</>
)}
{divider && <hr />}
2020-01-09 10:25:29 +00:00
</>
);
}
function getChannelChoices(device) {
const channelChoices = {
0: _("auto"),
};
device.available_bands.forEach((availableBand) => {
if (availableBand.hwmode !== device.hwmode) return;
availableBand.available_channels.forEach((availableChannel) => {
channelChoices[availableChannel.number.toString()] = `
${availableChannel.number}
(${availableChannel.frequency} MHz ${
availableChannel.radar ? " ,DFS" : ""
})
2020-01-09 10:25:29 +00:00
`;
});
});
return channelChoices;
}
function getHtmodeChoices(device) {
const htmodeChoices = {};
device.available_bands.forEach((availableBand) => {
if (availableBand.hwmode !== device.hwmode) return;
availableBand.available_htmodes.forEach((availableHtmod) => {
htmodeChoices[availableHtmod] = HTMODES[availableHtmod];
});
});
return htmodeChoices;
}
function getHwmodeChoices(device) {
return device.available_bands.map((availableBand) => ({
label: HWMODES[availableBand.hwmode],
value: availableBand.hwmode,
}));
}
2021-10-01 15:27:48 +00:00
function getEncryptionChoices(device) {
if (device.encryption === "custom") {
ENCRYPTIONMODES.custom = _("Custom");
}
return ENCRYPTIONMODES;
}