mirror of
https://gitlab.nic.cz/turris/reforis/foris-js.git
synced 2025-04-19 08:06:40 +02:00
94 lines
2.9 KiB
JavaScript
94 lines
2.9 KiB
JavaScript
/*
|
|
* Copyright (C) 2019 CZ.NIC z.s.p.o. (http://www.nic.cz/)
|
|
*
|
|
* 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 { ForisForm } from "../../form/components/ForisForm";
|
|
import WiFiForm from "./WiFiForm";
|
|
import ResetWiFiSettings from "./ResetWiFiSettings";
|
|
|
|
WiFiSettings.propTypes = {
|
|
ws: PropTypes.object.isRequired,
|
|
endpoint: PropTypes.string.isRequired,
|
|
resetEndpoint: PropTypes.string.isRequired,
|
|
hasGuestNetwork: PropTypes.bool,
|
|
};
|
|
|
|
export function WiFiSettings({
|
|
ws, endpoint, resetEndpoint, hasGuestNetwork,
|
|
}) {
|
|
return (
|
|
<>
|
|
<ForisForm
|
|
ws={ws}
|
|
forisConfig={{
|
|
endpoint,
|
|
wsModule: "wifi",
|
|
}}
|
|
prepData={prepData}
|
|
prepDataToSubmit={prepDataToSubmit}
|
|
validator={validator}
|
|
>
|
|
<WiFiForm hasGuestNetwork={hasGuestNetwork} />
|
|
</ForisForm>
|
|
<ResetWiFiSettings ws={ws} endpoint={resetEndpoint} />
|
|
</>
|
|
);
|
|
}
|
|
|
|
function prepData(formData) {
|
|
formData.devices.forEach((device, idx) => {
|
|
formData.devices[idx].channel = device.channel.toString();
|
|
});
|
|
return formData;
|
|
}
|
|
|
|
function prepDataToSubmit(formData) {
|
|
formData.devices.forEach((device, idx) => {
|
|
delete device.available_bands;
|
|
|
|
formData.devices[idx].channel = parseInt(device.channel);
|
|
|
|
if (!device.enabled) {
|
|
formData.devices[idx] = { id: device.id, enabled: false };
|
|
return;
|
|
}
|
|
|
|
if (!device.guest_wifi.enabled) formData.devices[idx].guest_wifi = { enabled: false };
|
|
});
|
|
return formData;
|
|
}
|
|
|
|
function validator(formData) {
|
|
const formErrors = formData.devices.map(
|
|
(device) => {
|
|
if (!device.enabled) return {};
|
|
|
|
const errors = {};
|
|
if (device.SSID.length > 32) errors.SSID = _("SSID can't be longer than 32 symbols");
|
|
if (device.SSID.length === 0) errors.SSID = _("SSID can't be empty");
|
|
|
|
if (device.password.length < 8) errors.password = _("Password must contain at least 8 symbols");
|
|
|
|
if (!device.guest_wifi.enabled) return errors;
|
|
|
|
const guest_wifi_errors = {};
|
|
if (device.guest_wifi.SSID.length > 32) guest_wifi_errors.SSID = _("SSID can't be longer than 32 symbols");
|
|
if (device.guest_wifi.SSID.length === 0) guest_wifi_errors.SSID = _("SSID can't be empty");
|
|
|
|
if (device.guest_wifi.password.length < 8) guest_wifi_errors.password = _("Password must contain at least 8 symbols");
|
|
|
|
if (guest_wifi_errors.SSID || guest_wifi_errors.password) {
|
|
errors.guest_wifi = guest_wifi_errors;
|
|
}
|
|
return errors;
|
|
},
|
|
);
|
|
return JSON.stringify(formErrors) === "[{},{}]" ? null : formErrors;
|
|
}
|