diff --git a/client/src/components/Settings/Encryption/Form.tsx b/client/src/components/Settings/Encryption/Form.tsx index 97b166e8..7968cc3e 100644 --- a/client/src/components/Settings/Encryption/Form.tsx +++ b/client/src/components/Settings/Encryption/Form.tsx @@ -74,20 +74,20 @@ const validationMessage = (warningValidation: string, isWarning: boolean) => { }; export type EncryptionFormValues = { - enabled: boolean; - serve_plain_dns: boolean; - server_name: string; - force_https: boolean; - port_https: number; - port_dns_over_tls: number; - port_dns_over_quic: number; - certificate_chain: string; - private_key: string; - certificate_path: string; - private_key_path: string; - certificate_source: string; - key_source: string; - private_key_saved: boolean; + enabled?: boolean; + serve_plain_dns?: boolean; + server_name?: string; + force_https?: boolean; + port_https?: number; + port_dns_over_tls?: number; + port_dns_over_quic?: number; + certificate_chain?: string; + private_key?: string; + certificate_path?: string; + private_key_path?: string; + certificate_source?: string; + key_source?: string; + private_key_saved?: boolean; }; type Props = { @@ -173,11 +173,11 @@ export const Form = ({ serve_plain_dns: servePlainDns, certificate_chain: certificateChain, private_key: privateKey, - certificate_path: certificatePath, private_key_path: privateKeyPath, - certificate_source: certificateSource, - private_key_saved: privateKeySaved, key_source: privateKeySource, + private_key_saved: privateKeySaved, + certificate_path: certificatePath, + certificate_source: certificateSource, } = watchedValues; useEffect(() => { @@ -185,7 +185,6 @@ export const Form = ({ if (JSON.stringify(previousValues) !== JSON.stringify(watchedValues)) { // TODO(ik) onChange TLS config validation - console.log('debouncedConfigValidation'); previousValuesRef.current = watchedValues; } }, [watchedValues]); diff --git a/client/src/components/Settings/Encryption/index.tsx b/client/src/components/Settings/Encryption/index.tsx index 4dda55bc..67c99842 100644 --- a/client/src/components/Settings/Encryption/index.tsx +++ b/client/src/components/Settings/Encryption/index.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useCallback } from 'react'; +import React, { useEffect, useCallback, useMemo } from 'react'; import { useTranslation } from 'react-i18next'; import { debounce } from 'lodash'; import { DEBOUNCE_TIMEOUT, ENCRYPTION_SOURCE } from '../../../helpers/constants'; @@ -24,17 +24,17 @@ export const Encryption = ({ encryption, setTlsConfig, validateTlsConfig }: Prop } }, [encryption, validateTlsConfig]); - const getInitialValues = useCallback((data: any): EncryptionFormValues => { - const { certificate_chain, private_key, private_key_saved } = data; + const initialValues = useMemo((): EncryptionFormValues => { + const { certificate_chain, private_key, private_key_saved } = encryption; const certificate_source = certificate_chain ? ENCRYPTION_SOURCE.CONTENT : ENCRYPTION_SOURCE.PATH; const key_source = private_key || private_key_saved ? ENCRYPTION_SOURCE.CONTENT : ENCRYPTION_SOURCE.PATH; return { - ...data, + ...encryption, certificate_source, key_source, }; - }, []); + }, [encryption]); const getSubmitValues = useCallback((values: any) => { const { certificate_source, key_source, private_key_saved, ...config } = values; @@ -67,18 +67,15 @@ export const Encryption = ({ encryption, setTlsConfig, validateTlsConfig }: Prop [getSubmitValues, setTlsConfig], ); - const debouncedConfigValidation = useCallback( - debounce((values) => { - const submitValues = getSubmitValues(values); + const validateConfig = useCallback((values) => { + const submitValues = getSubmitValues(values); - if (submitValues.enabled) { - validateTlsConfig(submitValues); - } - }, DEBOUNCE_TIMEOUT), - [], - ); + if (submitValues.enabled) { + validateTlsConfig(submitValues); + } + }, []); - const initialValues = getInitialValues(encryption); + const debouncedConfigValidation = useCallback(debounce(validateConfig, DEBOUNCE_TIMEOUT), [validateConfig]); return (