From 99b7066b834d487500f9a109022fa871ffb94bd9 Mon Sep 17 00:00:00 2001 From: counterweight Date: Fri, 23 May 2025 23:38:03 +0200 Subject: [PATCH] refactor --- src/components/LoanPanel.jsx | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/components/LoanPanel.jsx b/src/components/LoanPanel.jsx index 62dbee0..2ed0c8d 100644 --- a/src/components/LoanPanel.jsx +++ b/src/components/LoanPanel.jsx @@ -11,40 +11,48 @@ const LoanPanel = () => { const [loanDuration, setLoanDuration] = useState(12); const [loanInterest, setLoanInterest] = useState(5); + const sanitizeInputToIntWithinRange = ({ value, min, max }) => { + const onlyDigitsValue = sanitizeInputAsOnlyDigits({ value }); + const inRangeValue = sanitizeInputIntoRange({ + value: onlyDigitsValue, + min: min, + max: max, + }); + + return inRangeValue; + }; + const handleLoanPrincipalChange = (event) => { setHasBeenInteracted(1); const inputValue = event.target.value; - const onlyDigitsValue = sanitizeInputAsOnlyDigits({ value: inputValue }); - const inRangeValue = sanitizeInputIntoRange({ - value: onlyDigitsValue, + const sanitizedInputValue = sanitizeInputToIntWithinRange({ + value: inputValue, min: 0, max: 1_000_000, }); - setLoanPrincipal(inRangeValue); + setLoanPrincipal(sanitizedInputValue); }; const handleLoanDurationChange = (event) => { setHasBeenInteracted(1); const inputValue = event.target.value; - const onlyDigitsValue = sanitizeInputAsOnlyDigits({ value: inputValue }); - const inRangeValue = sanitizeInputIntoRange({ - value: onlyDigitsValue, + const sanitizedInputValue = sanitizeInputToIntWithinRange({ + value: inputValue, min: 1, max: 360, }); - setLoanDuration(inRangeValue); + setLoanDuration(sanitizedInputValue); }; const handleLoanInterestChange = (event) => { setHasBeenInteracted(1); const inputValue = event.target.value; - const onlyDigitsValue = sanitizeInputAsOnlyDigits({ value: inputValue }); - const inRangeValue = sanitizeInputIntoRange({ - value: onlyDigitsValue, + const sanitizedInputValue = sanitizeInputToIntWithinRange({ + value: inputValue, min: 0, max: 50, }); - setLoanInterest(inRangeValue); + setLoanInterest(sanitizedInputValue); }; return (