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.
107 lines
3.2 KiB
107 lines
3.2 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 { PortfolioPosition, User } from '@ghostfolio/common/interfaces';
|
|
import { hasPermission, permissions } from '@ghostfolio/common/permissions';
|
|
import { HoldingType, ToggleOption } from '@ghostfolio/common/types';
|
|
|
|
import { ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core';
|
|
import { DeviceDetectorService } from 'ngx-device-detector';
|
|
import { Subject } from 'rxjs';
|
|
import { takeUntil } from 'rxjs/operators';
|
|
|
|
@Component({
|
|
selector: 'gf-home-holdings',
|
|
styleUrls: ['./home-holdings.scss'],
|
|
templateUrl: './home-holdings.html'
|
|
})
|
|
export class HomeHoldingsComponent implements OnDestroy, OnInit {
|
|
public deviceType: string;
|
|
public hasImpersonationId: boolean;
|
|
public hasPermissionToCreateOrder: boolean;
|
|
public holdings: PortfolioPosition[];
|
|
public holdingType: HoldingType = 'ACTIVE';
|
|
public holdingTypeOptions: ToggleOption[] = [
|
|
{ label: $localize`Active`, value: 'ACTIVE' },
|
|
{ label: $localize`Closed`, value: 'CLOSED' }
|
|
];
|
|
public user: User;
|
|
|
|
private unsubscribeSubject = new Subject<void>();
|
|
|
|
public constructor(
|
|
private changeDetectorRef: ChangeDetectorRef,
|
|
private dataService: DataService,
|
|
private deviceService: DeviceDetectorService,
|
|
private impersonationStorageService: ImpersonationStorageService,
|
|
private userService: UserService
|
|
) {}
|
|
|
|
public ngOnInit() {
|
|
this.deviceType = this.deviceService.getDeviceInfo().deviceType;
|
|
|
|
this.impersonationStorageService
|
|
.onChangeHasImpersonation()
|
|
.pipe(takeUntil(this.unsubscribeSubject))
|
|
.subscribe((impersonationId) => {
|
|
this.hasImpersonationId = !!impersonationId;
|
|
});
|
|
|
|
this.userService.stateChanged
|
|
.pipe(takeUntil(this.unsubscribeSubject))
|
|
.subscribe((state) => {
|
|
if (state?.user) {
|
|
this.user = state.user;
|
|
|
|
this.hasPermissionToCreateOrder = hasPermission(
|
|
this.user.permissions,
|
|
permissions.createOrder
|
|
);
|
|
|
|
this.holdings = undefined;
|
|
|
|
this.fetchHoldings()
|
|
.pipe(takeUntil(this.unsubscribeSubject))
|
|
.subscribe(({ holdings }) => {
|
|
this.holdings = holdings;
|
|
|
|
this.changeDetectorRef.markForCheck();
|
|
});
|
|
|
|
this.changeDetectorRef.markForCheck();
|
|
}
|
|
});
|
|
}
|
|
|
|
public onChangeHoldingType(aHoldingType: HoldingType) {
|
|
this.holdingType = aHoldingType;
|
|
|
|
this.holdings = undefined;
|
|
|
|
this.fetchHoldings()
|
|
.pipe(takeUntil(this.unsubscribeSubject))
|
|
.subscribe(({ holdings }) => {
|
|
this.holdings = holdings;
|
|
|
|
this.changeDetectorRef.markForCheck();
|
|
});
|
|
}
|
|
|
|
public ngOnDestroy() {
|
|
this.unsubscribeSubject.next();
|
|
this.unsubscribeSubject.complete();
|
|
}
|
|
|
|
private fetchHoldings() {
|
|
const filters = this.userService.getFilters();
|
|
|
|
if (this.holdingType === 'CLOSED') {
|
|
filters.push({ id: 'CLOSED', type: 'HOLDING_TYPE' });
|
|
}
|
|
|
|
return this.dataService.fetchPortfolioHoldings({
|
|
filters,
|
|
range: this.user?.settings?.dateRange
|
|
});
|
|
}
|
|
}
|
|
|