Skip to content

Commit

Permalink
- client: Submit setFiltersConfig action on if the values are changed
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtemBaskal committed May 29, 2020
1 parent 72f253f commit aaf4ba8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 22 deletions.
1 change: 0 additions & 1 deletion client/src/components/Settings/FiltersConfig/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ const Form = (props) => {
<label className="form__label">
<Trans>filters_interval</Trans>
</label>

{getIntervalSelect(processing, t, handleChange, toNumber)}
</div>
</div>
Expand Down
40 changes: 21 additions & 19 deletions client/src/components/Settings/FiltersConfig/index.js
Original file line number Diff line number Diff line change
@@ -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);
}, DEBOUNCE_TIMEOUT);
const FiltersConfig = (props) => {
const { initialValues, processing } = props;

const handleFormChange = debounce((values) => {
const diff = getObjDiff(initialValues, values);

render() {
const { interval, enabled, processing } = this.props;
if (Object.values(diff).length > 0) {
props.setFiltersConfig(values);
}
}, DEBOUNCE_TIMEOUT);

return (
<Form
initialValues={{ interval, enabled }}
onSubmit={this.handleFormChange}
onChange={this.handleFormChange}
processing={processing}
/>
);
}
}
return (
<Form
initialValues={initialValues}
onSubmit={handleFormChange}
onChange={handleFormChange}
processing={processing}
/>
);
};

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,
Expand Down
6 changes: 4 additions & 2 deletions client/src/components/Settings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ class Settings extends Component {
<Card bodyType="card-body box-body--settings">
<div className="form">
<FiltersConfig
interval={filtering.interval}
enabled={filtering.enabled}
initialValues={{
interval: filtering.interval,
enabled: filtering.enabled,
}}
processing={filtering.processingSetConfig}
setFiltersConfig={setFiltersConfig}
/>
Expand Down
13 changes: 13 additions & 0 deletions client/src/helpers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit aaf4ba8

Please sign in to comment.