Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function handleMoneyInput(e: React.FormEvent<HTMLInputElement>) {
- // Validate the value based on input pattern
- if (!e.currentTarget.validity.valid) {
- return;
- }
- // Case when value is empty, immediately set weight value to empty
- if (!e.currentTarget.value || parseInt(e.currentTarget.value) < 1) {
- handleStateLoan(loan.productId, "", "");
- setValue(`${loan.productId}Amount`, "", {
- shouldValidate: true,
- });
- setValue(`${loan.productId}Weight`, "", {
- shouldValidate: true,
- });
- }
- // Set money value
- const moneyAmount = moneyFormat((e.target as HTMLInputElement).value);
- // Calculate gold gained
- if (e.currentTarget.value && goldPrice) {
- let money = e.currentTarget.value.replace(/[.,]/g, "");
- money = money.replace(/[,]/g, ".");
- const goldGained = ((parseFloat(money) / goldPrice) * 0.01)
- .toFixed(4)
- .replace(/[.]/g, ",");
- handleStateLoan(loan.productId, moneyAmount, goldGained);
- setValue(`${loan.productId}Amount`, moneyAmount, {
- shouldValidate: true,
- });
- setValue(`${loan.productId}Weight`, goldGained, {
- shouldValidate: true,
- });
- }
- }
- function handleWeightInput(e: React.FormEvent<HTMLInputElement>) {
- const values = e.currentTarget.value.split(",");
- if (
- (values.length === 2 && values[1].length > 4) ||
- !e.currentTarget.validity.valid
- ) {
- e.preventDefault();
- return;
- }
- const weight = e.currentTarget.value.replace(/[,]/g, ".");
- if (!weight || parseFloat(weight) <= 0 || isNaN(parseFloat(weight))) {
- handleStateLoan(loan.productId, "", "");
- setValue(`${loan.productId}Amount`, "", {
- shouldValidate: true,
- });
- setValue(`${loan.productId}Weight`, "", {
- shouldValidate: true,
- });
- }
- if (weight && !isNaN(parseFloat(weight)) && goldPrice) {
- const goldWeight = e.currentTarget.value;
- let moneyAmount = ((parseFloat(weight) * goldPrice) / 0.01).toFixed(
- 0
- );
- handleStateLoan(loan.productId, moneyAmount, goldWeight);
- setValue(`${loan.productId}Amount`, moneyAmount, {
- shouldValidate: true,
- });
- setValue(`${loan.productId}Weight`, goldWeight, {
- shouldValidate: true,
- });
- }
- }
- function handleWeightInputOnKeyUp(
- e: React.KeyboardEvent<HTMLInputElement>
- ) {
- triggerValidation();
- if (
- (e.code === "Backspace" || e.key === "Backspace") &&
- e.currentTarget.value === "0"
- ) {
- handleStateLoan(loan.productId, "", "");
- setValue(`${loan.productId}Amount`, "", {
- shouldValidate: true,
- });
- setValue(`${loan.productId}Weight`, "", {
- shouldValidate: true,
- });
- } else if (
- e.key === "0" &&
- e.currentTarget.value === "0" &&
- e.currentTarget.value.length === 1
- ) {
- handleStateLoan(loan.productId, "0", "0,");
- setValue(`${loan.productId}Amount`, "0", {
- shouldValidate: true,
- });
- setValue(`${loan.productId}Weight`, "0,", {
- shouldValidate: true,
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement