mirror of https://github.com/ghostfolio/ghostfolio
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
134 lines
3.8 KiB
134 lines
3.8 KiB
import { DataService } from '@ghostfolio/client/services/data.service';
|
|
import { ImpersonationStorageService } from '@ghostfolio/client/services/impersonation-storage.service';
|
|
import { UserService } from '@ghostfolio/client/services/user/user.service';
|
|
import {
|
|
InfoItem,
|
|
PortfolioSummary,
|
|
User
|
|
} from '@ghostfolio/common/interfaces';
|
|
import { hasPermission, permissions } from '@ghostfolio/common/permissions';
|
|
|
|
import { ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core';
|
|
import {
|
|
MatSnackBar,
|
|
MatSnackBarRef,
|
|
TextOnlySnackBar
|
|
} from '@angular/material/snack-bar';
|
|
import { Router } from '@angular/router';
|
|
import { Subject } from 'rxjs';
|
|
import { takeUntil } from 'rxjs/operators';
|
|
|
|
@Component({
|
|
selector: 'gf-home-summary',
|
|
styleUrls: ['./home-summary.scss'],
|
|
templateUrl: './home-summary.html'
|
|
})
|
|
export class HomeSummaryComponent implements OnDestroy, OnInit {
|
|
public hasImpersonationId: boolean;
|
|
public hasPermissionForSubscription: boolean;
|
|
public hasPermissionToUpdateUserSettings: boolean;
|
|
public info: InfoItem;
|
|
public isLoading = true;
|
|
public snackBarRef: MatSnackBarRef<TextOnlySnackBar>;
|
|
public summary: PortfolioSummary;
|
|
public user: User;
|
|
|
|
private unsubscribeSubject = new Subject<void>();
|
|
|
|
public constructor(
|
|
private changeDetectorRef: ChangeDetectorRef,
|
|
private dataService: DataService,
|
|
private impersonationStorageService: ImpersonationStorageService,
|
|
private router: Router,
|
|
private snackBar: MatSnackBar,
|
|
private userService: UserService
|
|
) {
|
|
this.info = this.dataService.fetchInfo();
|
|
|
|
this.hasPermissionForSubscription = hasPermission(
|
|
this.info?.globalPermissions,
|
|
permissions.enableSubscription
|
|
);
|
|
|
|
this.userService.stateChanged
|
|
.pipe(takeUntil(this.unsubscribeSubject))
|
|
.subscribe((state) => {
|
|
if (state?.user) {
|
|
this.user = state.user;
|
|
|
|
this.hasPermissionToUpdateUserSettings = hasPermission(
|
|
this.user.permissions,
|
|
permissions.updateUserSettings
|
|
);
|
|
|
|
this.update();
|
|
}
|
|
});
|
|
}
|
|
|
|
public ngOnInit() {
|
|
this.impersonationStorageService
|
|
.onChangeHasImpersonation()
|
|
.pipe(takeUntil(this.unsubscribeSubject))
|
|
.subscribe((impersonationId) => {
|
|
this.hasImpersonationId = !!impersonationId;
|
|
});
|
|
}
|
|
|
|
public onChangeEmergencyFund(emergencyFund: number) {
|
|
this.dataService
|
|
.putUserSetting({ emergencyFund })
|
|
.pipe(takeUntil(this.unsubscribeSubject))
|
|
.subscribe(() => {
|
|
this.userService.remove();
|
|
|
|
this.userService
|
|
.get()
|
|
.pipe(takeUntil(this.unsubscribeSubject))
|
|
.subscribe((user) => {
|
|
this.user = user;
|
|
|
|
this.changeDetectorRef.markForCheck();
|
|
});
|
|
});
|
|
}
|
|
|
|
public ngOnDestroy() {
|
|
this.unsubscribeSubject.next();
|
|
this.unsubscribeSubject.complete();
|
|
}
|
|
|
|
private update() {
|
|
this.isLoading = true;
|
|
|
|
this.dataService
|
|
.fetchPortfolioDetails()
|
|
.pipe(takeUntil(this.unsubscribeSubject))
|
|
.subscribe(({ summary }) => {
|
|
this.summary = summary;
|
|
this.isLoading = false;
|
|
|
|
if (!this.summary) {
|
|
this.snackBarRef = this.snackBar.open(
|
|
$localize`This feature requires a subscription.`,
|
|
this.hasPermissionForSubscription
|
|
? $localize`Upgrade Plan`
|
|
: undefined,
|
|
{ duration: 6000 }
|
|
);
|
|
|
|
this.snackBarRef.afterDismissed().subscribe(() => {
|
|
this.snackBarRef = undefined;
|
|
});
|
|
|
|
this.snackBarRef.onAction().subscribe(() => {
|
|
this.router.navigate(['/' + $localize`pricing`]);
|
|
});
|
|
}
|
|
|
|
this.changeDetectorRef.markForCheck();
|
|
});
|
|
|
|
this.changeDetectorRef.markForCheck();
|
|
}
|
|
}
|
|
|