Browse Source

Task/improve type safety in fire page component (#6807)

* Improve type safety
pull/5570/merge
Kenrick Tandrian 2 weeks ago
committed by GitHub
parent
commit
a04099b12d
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 121
      apps/client/src/app/pages/portfolio/fire/fire-page.component.ts
  2. 2
      apps/client/src/app/pages/portfolio/fire/fire-page.html

121
apps/client/src/app/pages/portfolio/fire/fire-page.component.ts

@ -16,7 +16,9 @@ import { CommonModule, NgStyle } from '@angular/common';
import { import {
ChangeDetectorRef, ChangeDetectorRef,
Component, Component,
computed,
DestroyRef, DestroyRef,
inject,
OnInit OnInit
} from '@angular/core'; } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
@ -42,33 +44,40 @@ import { NgxSkeletonLoaderModule } from 'ngx-skeleton-loader';
templateUrl: './fire-page.html' templateUrl: './fire-page.html'
}) })
export class GfFirePageComponent implements OnInit { export class GfFirePageComponent implements OnInit {
public deviceType: string; protected readonly deviceType = computed(
public fireWealth: FireWealth; () => this.deviceDetectorService.deviceInfo().deviceType
public hasImpersonationId: boolean; );
public hasPermissionToUpdateUserSettings: boolean;
public isLoading = false; protected fireWealth: FireWealth;
public projectedTotalAmount: number; protected hasImpersonationId: boolean;
public retirementDate: Date; protected hasPermissionToUpdateUserSettings: boolean;
public safeWithdrawalRateControl = new FormControl<number>(undefined); protected isLoading = false;
public safeWithdrawalRateOptions = [0.025, 0.03, 0.035, 0.04, 0.045]; protected retirementDate: Date;
public user: User; protected readonly safeWithdrawalRateControl = new FormControl<
public withdrawalRatePerMonth: Big; number | undefined
public withdrawalRatePerMonthProjected: Big; >(undefined);
public withdrawalRatePerYear: Big; protected readonly safeWithdrawalRateOptions = [
public withdrawalRatePerYearProjected: Big; 0.025, 0.03, 0.035, 0.04, 0.045
] as const;
public constructor( protected user: User;
private changeDetectorRef: ChangeDetectorRef, protected withdrawalRatePerMonth: Big;
private dataService: DataService, protected withdrawalRatePerMonthProjected: Big;
private destroyRef: DestroyRef, protected withdrawalRatePerYear: Big;
private deviceService: DeviceDetectorService, protected withdrawalRatePerYearProjected: Big;
private impersonationStorageService: ImpersonationStorageService,
private userService: UserService private projectedTotalAmount: number;
) {}
private readonly changeDetectorRef = inject(ChangeDetectorRef);
private readonly dataService = inject(DataService);
private readonly destroyRef = inject(DestroyRef);
private readonly deviceDetectorService = inject(DeviceDetectorService);
private readonly impersonationStorageService = inject(
ImpersonationStorageService
);
private readonly userService = inject(UserService);
public ngOnInit() { public ngOnInit() {
this.isLoading = true; this.isLoading = true;
this.deviceType = this.deviceService.getDeviceInfo().deviceType;
this.dataService this.dataService
.fetchPortfolioDetails() .fetchPortfolioDetails()
@ -76,7 +85,7 @@ export class GfFirePageComponent implements OnInit {
.subscribe(({ summary }) => { .subscribe(({ summary }) => {
this.fireWealth = { this.fireWealth = {
today: { today: {
valueInBaseCurrency: summary.fireWealth valueInBaseCurrency: summary?.fireWealth
? summary.fireWealth.today.valueInBaseCurrency ? summary.fireWealth.today.valueInBaseCurrency
: 0 : 0
} }
@ -104,7 +113,7 @@ export class GfFirePageComponent implements OnInit {
this.safeWithdrawalRateControl.valueChanges this.safeWithdrawalRateControl.valueChanges
.pipe(takeUntilDestroyed(this.destroyRef)) .pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((value) => { .subscribe((value) => {
this.onSafeWithdrawalRateChange(Number(value)); this.updateSafeWithdrawalRate(Number(value));
}); });
this.userService.stateChanged this.userService.stateChanged
@ -133,7 +142,7 @@ export class GfFirePageComponent implements OnInit {
}); });
} }
public onAnnualInterestRateChange(annualInterestRate: number) { protected onAnnualInterestRateChange(annualInterestRate: number) {
this.dataService this.dataService
.putUserSetting({ annualInterestRate }) .putUserSetting({ annualInterestRate })
.pipe(takeUntilDestroyed(this.destroyRef)) .pipe(takeUntilDestroyed(this.destroyRef))
@ -149,7 +158,7 @@ export class GfFirePageComponent implements OnInit {
}); });
} }
public onCalculationComplete({ protected onCalculationComplete({
projectedTotalAmount, projectedTotalAmount,
retirementDate retirementDate
}: FireCalculationCompleteEvent) { }: FireCalculationCompleteEvent) {
@ -161,11 +170,11 @@ export class GfFirePageComponent implements OnInit {
this.isLoading = false; this.isLoading = false;
} }
public onRetirementDateChange(retirementDate: Date) { protected onProjectedTotalAmountChange(projectedTotalAmount: number) {
this.dataService this.dataService
.putUserSetting({ .putUserSetting({
retirementDate: retirementDate.toISOString(), projectedTotalAmount,
projectedTotalAmount: null retirementDate: undefined
}) })
.pipe(takeUntilDestroyed(this.destroyRef)) .pipe(takeUntilDestroyed(this.destroyRef))
.subscribe(() => { .subscribe(() => {
@ -180,9 +189,12 @@ export class GfFirePageComponent implements OnInit {
}); });
} }
public onSafeWithdrawalRateChange(safeWithdrawalRate: number) { protected onRetirementDateChange(retirementDate: Date) {
this.dataService this.dataService
.putUserSetting({ safeWithdrawalRate }) .putUserSetting({
projectedTotalAmount: undefined,
retirementDate: retirementDate.toISOString()
})
.pipe(takeUntilDestroyed(this.destroyRef)) .pipe(takeUntilDestroyed(this.destroyRef))
.subscribe(() => { .subscribe(() => {
this.userService this.userService
@ -191,15 +203,12 @@ export class GfFirePageComponent implements OnInit {
.subscribe((user) => { .subscribe((user) => {
this.user = user; this.user = user;
this.calculateWithdrawalRates();
this.calculateWithdrawalRatesProjected();
this.changeDetectorRef.markForCheck(); this.changeDetectorRef.markForCheck();
}); });
}); });
} }
public onSavingsRateChange(savingsRate: number) { protected onSavingsRateChange(savingsRate: number) {
this.dataService this.dataService
.putUserSetting({ savingsRate }) .putUserSetting({ savingsRate })
.pipe(takeUntilDestroyed(this.destroyRef)) .pipe(takeUntilDestroyed(this.destroyRef))
@ -215,25 +224,6 @@ export class GfFirePageComponent implements OnInit {
}); });
} }
public onProjectedTotalAmountChange(projectedTotalAmount: number) {
this.dataService
.putUserSetting({
projectedTotalAmount,
retirementDate: null
})
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe(() => {
this.userService
.get(true)
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((user) => {
this.user = user;
this.changeDetectorRef.markForCheck();
});
});
}
private calculateWithdrawalRates() { private calculateWithdrawalRates() {
if (this.fireWealth && this.user?.settings?.safeWithdrawalRate) { if (this.fireWealth && this.user?.settings?.safeWithdrawalRate) {
this.withdrawalRatePerYear = new Big( this.withdrawalRatePerYear = new Big(
@ -258,4 +248,23 @@ export class GfFirePageComponent implements OnInit {
this.withdrawalRatePerYearProjected.div(12); this.withdrawalRatePerYearProjected.div(12);
} }
} }
private updateSafeWithdrawalRate(safeWithdrawalRate: number) {
this.dataService
.putUserSetting({ safeWithdrawalRate })
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe(() => {
this.userService
.get(true)
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((user) => {
this.user = user;
this.calculateWithdrawalRates();
this.calculateWithdrawalRatesProjected();
this.changeDetectorRef.markForCheck();
});
});
}
} }

2
apps/client/src/app/pages/portfolio/fire/fire-page.html

@ -13,7 +13,7 @@
[annualInterestRate]="user?.settings?.annualInterestRate" [annualInterestRate]="user?.settings?.annualInterestRate"
[colorScheme]="user?.settings?.colorScheme" [colorScheme]="user?.settings?.colorScheme"
[currency]="user?.settings?.baseCurrency" [currency]="user?.settings?.baseCurrency"
[deviceType]="deviceType" [deviceType]="deviceType()"
[fireWealth]="fireWealth?.today.valueInBaseCurrency" [fireWealth]="fireWealth?.today.valueInBaseCurrency"
[hasPermissionToUpdateUserSettings]=" [hasPermissionToUpdateUserSettings]="
!hasImpersonationId && hasPermissionToUpdateUserSettings !hasImpersonationId && hasPermissionToUpdateUserSettings

Loading…
Cancel
Save