The main goal of this task is to take values from the query parameter and put it in inputu. The first problem is that I don't know much about how to do it so that the code doesn't repeat itself and I don't know how specifically I could improve the code so that multiple things don't happen again. Next, the function should contain a simple validation of min max to prevent them from entering silliness from the query parameter.
getFromQuery = props => {
const { limitValues } = props;
const parsed = queryString.parse(location.search);
const validation = (value, min, max) => {
if (value < min) {
return min;
} else if (value > max) {
return max;
}
return value;
};
if (parsed.price) {
const defautPrice = limitValues.find(item => item.sliderType === 'PRICE');
props.inputs.PRICE = validation(parsed.price, defautPrice.minValue, defautPrice.maxValue);
}
if (parsed.savings) {
const defautSavings = limitValues.find(item => item.sliderType === 'SAVINGS');
props.inputs.SAVINGS = validation(parsed.savings, defautSavings.minValue, defautSavings.maxValue);
}
if (parsed.maturity) {
const defautMaturity = limitValues.find(item => item.sliderType === 'MATURITY');
props.inputs.MATURITY = validation(parsed.maturity, defautMaturity.minValue, defautMaturity.maxValue);
}
};