From b8032801a2af0fc415ee5f5a6f09f6721b965a24 Mon Sep 17 00:00:00 2001 From: Artem Baskal Date: Fri, 29 May 2020 16:08:01 +0300 Subject: [PATCH] - client: Submit setFiltersConfig action on if the values are changed: Merge pull request #637 in DNS/adguard-home from fix/1749 to master Close #1749 Squashed commit of the following: commit aaf4ba86429670ea8b0001325562c4a173be5b4a Author: ArtemBaskal Date: Fri May 29 13:29:44 2020 +0300 - client: Submit setFiltersConfig action on if the values are changed --- .../components/Settings/FiltersConfig/Form.js | 1 - .../Settings/FiltersConfig/index.js | 40 ++++++++++--------- client/src/components/Settings/index.js | 6 ++- client/src/helpers/helpers.js | 13 ++++++ 4 files changed, 38 insertions(+), 22 deletions(-) diff --git a/client/src/components/Settings/FiltersConfig/Form.js b/client/src/components/Settings/FiltersConfig/Form.js index 9863d810..d08196bf 100644 --- a/client/src/components/Settings/FiltersConfig/Form.js +++ b/client/src/components/Settings/FiltersConfig/Form.js @@ -61,7 +61,6 @@ const Form = (props) => { - {getIntervalSelect(processing, t, handleChange, toNumber)} diff --git a/client/src/components/Settings/FiltersConfig/index.js b/client/src/components/Settings/FiltersConfig/index.js index c19fe24e..65e82ccf 100644 --- a/client/src/components/Settings/FiltersConfig/index.js +++ b/client/src/components/Settings/FiltersConfig/index.js @@ -1,33 +1,35 @@ -import React, { Component } from 'react'; +import React from 'react'; import PropTypes from 'prop-types'; import { withTranslation } from 'react-i18next'; import debounce from 'lodash/debounce'; import { DEBOUNCE_TIMEOUT } from '../../../helpers/constants'; import Form from './Form'; +import { getObjDiff } from '../../../helpers/helpers'; -class FiltersConfig extends Component { - handleFormChange = debounce((values) => { - this.props.setFiltersConfig(values); +const FiltersConfig = (props) => { + const { initialValues, processing } = props; + + const handleFormChange = debounce((values) => { + const diff = getObjDiff(initialValues, values); + + if (Object.values(diff).length > 0) { + props.setFiltersConfig(values); + } }, DEBOUNCE_TIMEOUT); - render() { - const { interval, enabled, processing } = this.props; - - return ( -
- ); - } -} + return ( + + ); +}; FiltersConfig.propTypes = { - interval: PropTypes.number.isRequired, - enabled: PropTypes.bool.isRequired, + initialValues: PropTypes.object.isRequired, processing: PropTypes.bool.isRequired, setFiltersConfig: PropTypes.func.isRequired, t: PropTypes.func.isRequired, diff --git a/client/src/components/Settings/index.js b/client/src/components/Settings/index.js index c3e08584..66cff317 100644 --- a/client/src/components/Settings/index.js +++ b/client/src/components/Settings/index.js @@ -92,8 +92,10 @@ class Settings extends Component {
diff --git a/client/src/helpers/helpers.js b/client/src/helpers/helpers.js index d931410b..a6bd8495 100644 --- a/client/src/helpers/helpers.js +++ b/client/src/helpers/helpers.js @@ -482,6 +482,19 @@ export const getCurrentFilter = (url, filters) => { }; }; +/** + * @param {object} initialValues + * @param {object} values + * @returns {object} Returns different values of objects + */ +export const getObjDiff = (initialValues, values) => Object.entries(values) + .reduce((acc, [key, value]) => { + if (value !== initialValues[key]) { + acc[key] = value; + } + return acc; + }, {}); + /** * @param number Number to format * @returns string Returns a string with a language-sensitive representation of this number