Skip to content

Commit

Permalink
Make buying inputs possibly undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
kacan98 committed Apr 16, 2024
1 parent afe5cfb commit 962efb1
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 54 deletions.
4 changes: 2 additions & 2 deletions public/i18n/cz/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
"Buying and selling costs": "Náklady na koupi a prodej",
"Buying and selling details": "Detaily koupě a prodeje",
"Total ownership costs": "Celkové náklady na vlastnictví",
"Simply ownership cost over the time of staying": "Jednoduše náklady na vlastnictví během doby pobytu",
"Capital from selling": "Kapitál z prodeje",
"Capital from selling details": "Detaily kapitálu z prodeje",
"Currency": "Měna",
Expand All @@ -71,5 +70,6 @@
"Auto": "Automatický",
"Light": "Světlý",
"Dark": "Tmavý",
"Mortgage vs Renting value": "Hodnota hypotéky vs nájem"
"Mortgage vs Renting value": "Hodnota hypotéky vs nájem",
"Buying and renting is equally expensive": "Koupě a nájem jsou stejně drahé"
}
4 changes: 2 additions & 2 deletions public/i18n/de/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
"Buying and selling costs": "Kauf- und Verkaufskosten",
"Buying and selling details": "Details zu Kauf und Verkauf",
"Total ownership costs": "Gesamte Besitzkosten",
"Simply ownership cost over the time of staying": "Einfache Besitzkosten über die Zeit des Aufenthalts",
"Capital from selling": "Kapital aus dem Verkauf",
"Capital from selling details": "Details zum Kapital aus dem Verkauf",
"Currency": "Währung",
Expand All @@ -71,5 +70,6 @@
"Auto": "Auto",
"Light": "Hell",
"Dark": "Dunkel",
"Mortgage vs Renting value": "Hypothek vs Mietwert"
"Mortgage vs Renting value": "Hypothek vs Mietwert",
"Buying and renting is equally expensive": "Kaufen und Mieten ist gleich teuer"
}
4 changes: 2 additions & 2 deletions public/i18n/dk/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
"Buying and selling costs": "Købs- og salgsomkostninger",
"Buying and selling details": "Detaljer om køb og salg",
"Total ownership costs": "Samlede ejerskabsomkostninger",
"Simply ownership cost over the time of staying": "Enkle ejerskabsomkostninger over opholdstiden",
"Capital from selling": "Kapital fra salg",
"Capital from selling details": "Detaljer om kapital fra salg",
"Currency": "Valuta",
Expand All @@ -71,5 +70,6 @@
"Auto": "Auto",
"Light": "Lys",
"Dark": "Mørk",
"Mortgage vs Renting value": "Pant kontra lejeværdi"
"Mortgage vs Renting value": "Pant kontra lejeværdi",
"Buying and renting is equally expensive": "Køb og leje er lige dyrt"
}
4 changes: 2 additions & 2 deletions public/i18n/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
"Buying and selling costs": "Buying and Selling Costs",
"Buying and selling details": "Buying and Selling Details",
"Total ownership costs": "Total Ownership Costs",
"Simply ownership cost over the time of staying": "Simply Ownership Cost over the Time of Staying",
"Capital from selling": "Capital from Selling",
"Capital from selling details": "Details of Capital from Selling",
"Currency": "Currency",
Expand All @@ -71,5 +70,6 @@
"Auto": "Auto",
"Light": "Light",
"Dark": "Dark",
"Mortgage vs Renting value": "Mortgage vs Renting Value"
"Mortgage vs Renting value": "Mortgage vs Renting Value",
"Buying and renting is equally expensive": "Buying and renting is equally expensive"
}
4 changes: 2 additions & 2 deletions public/i18n/ro/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
"Buying and selling costs": "Costuri de cumpărare și vânzare",
"Buying and selling details": "Detalii despre cumpărare și vânzare",
"Total ownership costs": "Costuri totale de proprietate",
"Simply ownership cost over the time of staying": "Simplu cost de proprietate pe durata șederii",
"Capital from selling": "Capital din vânzare",
"Capital from selling details": "Detalii capital din vânzare",
"Currency": "Monedă",
Expand All @@ -71,5 +70,6 @@
"Auto": "Auto",
"Light": "Luminos",
"Dark": "Întunecat",
"Mortgage vs Renting value": "Valoarea ipotecii vs închiriere"
"Mortgage vs Renting value": "Valoarea ipotecii vs închiriere",
"Buying and renting is equally expensive": "Cumpărarea și închirierea sunt la fel de scumpe"
}
4 changes: 2 additions & 2 deletions public/i18n/se/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
"Buying and selling costs": "Kostnader för köp och försäljning",
"Buying and selling details": "Detaljer om köp och försäljning",
"Total ownership costs": "Totala ägandekostnader",
"Simply ownership cost over the time of staying": "Enkla ägandekostnader över tiden för vistelsen",
"Capital from selling": "Kapital från försäljning",
"Capital from selling details": "Detaljer om kapital från försäljning",
"Currency": "Valuta",
Expand All @@ -71,5 +70,6 @@
"Auto": "Auto",
"Light": "Ljus",
"Dark": "Mörk",
"Mortgage vs Renting value": "Bolån vs Hyra värde"
"Mortgage vs Renting value": "Bolån vs Hyra värde",
"Buying and renting is equally expensive": "Köp och hyra är lika dyrt"
}
22 changes: 12 additions & 10 deletions src/components/inputs/blocks/buying.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ function Buying() {
useCalculatorSlice("buying");

const {
propertyPrice,
propertyPrice: price,
propertyValueGrowthPercentage,
loanTerm,
interestRate,
buyingCostsPercentage,
sellingCostsPercentage,
yearlyOwnershipCost,
depositPercentage,
depositPercentage: depositPct,
} = buyingState;
const propertyPrice = price || 0;
const depositPercentage = depositPct || 0;

const formatAsCurrency: (value: number) => string =
useLocaleCurrencyFormatter();
Expand All @@ -50,16 +52,16 @@ function Buying() {
const { totalPrincipalPaid, totalInterestPaid } = simulateTimePassage({
initialPropertyValue: propertyPrice,
depositPercentage,
yearlyOwnershipCost,
yearlyOwnershipCost: yearlyOwnershipCost || 0,

yearsStaying,
loanTerm,
yearsStaying: yearsStaying || 0,
loanTerm: loanTerm || 0,

interestRate,
loanAmount,
propertyValueGrowthPercentage,
buyingCostsPercentage,
sellingCostsPercentage,
interestRate: interestRate || 0,
loanAmount: loanAmount || 0,
propertyValueGrowthPercentage: propertyValueGrowthPercentage || 0,
buyingCostsPercentage: buyingCostsPercentage || 0,
sellingCostsPercentage: sellingCostsPercentage || 0,

mortgagePerMonth,
});
Expand Down
2 changes: 1 addition & 1 deletion src/components/inputs/blocks/renting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const Renting = () => {
const monthlyDifference = mortgagePerMonth - (rentingState.monthlyRent || 0);

const differenceBetweenRentAndMortgageDeposit =
mortgageDeposit - rentingState.initialInvestment;
mortgageDeposit - (rentingState.initialInvestment || 0);

return (
<>
Expand Down
5 changes: 1 addition & 4 deletions src/components/result/blocks/buyingResultBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function BuyingResultBlock() {
chart={<BuyingChart graphData={yearValueChanges} />}
heading={t("labelForYears", {
rentingOrBuying: t("Buying"),
yearsNumber: yearsStaying,
yearsNumber: yearsStaying || 0,
yearOrYears: yearsStaying !== 1 ? t("yearsPlural") : t("yearSingular"),
})}
rows={[
Expand Down Expand Up @@ -86,9 +86,6 @@ function BuyingResultBlock() {
{
label: t("Total ownership costs"),
value: -1 * totalOwnershipCosts,
tooltip: (
<div>{t("Simply ownership cost over the time of staying")}</div>
),
},
{
label: t("Capital from selling"),
Expand Down
5 changes: 3 additions & 2 deletions src/components/result/blocks/rentingResultBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ function RentingResultBlock() {
const { yearsStaying }: FuturePredictionsState = useSelector(
(state: RootState) => state.calculator.futurePredictions,
);
const { initialInvestment: rentDeposit }: RentingState = useSelector(
const { initialInvestment: deposit }: RentingState = useSelector(
(state: RootState) => state.calculator.renting,
);
const rentDeposit = deposit || 0;
const { totalRenting, rentTotal } = useRentDetails();

const alternativeInvestment = useAlternativeInvestmentReturns();
Expand All @@ -23,7 +24,7 @@ function RentingResultBlock() {
<BuyingOrSellingResultWrapper
heading={t("labelForYears", {
rentingOrBuying: t("Renting"),
yearsNumber: yearsStaying,
yearsNumber: yearsStaying || 0,
yearOrYears: yearsStaying !== 1 ? t("yearsPlural") : t("yearSingular"),
})}
rows={[
Expand Down
19 changes: 13 additions & 6 deletions src/components/result/blocks/resultBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,19 @@ function ResultBlock() {
textAlign: "left",
}}
>
<Typography variant={"h3"} gutterBottom>
{t("Buying/renting is cheaper by", {
buyingOrRenting: t(buyingIsBetter ? "Buying" : "Renting"),
difference: formatAsCurrency(Math.abs(difference)),
})}
</Typography>
{difference !== 0 ? (
<Typography variant={"h3"} gutterBottom>
{t("Buying/renting is cheaper by", {
buyingOrRenting: t(buyingIsBetter ? "Buying" : "Renting"),
difference: formatAsCurrency(Math.abs(difference)),
})}
</Typography>
) : (
<Typography variant={"h3"} gutterBottom>
{t("Buying and renting is equally expensive")}
</Typography>
)}

<ResultChart />
</Box>
);
Expand Down
18 changes: 9 additions & 9 deletions src/services/buying/useMortgageDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ export function useMortgageDetails(): MortgageDetails {
}: BuyingState = useSelector((state: RootState) => state.calculator.buying);

return calculateMortgageDetails({
yearsStaying,
depositPercentage,
initialPropertyValue: propertyPrice,
yearlyOwnershipCost,
loanTerm,
interestRate,
propertyValueGrowthPercentage,
buyingCostsPercentage,
sellingCostsPercentage,
yearsStaying: yearsStaying || 0,
depositPercentage: depositPercentage || 0,
initialPropertyValue: propertyPrice || 0,
yearlyOwnershipCost: yearlyOwnershipCost || 0,
loanTerm: loanTerm || 0,
interestRate: interestRate || 0,
propertyValueGrowthPercentage: propertyValueGrowthPercentage || 0,
buyingCostsPercentage: buyingCostsPercentage || 0,
sellingCostsPercentage: sellingCostsPercentage || 0,
});
}
2 changes: 1 addition & 1 deletion src/services/useAlternativeInvestment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function useAlternativeInvestmentReturns() {
calculateInvestmentReturns({
initialInvestment: initialCash,
investmentPerPeriod: moneyAvailablePerMonth * 12,
numberOfPeriods: yearsStaying,
numberOfPeriods: yearsStaying || 0,
appreciationRate: (alternativeInvestmentReturnPercentage || 0) / 100,
});

Expand Down
16 changes: 8 additions & 8 deletions store/calculatorSlices/buying.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { createSlice, PayloadAction } from "@reduxjs/toolkit";

export interface BuyingState {
propertyPrice: number;
depositPercentage: number;
yearlyOwnershipCost: number;
loanTerm: number;
interestRate: number;
buyingCostsPercentage: number;
sellingCostsPercentage: number;
propertyValueGrowthPercentage: number;
propertyPrice?: number;
depositPercentage?: number;
yearlyOwnershipCost?: number;
loanTerm?: number;
interestRate?: number;
buyingCostsPercentage?: number;
sellingCostsPercentage?: number;
propertyValueGrowthPercentage?: number;
}

const initialState: BuyingState = {
Expand Down
2 changes: 1 addition & 1 deletion store/calculatorSlices/futurePreditions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createSlice, PayloadAction } from "@reduxjs/toolkit";

export interface FuturePredictionsState {
yearsStaying: number;
yearsStaying?: number;
}

const initialState: FuturePredictionsState = {
Expand Down

0 comments on commit 962efb1

Please sign in to comment.