import React, { useEffect, useState } from 'react'; import { Trans, useTranslation } from 'react-i18next'; 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'; import StaticLeases from './StaticLeases/index'; import Card from '../../ui/Card'; import PageTitle from '../../ui/PageTitle'; import Loading from '../../ui/Loading'; 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'; import './index.css'; 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); const interface_name = useSelector( (state) => state.form[FORM_NAME.DHCP_INTERFACES]?.values?.interface_name, ); const [ipv4placeholders, setIpv4Placeholders] = useState(DHCP_DESCRIPTION_PLACEHOLDERS.ipv4); const [ipv6placeholders, setIpv6Placeholders] = useState(DHCP_DESCRIPTION_PLACEHOLDERS.ipv6); useEffect(() => { dispatch(getDhcpStatus()); dispatch(getDhcpInterfaces()); }, []); useEffect(() => { const [ipv4] = interfaces?.[interface_name]?.ipv4_addresses ?? []; const [ipv6] = interfaces?.[interface_name]?.ipv6_addresses ?? []; const gateway_ip = interfaces?.[interface_name]?.gateway_ip; const v4placeholders = ipv4 ? calculateDhcpPlaceholdersIpv4(ipv4, gateway_ip) : DHCP_DESCRIPTION_PLACEHOLDERS.ipv4; const v6placeholders = ipv6 ? calculateDhcpPlaceholdersIpv6() : DHCP_DESCRIPTION_PLACEHOLDERS.ipv6; setIpv4Placeholders(v4placeholders); setIpv6Placeholders(v6placeholders); }, [interface_name]); 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()); } }; const handleSubmit = (values) => { dispatch(setDhcpConfig({ interface_name, ...values, })); }; const enteredSomeV4Value = Object.values(v4) .some(Boolean); const enteredSomeV6Value = Object.values(v6) .some(Boolean); const enteredSomeValue = enteredSomeV4Value || enteredSomeV6Value || interfaceName; const getToggleDhcpButton = () => { const otherDhcpFound = check && (check.v4.other_server.found === STATUS_RESPONSE.YES || check.v6.other_server.found === STATUS_RESPONSE.YES); const filledConfig = interface_name && (Object.values(v4) .every(Boolean) || Object.values(v6) .every(Boolean)); const className = classNames('btn btn-sm', { '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)); }; return ; }; const statusButtonClass = classNames('btn btn-sm dhcp-form__button', { 'btn-loading btn-primary': processingStatus, 'btn-outline-primary': !processingStatus, }); const onClick = () => dispatch(findActiveDhcp(interface_name)); const toggleModal = () => dispatch(toggleLeaseModal()); const initialV4 = enteredSomeV4Value ? v4 : {}; const initialV6 = enteredSomeV6Value ? v6 : {}; if (processing || processingInterfaces) { return ; } if (!processing && !dhcp_available) { return

unavailable_dhcp

unavailable_dhcp_desc

; } const toggleDhcpButton = getToggleDhcpButton(); return <> {toggleDhcpButton} {!processing && !processingInterfaces && <> {!enabled && check && (check.v4.other_server.found !== STATUS_RESPONSE.NO || check.v6.other_server.found !== STATUS_RESPONSE.NO) &&

dhcp_warning
}
{enabled &&
}
} ; }; export default Dhcp;