kuotata/src/components/LoanPanel.jsx

89 lines
2.8 KiB
React
Raw Normal View History

2025-05-22 18:52:17 +02:00
import { useState } from "react";
2025-05-22 19:11:07 +02:00
import LoanPrincipalInput from "./LoanPrincipalInput";
import LoanDurationInput from "./LoanDurationInput";
import LoanInterestInput from "./LoanInterestInput";
import sanitizeInputIntoRange from "../inputSanitizers/sanitizeInputIntoRange";
import sanitizeInputAsOnlyDigits from "../inputSanitizers/sanitizeInputAsOnlyDigits";
2025-05-22 18:52:17 +02:00
2025-05-22 18:36:26 +02:00
const LoanPanel = () => {
2025-05-22 18:52:17 +02:00
const [hasBeenInteracted, setHasBeenInteracted] = useState(false);
2025-05-22 19:09:43 +02:00
const [loanPrincipal, setLoanPrincipal] = useState("");
2025-05-22 18:52:17 +02:00
const [loanDuration, setLoanDuration] = useState(12);
const [loanInterest, setLoanInterest] = useState(5);
2025-05-23 23:38:03 +02:00
const sanitizeInputToIntWithinRange = ({ value, min, max }) => {
const onlyDigitsValue = sanitizeInputAsOnlyDigits({ value });
const inRangeValue = sanitizeInputIntoRange({
value: onlyDigitsValue,
min: min,
max: max,
});
return inRangeValue;
};
2025-05-22 19:09:43 +02:00
const handleLoanPrincipalChange = (event) => {
setHasBeenInteracted(1);
const inputValue = event.target.value;
2025-05-23 23:38:03 +02:00
const sanitizedInputValue = sanitizeInputToIntWithinRange({
value: inputValue,
min: 0,
max: 1_000_000,
});
2025-05-23 23:38:03 +02:00
setLoanPrincipal(sanitizedInputValue);
2025-05-22 19:09:43 +02:00
};
const handleLoanDurationChange = (event) => {
setHasBeenInteracted(1);
const inputValue = event.target.value;
2025-05-23 23:38:03 +02:00
const sanitizedInputValue = sanitizeInputToIntWithinRange({
value: inputValue,
min: 1,
max: 360,
});
2025-05-23 23:38:03 +02:00
setLoanDuration(sanitizedInputValue);
2025-05-22 19:09:43 +02:00
};
const handleLoanInterestChange = (event) => {
setHasBeenInteracted(1);
const inputValue = event.target.value;
2025-05-23 23:38:03 +02:00
const sanitizedInputValue = sanitizeInputToIntWithinRange({
value: inputValue,
min: 0,
max: 50,
});
2025-05-23 23:38:03 +02:00
setLoanInterest(sanitizedInputValue);
2025-05-22 19:09:43 +02:00
};
2025-05-22 18:36:26 +02:00
return (
<>
<div className="p-5 bg-gray-50 rounded-3xl border border-gray-400">
<form className="flex flex-col justify-end items-end my-5 text-white bg-blue-600 p-5 rounded-3xl shadow-[0_4px_0_rgba(0,255,255,1)]">
2025-05-22 19:11:07 +02:00
<LoanPrincipalInput
2025-05-22 19:09:43 +02:00
onChangeCallback={handleLoanPrincipalChange}
loanPrincipal={loanPrincipal}
/>
2025-05-22 19:11:07 +02:00
<LoanDurationInput
2025-05-22 19:09:43 +02:00
onChangeCallback={handleLoanDurationChange}
loanDuration={loanDuration}
/>
2025-05-22 19:11:07 +02:00
<LoanInterestInput
2025-05-22 19:09:43 +02:00
onChangeCallback={handleLoanInterestChange}
loanInterest={loanInterest}
/>
2025-05-22 18:36:26 +02:00
</form>
2025-05-23 23:41:22 +02:00
{hasBeenInteracted ? (
<div className="flex flex-col items-center justify-center">
<div className="w-8 h-8 border-4 border-blue-500 border-t-transparent rounded-full animate-spin"></div>
<p className="font-light">
Calculando los detalles del préstamo...
</p>
</div>
) : null}
2025-05-22 18:36:26 +02:00
</div>
</>
);
};
export default LoanPanel;