Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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();
};
Expand Down Expand Up @@ -168,7 +169,7 @@ export default function FhsaCalculatorComponent() {
}
if (!AccountChecked) {
errorMsg.textContent = null;
FhsaResultComponent([2025, 0]);
FhsaResultComponent([currentYear, 0]);
setShowCalculation(true);
return;
}
Expand Down Expand Up @@ -271,9 +272,7 @@ export default function FhsaCalculatorComponent() {
Your contribution limit for this year is:
</div>
<div id="contributionRoom"></div>
<div>
Your total lifetime contribution room after this year is:
</div>
<div>Your total lifetime contribution room left is:</div>
<div id="totalRemaining"></div>
</div>{' '}
</div>
Expand Down
24 changes: 13 additions & 11 deletions src/_components/calculator-component/fhsa/fhsa-result-component.tsx
Original file line number Diff line number Diff line change
@@ -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);


}
const totalRemaining = 40000 - contributed;
document.getElementById('contributionRoom').textContent =
contributionRoom.toString();
document.getElementById('totalRemaining').textContent =
totalRemaining.toString();
return [contributionRoom, totalRemaining];
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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 = () => {
Expand All @@ -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;

Expand Down
41 changes: 22 additions & 19 deletions src/_components/calculator-component/tfsa/tfsa-math.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Loading