diff --git a/src/_components/calculator-component/fhsa/fhsa-calculator-component.tsx b/src/_components/calculator-component/fhsa/fhsa-calculator-component.tsx index e3ac1211..7dffb7dd 100644 --- a/src/_components/calculator-component/fhsa/fhsa-calculator-component.tsx +++ b/src/_components/calculator-component/fhsa/fhsa-calculator-component.tsx @@ -7,6 +7,8 @@ import React from 'react'; import FHSAvars from './fhsa-interface'; import FhsaResultComponent from './fhsa-result-component'; +const currentYear = new Date().getFullYear(); + export default function FhsaCalculatorComponent() { const [AgeChecked, setAgeChecked] = React.useState(false); const [HouseChecked, setHouseChecked] = React.useState(false); @@ -92,7 +94,7 @@ export default function FhsaCalculatorComponent() { const element = document.getElementById('openedError')!; if (input.opened.textInput.current) { if ( - Number(input.opened.textInput.current.value) <= 2025 && + Number(input.opened.textInput.current.value) <= currentYear && Number(input.opened.textInput.current.value) >= 2023 ) { setOpenedInputFlag(false); @@ -101,8 +103,7 @@ export default function FhsaCalculatorComponent() { return; } } - element.textContent = - 'Error: your FHSA must be opened on a valid date. Please enter a valid date (2023-2025).'; + element.textContent = `Error: your FHSA must be opened on a valid date. Please enter a valid date (2023-${currentYear}).`; setOpenedInputFlag(true); resetContributed(); }; @@ -168,7 +169,7 @@ export default function FhsaCalculatorComponent() { } if (!AccountChecked) { errorMsg.textContent = null; - FhsaResultComponent([2025, 0]); + FhsaResultComponent([currentYear, 0]); setShowCalculation(true); return; } @@ -271,9 +272,7 @@ export default function FhsaCalculatorComponent() { Your contribution limit for this year is:
-
- Your total lifetime contribution room after this year is: -
+
Your total lifetime contribution room left is:
{' '} diff --git a/src/_components/calculator-component/fhsa/fhsa-result-component.tsx b/src/_components/calculator-component/fhsa/fhsa-result-component.tsx index 19fe9a0b..c2f70753 100644 --- a/src/_components/calculator-component/fhsa/fhsa-result-component.tsx +++ b/src/_components/calculator-component/fhsa/fhsa-result-component.tsx @@ -1,16 +1,18 @@ -"use client" +'use client'; +// FHSA Calculator Example https://fhsacalculator.ca/fhsa-calculator/contribution-limit -const NEXT_YEAR = 2026 +const NEXT_YEAR = new Date().getFullYear() + 1; export default function FhsaResultComponent(values: number[]) { - const opened = NEXT_YEAR - values[0]; - const contributed = values[1]; - const contributionRoom = (opened * 8000) - contributed; - const totalRemaining = 40000 - contributionRoom - contributed; - document.getElementById("contributionRoom").textContent = contributionRoom.toString(); - document.getElementById("totalRemaining").textContent = totalRemaining.toString(); - return [contributionRoom, totalRemaining]; + const opened = NEXT_YEAR - values[0]; + const contributed = values[1]; + const contributionRoom = Math.min(opened * 8000 - contributed, 16000); - -} \ No newline at end of file + const totalRemaining = 40000 - contributed; + document.getElementById('contributionRoom').textContent = + contributionRoom.toString(); + document.getElementById('totalRemaining').textContent = + totalRemaining.toString(); + return [contributionRoom, totalRemaining]; +} diff --git a/src/_components/calculator-component/tfsa/tfsa-calculator-component.tsx b/src/_components/calculator-component/tfsa/tfsa-calculator-component.tsx index fabd783a..7e97bbfe 100644 --- a/src/_components/calculator-component/tfsa/tfsa-calculator-component.tsx +++ b/src/_components/calculator-component/tfsa/tfsa-calculator-component.tsx @@ -6,6 +6,8 @@ import React from 'react'; import TFSAvars from './tfsa-interface'; import tfsaMath from './tfsa-math'; +const currentYear = new Date().getFullYear(); + function TfsaCalculatorComponent() { const input = new TFSAvars(); const [checked, setChecked] = React.useState(false); @@ -59,9 +61,9 @@ function TfsaCalculatorComponent() { input.contributed.textInput.current ) { const temp = Number(input.born.textInput.current.value); - if (temp >= 1900 && temp < 2026) { + if (temp >= 1900 && temp <= currentYear) { element.textContent = null; - if (temp == 2007) { + if (temp == currentYear - 18) { setShowSwitch(true); } else setShowSwitch(false); setBornInputFlag(false); @@ -71,7 +73,7 @@ function TfsaCalculatorComponent() { } setBornInputFlag(true); } - element.textContent = 'Oops! Please enter a number between 1900-2025'; + element.textContent = `Oops! Please enter a number between 1900-${currentYear}`; } const handleResidentChange = () => { @@ -84,7 +86,10 @@ function TfsaCalculatorComponent() { return; } const temp = Number(input.resident.textInput.current.value); - if (temp >= Number(input.born.textInput.current.value) && temp < 2026) { + if ( + temp >= Number(input.born.textInput.current.value) && + temp <= currentYear + ) { setResidentInputFlag(false); element.textContent = null; diff --git a/src/_components/calculator-component/tfsa/tfsa-math.tsx b/src/_components/calculator-component/tfsa/tfsa-math.tsx index d167a417..2deb96ec 100644 --- a/src/_components/calculator-component/tfsa/tfsa-math.tsx +++ b/src/_components/calculator-component/tfsa/tfsa-math.tsx @@ -1,36 +1,39 @@ 'use client'; +// TFSA Calculation Sources +// Previous tax years https://www.empire.ca/how-use-canadian-tax-free-savings-account-tfsa/ +// Example of a working calculator https://www.theglobeandmail.com/investing/personal-finance/tools/tfsa-limit/ + export default function tfsaMath(inputArray: string[]) { if (inputArray[0] == 'keyword') return 0; - // eslint-disable-next-line prefer-const - let inputs = [ + + const currentYear = new Date().getFullYear(); + + const inputs = [ Math.floor(Number(inputArray[0])), Math.floor(Number(inputArray[1])), Math.floor(Number(inputArray[2])), Math.floor(Number(inputArray[3])), Math.floor(Number(inputArray[4])), ]; - // eslint-disable-next-line prefer-const - let yearlyRate = [ - 7000, 7000, 6500, 6000, 6000, 6000, 6000, 5500, 5500, 5500, 10000, 5500, - 5500, 5000, 5000, 5000, 5000, + const yearlyRate = [ + 7000, 7000, 7000, 6500, 6000, 6000, 6000, 6000, 5500, 5500, 5500, 10000, + 5500, 5500, 5000, 5000, 5000, 5000, ]; + + if (inputs[1] > currentYear - 18) return 0; + let total = 0; - if (inputs[1] > 2007) { - return 0; - } - // eslint-disable-next-line prefer-const - let ageAbove18 = 2025 - (inputs[1] + 18); - // eslint-disable-next-line prefer-const - let yearsAsResident = 2025 - inputs[0]; + const ageAbove18 = currentYear - (inputs[1] + 18); + const yearsAsResident = currentYear - inputs[0]; + let counter = ageAbove18 < yearsAsResident ? ageAbove18 : yearsAsResident; if (counter + 1 > yearlyRate.length) { - total = 102000; - } else { - while (counter >= 0) { - total = total + yearlyRate[counter]; - counter--; - } + counter = yearlyRate.length - 1; + } + while (counter >= 0) { + total = total + yearlyRate[counter]; + counter--; } total = total - inputs[2] + inputs[3] - inputs[4]; if (total < 0) total = 0;