badguardhome/client/src/components/Settings/Dhcp/index.js

278 lines
9.0 KiB
JavaScript
Raw Normal View History

2020-08-19 15:23:05 +00:00
import React, { useEffect, useState } from 'react';
import { Trans, useTranslation } from 'react-i18next';
2020-08-19 15:23:05 +00:00
import { shallowEqual, useDispatch, useSelector } from 'react-redux';
import classNames from 'classnames';
import { destroy } from 'redux-form';
import {
DHCP_DESCRIPTION_PLACEHOLDERS,
DHCP_FORM_NAMES,
STATUS_RESPONSE,
FORM_NAME,
} from '../../../helpers/constants';
import Leases from './Leases';
2019-05-28 12:07:46 +00:00
import StaticLeases from './StaticLeases/index';
import Card from '../../ui/Card';
import PageTitle from '../../ui/PageTitle';
import Loading from '../../ui/Loading';
2020-08-19 15:23:05 +00:00
import {
findActiveDhcp,
getDhcpInterfaces,
getDhcpStatus,
resetDhcp,
setDhcpConfig,
toggleDhcp,
toggleLeaseModal,
} from '../../../actions';
import FormDHCPv4 from './FormDHCPv4';
import FormDHCPv6 from './FormDHCPv6';
import Interfaces from './Interfaces';
import {
calculateDhcpPlaceholdersIpv4,
calculateDhcpPlaceholdersIpv6,
} from '../../../helpers/helpers';
2020-08-19 15:23:05 +00:00
const Dhcp = () => {
const { t } = useTranslation();
const dispatch = useDispatch();
const {
processingStatus,
processingConfig,
processing,
processingInterfaces,
check,
leases,
staticLeases,
isModalOpen,
processingAdding,
processingDeleting,
processingDhcp,
v4,
v6,
interface_name: interfaceName,
enabled,
dhcp_available,
interfaces,
} = useSelector((state) => state.dhcp, shallowEqual);
2020-08-19 15:23:05 +00:00
const interface_name = useSelector(
(state) => state.form[FORM_NAME.DHCP_INTERFACES]?.values?.interface_name,
);
2020-08-19 15:23:05 +00:00
const [ipv4placeholders, setIpv4Placeholders] = useState(DHCP_DESCRIPTION_PLACEHOLDERS.ipv4);
const [ipv6placeholders, setIpv6Placeholders] = useState(DHCP_DESCRIPTION_PLACEHOLDERS.ipv6);
2020-08-19 15:23:05 +00:00
useEffect(() => {
dispatch(getDhcpStatus());
dispatch(getDhcpInterfaces());
}, []);
2020-08-19 15:23:05 +00:00
useEffect(() => {
const [ipv4] = interfaces?.[interface_name]?.ipv4_addresses ?? [];
const [ipv6] = interfaces?.[interface_name]?.ipv6_addresses ?? [];
const gateway_ip = interfaces?.[interface_name]?.gateway_ip;
2020-08-19 15:23:05 +00:00
const v4placeholders = ipv4
? calculateDhcpPlaceholdersIpv4(ipv4, gateway_ip)
: DHCP_DESCRIPTION_PLACEHOLDERS.ipv4;
2020-08-19 15:23:05 +00:00
const v6placeholders = ipv6
? calculateDhcpPlaceholdersIpv6()
: DHCP_DESCRIPTION_PLACEHOLDERS.ipv6;
2020-08-19 15:23:05 +00:00
setIpv4Placeholders(v4placeholders);
setIpv6Placeholders(v6placeholders);
}, [interface_name]);
2020-08-19 15:23:05 +00:00
const clear = () => {
// eslint-disable-next-line no-alert
if (window.confirm(t('dhcp_reset'))) {
Object.values(DHCP_FORM_NAMES)
.forEach((formName) => dispatch(destroy(formName)));
dispatch(resetDhcp());
}
2020-08-19 15:23:05 +00:00
};
2020-08-19 15:23:05 +00:00
const handleSubmit = (values) => {
dispatch(setDhcpConfig({
interface_name,
...values,
}));
};
2020-08-19 15:23:05 +00:00
const enteredSomeV4Value = Object.values(v4)
.some(Boolean);
const enteredSomeV6Value = Object.values(v6)
.some(Boolean);
const enteredSomeValue = enteredSomeV4Value || enteredSomeV6Value || interfaceName;
2019-03-28 13:30:22 +00:00
2020-08-19 15:23:05 +00:00
const getToggleDhcpButton = () => {
const otherDhcpFound = check && (check.v4.other_server.found === STATUS_RESPONSE.YES
|| check.v6.other_server.found === STATUS_RESPONSE.YES);
2019-03-28 13:30:22 +00:00
2020-08-19 15:23:05 +00:00
const filledConfig = interface_name && (Object.values(v4)
.every(Boolean) || Object.values(v6)
.every(Boolean));
const className = classNames('btn btn-sm mr-2', {
'btn-gray': enabled,
'btn-outline-success': !enabled,
});
const onClickDisable = () => dispatch(toggleDhcp({ enabled }));
const onClickEnable = () => {
const values = {
enabled,
interface_name,
v4: enteredSomeV4Value ? v4 : {},
v6: enteredSomeV6Value ? v6 : {},
};
dispatch(toggleDhcp(values));
};
2019-04-04 13:34:46 +00:00
2020-08-19 15:23:05 +00:00
return <button
type="button"
className={className}
onClick={enabled ? onClickDisable : onClickEnable}
disabled={processingDhcp || processingConfig
|| (!enabled && (!filledConfig || !check || otherDhcpFound))}
>
<Trans>{enabled ? 'dhcp_disable' : 'dhcp_enable'}</Trans>
</button>;
};
2019-04-04 13:34:46 +00:00
2020-08-19 15:23:05 +00:00
const statusButtonClass = classNames('btn btn-sm mx-2', {
'btn-loading btn-primary': processingStatus,
'btn-outline-primary': !processingStatus,
});
Pull request #730: + client: Add Hot Module Replacement Merge in DNS/adguard-home from feature/hmr to master Squashed commit of the following: commit 952ed1955c2a7a32446d99489f137f02eb47c99e Merge: 83484931 de92c852 Author: ArtemBaskal <a.baskal@adguard.com> Date: Thu Aug 13 11:02:10 2020 +0300 Merge branch 'master' into feature/hmr commit 8348493105d7d63d8b0836a5c272df2b17a6b142 Author: ArtemBaskal <a.baskal@adguard.com> Date: Wed Aug 5 15:07:31 2020 +0300 Remove empty prop types, remove Services empty container commit b2fe4a30b79d91e482318ee5deea8e49c7038f7e Author: ArtemBaskal <a.baskal@adguard.com> Date: Wed Aug 5 13:56:35 2020 +0300 Move constants commit f8be4c18c35193ad77bf5e25f311ad834c1d6870 Author: ArtemBaskal <a.baskal@adguard.com> Date: Wed Aug 5 13:19:02 2020 +0300 Fix Setup bug, update webpack.dev commit 1d9cc4ddf8af2c979eb707a7f0fc06744eec186c Author: ArtemBaskal <a.baskal@adguard.com> Date: Wed Aug 5 12:10:38 2020 +0300 Review changes commit a1edb21358def21ed1808b081ffc2f0b6755e3da Author: ArtemBaskal <a.baskal@adguard.com> Date: Wed Aug 5 11:46:58 2020 +0300 Remove lazy loading, fix updated components commit 0aa2cf55f8d4206ac9e2f99fc1b990ed8a9c7825 Author: ArtemBaskal <a.baskal@adguard.com> Date: Tue Aug 4 20:32:19 2020 +0300 Refactor App component, add lazy loading commit 3c2ba4772a91ff7b06641dba6c6bf3fdcd2fdf7f Author: ArtemBaskal <a.baskal@adguard.com> Date: Tue Aug 4 17:12:41 2020 +0300 Simplify App hot loading boilerplate, setup lazy loading, update Header commit 8df3221f315372b066f2ac0c9a1687f1677b8415 Author: ArtemBaskal <a.baskal@adguard.com> Date: Tue Aug 4 15:16:06 2020 +0300 + client: Add Hot Module Replacement
2020-08-13 08:15:45 +00:00
2020-08-19 15:23:05 +00:00
const onClick = () => dispatch(findActiveDhcp(interface_name));
2020-08-19 15:23:05 +00:00
const toggleModal = () => dispatch(toggleLeaseModal());
const initialV4 = enteredSomeV4Value ? v4 : {};
const initialV6 = enteredSomeV6Value ? v6 : {};
if (processing || processingInterfaces) {
return <Loading />;
}
2020-08-19 15:23:05 +00:00
if (!processing && !dhcp_available) {
return <div className="text-center pt-5">
<h2>
<Trans>unavailable_dhcp</Trans>
</h2>
<h4>
<Trans>unavailable_dhcp_desc</Trans>
</h4>
</div>;
}
const toggleDhcpButton = getToggleDhcpButton();
return <>
<PageTitle title={t('dhcp_settings')} subtitle={t('dhcp_description')}>
<div className="page-title__actions">
<div className="mb-3">
{toggleDhcpButton}
<button
type="button"
className={statusButtonClass}
onClick={onClick}
disabled={enabled || !interface_name || processingConfig}
>
<Trans>check_dhcp_servers</Trans>
</button>
<button
type="button"
className='btn btn-sm mx-2 btn-outline-secondary'
disabled={!enteredSomeValue || processingConfig}
onClick={clear}
>
<Trans>reset_settings</Trans>
</button>
</div>
</div>
</PageTitle>
{!processing && !processingInterfaces
&& <>
{!enabled
&& check
&& (check.v4.other_server.found !== STATUS_RESPONSE.NO
|| check.v6.other_server.found !== STATUS_RESPONSE.NO)
&& <div className="mb-5">
<hr />
<div className="text-danger">
<Trans>dhcp_warning</Trans>
</div>
</div>}
<Interfaces
initialValues={{ interface_name: interfaceName }}
/>
<Card
title={t('dhcp_ipv4_settings')}
bodyType="card-body box-body--settings"
>
<div>
<FormDHCPv4
onSubmit={handleSubmit}
initialValues={{ v4: initialV4 }}
processingConfig={processingConfig}
ipv4placeholders={ipv4placeholders}
/>
</div>
</Card>
<Card
title={t('dhcp_ipv6_settings')}
bodyType="card-body box-body--settings"
>
<div>
<FormDHCPv6
onSubmit={handleSubmit}
initialValues={{ v6: initialV6 }}
processingConfig={processingConfig}
ipv6placeholders={ipv6placeholders}
/>
</div>
</Card>
{enabled
&& <Card
title={t('dhcp_leases')}
bodyType="card-body box-body--settings"
>
<div className="row">
<div className="col">
<Leases leases={leases} />
</div>
</div>
</Card>}
<Card
title={t('dhcp_static_leases')}
bodyType="card-body box-body--settings"
>
<div className="row">
<div className="col-12">
<StaticLeases
staticLeases={staticLeases}
isModalOpen={isModalOpen}
processingAdding={processingAdding}
processingDeleting={processingDeleting}
/>
</div>
<div className="col-12">
<button
type="button"
className="btn btn-success btn-standard mt-3"
onClick={toggleModal}
>
<Trans>dhcp_add_static_lease</Trans>
</button>
</div>
</div>
</Card>
</>}
</>;
};
2020-08-19 15:23:05 +00:00
export default Dhcp;