Skip to content

Commit

Permalink
chore: Setup linting on project (#41)
Browse files Browse the repository at this point in the history
* chore: Setup linting rules

* fix: Linting issues

* fix: Outstanding linting fixes

* chore: Tidy up eslint

* chore: Run prettier after rebase

* fix: Add back `socialRentTotalEarning`
- Please see https://github.com/theopensystemslab/fairhold-dashboard/pull/41\#discussion_r1701743583
  • Loading branch information
DafyddLlyr authored Aug 7, 2024
1 parent 4d73932 commit 75e1aec
Show file tree
Hide file tree
Showing 19 changed files with 172 additions and 133 deletions.
12 changes: 11 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
{
"extends": ["next/core-web-vitals", "prettier"]
"extends": [
"next/core-web-vitals",
"prettier",
"plugin:@typescript-eslint/recommended"
],
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
}
}
13 changes: 7 additions & 6 deletions app/api/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export async function POST(req: Request) {

// Calculate the total price if (!pricesPaid) throw Error("Prices fetching failed");
const totalPrice = pricesPaid.reduce(
(total: number, item: any) => total + item.price,
(total, item) => total + item.price,
0
);
console.log("totalPrice:", totalPrice);
Expand Down Expand Up @@ -216,11 +216,12 @@ export async function POST(req: Request) {
if (socialRentEarningRes.length === 1) {
socialRentAverageEarning = socialRentEarningRes[0].earningsperweek;
} else if (socialRentEarningRes.length > 1) {
const socialRentTotalEarning = socialRentEarningRes.reduce(
(sum, item) => sum + item.earningsperweek,
0
);
socialRentAverageEarning = totalRent / socialRentEarningRes.length;
const socialRentTotalEarning = socialRentEarningRes.reduce(
(sum, item) => sum + item.earningsperweek,
0
);
socialRentAverageEarning =
socialRentTotalEarning / socialRentEarningRes.length;
}
console.log("socialRentAverageEarning: ", socialRentAverageEarning);

Expand Down
2 changes: 0 additions & 2 deletions app/components/graphs/LifetimeCombinedChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import {
Chart,
ChartData,
ChartDataset,
ChartOptions,
ChartTypeRegistry,
PointElement,
BarElement,
BarController,
Expand Down
2 changes: 0 additions & 2 deletions app/components/graphs/TotalPaymentBarChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import {
BarController,
CategoryScale,
LinearScale,
Tooltip,
Legend,
} from "chart.js";

Chart.register(BarElement, BarController, CategoryScale, LinearScale);
Expand Down
5 changes: 2 additions & 3 deletions app/components/ui/CalculatorInput.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use client";
import { trueDependencies } from "mathjs";
import React, { useState } from "react";
import React, { FormEvent, useState } from "react";
import calculateFairhold from "@/app/models/testClasses";
import { Household } from "@/app/models/Household";
import Dashboard from "./Dashboard";
Expand All @@ -22,7 +21,7 @@ const CalculatorInput = () => {
const [houseAge, setHouseAge] = useState(""); // variables associated to the house age

// fucntion that defines what happens after submitting the form
async function handleSubmit(e: any) {
async function handleSubmit(e: FormEvent<HTMLFormElement>) {
e.preventDefault(); // pr event the default of the form
const formData = new FormData(e.currentTarget); // get the data in the form, e.g postcode, house size etc
const data = Object.fromEntries(formData.entries());
Expand Down
2 changes: 1 addition & 1 deletion app/models/Mortgage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class Mortgage {
let remainingBalance =
this.totalMortgageCost - this.monthlyPayment * MONTHS_PER_YEAR;

let yearlyPaymentBreakdown: MortgageBreakdown = [
const yearlyPaymentBreakdown: MortgageBreakdown = [
{
yearlyPayment: yearlyPayment,
cumulativePaid: cumulativePaid,
Expand Down
3 changes: 1 addition & 2 deletions app/models/Property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type PropertyParams = Pick<

export const HOUSE_TYPES = ["D", "S", "T", "F"] as const;

type HouseType = (typeof HOUSE_TYPES)[number];
export type HouseType = (typeof HOUSE_TYPES)[number];

export class Property {
postcode: string;
Expand Down Expand Up @@ -91,7 +91,6 @@ export class Property {
}

private calculateBedWeightedAveragePrice() {
const beds = BED_WEIGHTS_AND_CAPS.numberOfBedrooms;
const bedWeights = BED_WEIGHTS_AND_CAPS.weight;
let bedWeight;

Expand Down
22 changes: 0 additions & 22 deletions app/models/tenure/FairholdLandPurchase.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,10 @@
import { Fairhold } from "../Fairhold";
import { DEFAULT_FORECAST_PARAMETERS } from "../ForecastParameters";
import { Property } from "../Property";
import { FairholdLandPurchase } from "./FairholdLandPurchase";
import { MarketPurchase } from "./MarketPurchase";

let tenureFairholdLandPurchase: FairholdLandPurchase;

beforeEach(() => {
let property = new Property({
postcode: "WV8 1HG",
houseType: "T",
numberOfBedrooms: 2,
age: 10,
size: 88,
newBuildPricePerMetre: 2120,
averageMarketPrice: 218091.58,
itl3: "TLG24",
});

let tenureMarketPurchase = new MarketPurchase({
incomeYearly: 45816,
averagePrice: 218091.58,
newBuildPrice: 186560,
depreciatedBuildPrice: 110717.45,
landPrice: 31531.579,
...DEFAULT_FORECAST_PARAMETERS,
});

tenureFairholdLandPurchase = new FairholdLandPurchase({
//incomeYearly: 45816,
//averagePrice: 218091.58,
Expand Down
2 changes: 1 addition & 1 deletion app/models/tenure/FairholdLandPurchase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class FairholdLandPurchase {
let landMortgagePaymentYearlyIterative =
landMortgagePaymentYearly[0].yearlyPayment;

let lifetime: Lifetime = [
const lifetime: Lifetime = [
{
maintenanceCost: maintenanceCostIterative,
landMortgagePaymentYearly: landMortgagePaymentYearlyIterative,
Expand Down
23 changes: 0 additions & 23 deletions app/models/tenure/FairholdLandRent.test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,10 @@
import { Fairhold } from "../Fairhold";
import { DEFAULT_FORECAST_PARAMETERS } from "../ForecastParameters";
import { Property } from "../Property";
import { FairholdLandRent } from "./FairholdLandRent";
import { MarketRent } from "./MarketRent";

let tenureFairholdLandRent: FairholdLandRent;

beforeEach(() => {
let property = new Property({
postcode: "WV8 1HG",
houseType: "T",
numberOfBedrooms: 2,
age: 10,
size: 88,
newBuildPricePerMetre: 2120,
averageMarketPrice: 218091.58,
itl3: "TLG24",
});

let tenureMarketRent = new MarketRent({
averageRentYearly: 20000,
incomeYearly: 45816,
averagePrice: 218091.58,
newBuildPrice: 186560,
depreciatedBuildPrice: 110717.45,
landPrice: 31531.579,
...DEFAULT_FORECAST_PARAMETERS,
});

tenureFairholdLandRent = new FairholdLandRent({
averageRentYearly: 20000,
incomeYearly: 45816,
Expand Down
11 changes: 6 additions & 5 deletions app/models/tenure/FairholdLandRent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ export class FairholdLandRent {
let averageRentYearlyIterative = averageRentYearly;
let averageRentLandYearlyIterative =
averageRentYearlyIterative * landToTotalRatioIterative;
let affordabilityIterative = averageRentYearlyIterative / incomeIterative;
const affordabilityIterative = averageRentYearlyIterative / incomeIterative;
let maintenanceCostIterative =
maintenanceCostPercentage * newBuildPriceIterative;

let fairholdRentLandIterative = new Fairhold({
const fairholdRentLandIterative = new Fairhold({
affordability: affordabilityIterative,
landPriceOrRent: averageRentLandYearlyIterative / MONTHS_PER_YEAR,
}).discountedLandPriceOrRent;
Expand All @@ -100,7 +100,7 @@ export class FairholdLandRent {
let houseMortgagePaymentYearlyIterative =
houseMortgagePaymentYearly[0].yearlyPayment; // find the first year

let lifetime: Lifetime = [
const lifetime: Lifetime = [
{
maintenanceCost: maintenanceCostIterative,
fairholdRentLand: fairholdRentLandIterative,
Expand All @@ -125,9 +125,10 @@ export class FairholdLandRent {
averageRentLandYearlyIterative =
averageRentYearlyIterative * landToTotalRatioIterative;

let affordabilityIterative = averageRentYearlyIterative / incomeIterative;
const affordabilityIterative =
averageRentYearlyIterative / incomeIterative;

let fairholdRentLandIterative = new Fairhold({
const fairholdRentLandIterative = new Fairhold({
affordability: affordabilityIterative,
landPriceOrRent: averageRentLandYearlyIterative,
}).discountedLandPriceOrRent;
Expand Down
14 changes: 1 addition & 13 deletions app/models/tenure/MarketPurchase.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Property } from "../Property";
import { MarketPurchase } from "./MarketPurchase";

let tenureMarketPurchase: MarketPurchase;

beforeEach(() => {
let forecastParameters = {
const forecastParameters = {
maintenanceCostPercentage: 0.0125, // percentage maintenance cost
incomeGrowthPerYear: 0.04, // 4% income growth per year
constructionPriceGrowthPerYear: 0.025, // 2.5%
Expand All @@ -14,17 +13,6 @@ beforeEach(() => {
affordabilityThresholdIncomePercentage: 0.35, // percentage of imcome to afford rent or purchase
};

let property = new Property({
postcode: "WV8 1HG",
houseType: "T",
numberOfBedrooms: 2,
age: 10,
size: 88,
newBuildPricePerMetre: 2120,
averageMarketPrice: 218091.58,
itl3: "TLG24",
});

tenureMarketPurchase = new MarketPurchase({
incomeYearly: 45816,
averagePrice: 218091.58,
Expand Down
4 changes: 2 additions & 2 deletions app/models/tenure/MarketPurchase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ export class MarketPurchase {
const houseMortgagePaymentYearly =
this.houseMortgage.yearlyPaymentBreakdown;
const landMortgagePaymentYearly = this.landMortgage.yearlyPaymentBreakdown;

// Find the first year
let houseMortgagePaymentYearlyIterative =
houseMortgagePaymentYearly[0].yearlyPayment;
let landMortgagePaymentYearlyIterative =
landMortgagePaymentYearly[0].yearlyPayment;
landMortgagePaymentYearly[0].yearlyPayment;

const lifetime: Lifetime = [
{
Expand Down
14 changes: 1 addition & 13 deletions app/models/tenure/MarketRent.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Property } from "../Property";
import { MarketRent } from "./MarketRent";

let tenureMarketRent: MarketRent;

beforeEach(() => {
let forecastParameters = {
const forecastParameters = {
maintenanceCostPercentage: 0.0125, // percentage maintenance cost
incomeGrowthPerYear: 0.04, // 4% income growth per year
constructionPriceGrowthPerYear: 0.025, // 2.5%
Expand All @@ -14,17 +13,6 @@ beforeEach(() => {
affordabilityThresholdIncomePercentage: 0.35, // percentage of imcome to afford rent or purchase
};

let property = new Property({
postcode: "WV8 1HG",
houseType: "T",
numberOfBedrooms: 2,
age: 10,
size: 88,
newBuildPricePerMetre: 2120,
averageMarketPrice: 218091.58,
itl3: "TLG24",
});

tenureMarketRent = new MarketRent({
averageRentYearly: 20000,
incomeYearly: 45816,
Expand Down
2 changes: 1 addition & 1 deletion app/models/tenure/MarketRent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class MarketRent {
let averageRentHouseYearlyIterative =
averageRentYearlyIterative - averageRentLandYearlyIterative;

let lifetime: Lifetime = [
const lifetime: Lifetime = [
{
averageRentLandYearly: averageRentLandYearlyIterative,
averageRentHouseYearly: averageRentHouseYearlyIterative,
Expand Down
24 changes: 1 addition & 23 deletions app/models/tenure/SocialRent.test.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,9 @@
import { Property } from "../Property";
import { SocialRent, SocialRentAdjustments } from "./SocialRent";

let tenureSocialRent: SocialRent;

beforeEach(() => {
let forecastParameters = {
maintenanceCostPercentage: 0.0125, // percentage maintenance cost
incomeGrowthPerYear: 0.04, // 4% income growth per year
constructionPriceGrowthPerYear: 0.025, // 2.5%
rentGrowthPerYear: 0.03, // 3%
propertyPriceGrowthPerYear: 0.05, // 5%
yearsForecast: 40, // 40 years
affordabilityThresholdIncomePercentage: 0.35, // percentage of imcome to afford rent or purchase
};

let property = new Property({
postcode: "WV8 1HG",
houseType: "T",
numberOfBedrooms: 2,
age: 10,
size: 88,
newBuildPricePerMetre: 2120,
averageMarketPrice: 218091.58,
itl3: "TLG24",
});

let socialRentAdjustments: SocialRentAdjustments = [
const socialRentAdjustments: SocialRentAdjustments = [
{
inflation: 3.3,
total: 4.3,
Expand Down
29 changes: 25 additions & 4 deletions app/models/testClasses.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
import { DEFAULT_FORECAST_PARAMETERS } from "./ForecastParameters";
import { Household } from "./Household";
import { Property } from "./Property";
import { HouseType, Property } from "./Property";
import { MONTHS_PER_YEAR } from "./constants";
import { SocialRentAdjustments } from "./tenure/SocialRent";

function calculateFairhold(responseData: any) {
if (!responseData.buildPrice || responseData.buildPrice.length === 0) {
// TODO: Share type with backend
export interface ResponseData {
postcode: string;
houseType: HouseType;
houseBedrooms: number;
buildPrice: number;
houseAge: number;
houseSize: number;
newBuildPricePerMetre: number;
averagePrice: number;
itl3: string;
gdhi: number;
averageRentMonthly: number;
socialRentAverageEarning: number;
socialRentAdjustments: SocialRentAdjustments;
hpi: number;
gasBillYearly: number;
}

function calculateFairhold(responseData: ResponseData) {
if (!responseData.buildPrice) {
throw new Error("buildPrice data is missing or empty");
}
if (!responseData.itl3 || responseData.itl3.length === 0) {
Expand All @@ -25,7 +46,7 @@ function calculateFairhold(responseData: any) {
// define the household object
const household = new Household({
incomePerPersonYearly: responseData.gdhi,
averageRentYearly: responseData.averageRentMonthly * 12,
averageRentYearly: responseData.averageRentMonthly * MONTHS_PER_YEAR,
socialRentAverageEarning: responseData.socialRentAverageEarning,
socialRentAdjustments: responseData.socialRentAdjustments,
housePriceIndex: responseData.hpi,
Expand Down
Loading

0 comments on commit 75e1aec

Please sign in to comment.