Advertisement
pradiptaagus

tabungan emas calculator

Jun 28th, 2022
1,177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function handleMoneyInput(e: React.FormEvent<HTMLInputElement>) {
  2.         // Validate the value based on input pattern
  3.         if (!e.currentTarget.validity.valid) {
  4.             return;
  5.         }
  6.  
  7.         // Case when value is empty, immediately set weight value to empty
  8.         if (!e.currentTarget.value || parseInt(e.currentTarget.value) < 1) {
  9.             handleStateLoan(loan.productId, "", "");
  10.             setValue(`${loan.productId}Amount`, "", {
  11.                 shouldValidate: true,
  12.             });
  13.             setValue(`${loan.productId}Weight`, "", {
  14.                 shouldValidate: true,
  15.             });
  16.         }
  17.  
  18.         // Set money value
  19.         const moneyAmount = moneyFormat((e.target as HTMLInputElement).value);
  20.  
  21.         // Calculate gold gained
  22.         if (e.currentTarget.value && goldPrice) {
  23.             let money = e.currentTarget.value.replace(/[.,]/g, "");
  24.             money = money.replace(/[,]/g, ".");
  25.             const goldGained = ((parseFloat(money) / goldPrice) * 0.01)
  26.                 .toFixed(4)
  27.                 .replace(/[.]/g, ",");
  28.             handleStateLoan(loan.productId, moneyAmount, goldGained);
  29.             setValue(`${loan.productId}Amount`, moneyAmount, {
  30.                 shouldValidate: true,
  31.             });
  32.             setValue(`${loan.productId}Weight`, goldGained, {
  33.                 shouldValidate: true,
  34.             });
  35.         }
  36.     }
  37.  
  38.     function handleWeightInput(e: React.FormEvent<HTMLInputElement>) {
  39.         const values = e.currentTarget.value.split(",");
  40.         if (
  41.             (values.length === 2 && values[1].length > 4) ||
  42.             !e.currentTarget.validity.valid
  43.         ) {
  44.             e.preventDefault();
  45.             return;
  46.         }
  47.  
  48.         const weight = e.currentTarget.value.replace(/[,]/g, ".");
  49.  
  50.         if (!weight || parseFloat(weight) <= 0 || isNaN(parseFloat(weight))) {
  51.             handleStateLoan(loan.productId, "", "");
  52.             setValue(`${loan.productId}Amount`, "", {
  53.                 shouldValidate: true,
  54.             });
  55.             setValue(`${loan.productId}Weight`, "", {
  56.                 shouldValidate: true,
  57.             });
  58.         }
  59.  
  60.         if (weight && !isNaN(parseFloat(weight)) && goldPrice) {
  61.             const goldWeight = e.currentTarget.value;
  62.             let moneyAmount = ((parseFloat(weight) * goldPrice) / 0.01).toFixed(
  63.                 0
  64.             );
  65.             handleStateLoan(loan.productId, moneyAmount, goldWeight);
  66.             setValue(`${loan.productId}Amount`, moneyAmount, {
  67.                 shouldValidate: true,
  68.             });
  69.             setValue(`${loan.productId}Weight`, goldWeight, {
  70.                 shouldValidate: true,
  71.             });
  72.         }
  73.     }
  74.  
  75.     function handleWeightInputOnKeyUp(
  76.         e: React.KeyboardEvent<HTMLInputElement>
  77.     ) {
  78.         triggerValidation();
  79.         if (
  80.             (e.code === "Backspace" || e.key === "Backspace") &&
  81.             e.currentTarget.value === "0"
  82.         ) {
  83.             handleStateLoan(loan.productId, "", "");
  84.             setValue(`${loan.productId}Amount`, "", {
  85.                 shouldValidate: true,
  86.             });
  87.             setValue(`${loan.productId}Weight`, "", {
  88.                 shouldValidate: true,
  89.             });
  90.         } else if (
  91.             e.key === "0" &&
  92.             e.currentTarget.value === "0" &&
  93.             e.currentTarget.value.length === 1
  94.         ) {
  95.             handleStateLoan(loan.productId, "0", "0,");
  96.             setValue(`${loan.productId}Amount`, "0", {
  97.                 shouldValidate: true,
  98.             });
  99.             setValue(`${loan.productId}Weight`, "0,", {
  100.                 shouldValidate: true,
  101.             });
  102.         }
  103.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement