badguardhome/client/src/components/Settings/Dhcp/Interfaces.js
Ainar Garipov bf4c256c72 Pull request: return 501 when we don't support features
Merge in DNS/adguard-home from 2295-dhcp-windows to master

Updates #2295.

Squashed commit of the following:

commit 3b00a90c3d9bc33e9af478e4062c0f938d4f327d
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Mon Nov 16 16:45:43 2020 +0300

    all: use the 501 handlers instead of the real ones, revert other changes

commit 0a3b37736a21abd6181e0d28c32069e8d7a576d0
Merge: 45feba755 6358240e9
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Mon Nov 16 15:59:15 2020 +0300

    Merge branch 'master' into 2295-dhcp-windows and update

commit 45feba755dde37e43cc8075b896e1576157341e6
Merge: cd987d8bc a19523b25
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Mon Nov 16 15:51:16 2020 +0300

    Merge branch 'master' into 2295-dhcp-windows

commit cd987d8bc2cd524b7454d9037b595069714645f9
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Nov 13 15:55:23 2020 +0300

    all: improve tests and refactor dhcp checking code even more

commit 3aad675443f325b5909523bcc1c987aa04ac61d9
Merge: 70c477e61 09196118e
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Nov 13 14:44:43 2020 +0300

    Merge branch 'master' into 2295-dhcp-windows

commit 70c477e61cdc1237603918f1c44470c1549f1136
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Nov 13 14:34:06 2020 +0300

    home: fix dhcpd test on windows

commit e59597d783fb9304e63f94eee2b5a5d67a5b2169
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Nov 13 13:38:25 2020 +0300

    all: mention the feature in the changelog

commit 5555c8d881b1c20b5b0a0cb096a17cf56e209c06
Merge: c3b6a5a93 e802e6645
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Nov 13 13:35:35 2020 +0300

    Merge branch 'master' into 2295-dhcp-windows

commit c3b6a5a930693090838eb1ef9f75a09b5b223ba6
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Nov 12 20:37:09 2020 +0300

    util: fix comment

commit ed92dfdb5d3a6c4ba5d032cbe781e7fd87882813
Author: ArtemBaskal <asbaskal@miem.hse.ru>
Date:   Thu Nov 12 20:24:14 2020 +0300

    Adapt client

commit e6f0494c20a4ad5388492af9091568eea5c6e2d6
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Nov 12 13:35:25 2020 +0300

    return 501 when we don't support features
2020-11-16 19:01:12 +03:00

110 lines
3.2 KiB
JavaScript

import React from 'react';
import { shallowEqual, useSelector } from 'react-redux';
import { Field, reduxForm } from 'redux-form';
import { Trans, useTranslation } from 'react-i18next';
import propTypes from 'prop-types';
import { renderSelectField } from '../../../helpers/form';
import { validateRequiredValue } from '../../../helpers/validators';
import { FORM_NAME } from '../../../helpers/constants';
const renderInterfaces = (interfaces) => Object.keys(interfaces)
.map((item) => {
const option = interfaces[item];
const { name } = option;
const [interfaceIPv4] = option?.ipv4_addresses ?? [];
const [interfaceIPv6] = option?.ipv6_addresses ?? [];
const optionContent = [name, interfaceIPv4, interfaceIPv6].filter(Boolean).join(' - ');
return <option value={name} key={name}>{optionContent}</option>;
});
const getInterfaceValues = ({
gateway_ip,
hardware_address,
ip_addresses,
}) => [
{
name: 'dhcp_form_gateway_input',
value: gateway_ip,
},
{
name: 'dhcp_hardware_address',
value: hardware_address,
},
{
name: 'dhcp_ip_addresses',
value: ip_addresses,
render: (ip_addresses) => ip_addresses
.map((ip) => <span key={ip} className="interface__ip">{ip}</span>),
},
];
const renderInterfaceValues = ({
gateway_ip,
hardware_address,
ip_addresses,
}) => <div className='d-flex align-items-end dhcp__interfaces-info'>
<ul className="list-unstyled m-0">
{getInterfaceValues({
gateway_ip,
hardware_address,
ip_addresses,
}).map(({ name, value, render }) => value && <li key={name}>
<span className="interface__title"><Trans>{name}</Trans>: </span>
{render?.(value) || value}
</li>)}
</ul>
</div>;
const Interfaces = () => {
const { t } = useTranslation();
const {
processingInterfaces,
interfaces,
enabled,
} = useSelector((store) => store.dhcp, shallowEqual);
const interface_name = useSelector(
(store) => store.form[FORM_NAME.DHCP_INTERFACES]?.values?.interface_name,
);
const interfaceValue = interface_name && interfaces[interface_name];
if (processingInterfaces || !interfaces) {
return null;
}
return <div className="row dhcp__interfaces">
<div className="col col__dhcp">
<Field
name="interface_name"
component={renderSelectField}
className="form-control custom-select pl-4 col-md"
validate={[validateRequiredValue]}
label='dhcp_interface_select'
>
<option value='' disabled={enabled}>
{t('dhcp_interface_select')}
</option>
{renderInterfaces(interfaces)}
</Field>
</div>
{interfaceValue
&& renderInterfaceValues(interfaceValue)}
</div>;
};
renderInterfaceValues.propTypes = {
gateway_ip: propTypes.string.isRequired,
hardware_address: propTypes.string.isRequired,
ip_addresses: propTypes.arrayOf(propTypes.string).isRequired,
};
export default reduxForm({
form: FORM_NAME.DHCP_INTERFACES,
})(Interfaces);