Reorganize helper lib (Move interfaces and types) * InfoItem * PortfolioItem * PortfolioOverview * PortfolioPerformance * Position * PortfolioPosition * PortfolioReport * PortfolioReportRule * User * UserSettings * DateRange * AdminData * AccessWithGranteeUser * OrderWithAccount * Granularity * UserWithSettings * RequestWithUser
117 lines
3.3 KiB
TypeScript
117 lines
3.3 KiB
TypeScript
import {
|
|
ChangeDetectionStrategy,
|
|
Component,
|
|
Input,
|
|
OnChanges
|
|
} from '@angular/core';
|
|
import { MatDialog } from '@angular/material/dialog';
|
|
import { Router } from '@angular/router';
|
|
import { LoginWithAccessTokenDialog } from '@ghostfolio/client/pages/login/login-with-access-token-dialog/login-with-access-token-dialog.component';
|
|
import { DataService } from '@ghostfolio/client/services/data.service';
|
|
import { ImpersonationStorageService } from '@ghostfolio/client/services/impersonation-storage.service';
|
|
import { TokenStorageService } from '@ghostfolio/client/services/token-storage.service';
|
|
import { InfoItem, User } from '@ghostfolio/helper/interfaces';
|
|
import { hasPermission, permissions } from '@ghostfolio/helper/permissions';
|
|
import { EMPTY, Subject } from 'rxjs';
|
|
import { catchError, takeUntil } from 'rxjs/operators';
|
|
|
|
@Component({
|
|
selector: 'gf-header',
|
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
templateUrl: './header.component.html',
|
|
styleUrls: ['./header.component.scss']
|
|
})
|
|
export class HeaderComponent implements OnChanges {
|
|
@Input() currentRoute: string;
|
|
@Input() info: InfoItem;
|
|
@Input() user: User;
|
|
|
|
public hasPermissionForSocialLogin: boolean;
|
|
public hasPermissionForSubscription: boolean;
|
|
public hasPermissionToAccessAdminControl: boolean;
|
|
public impersonationId: string;
|
|
|
|
private unsubscribeSubject = new Subject<void>();
|
|
|
|
public constructor(
|
|
private dataService: DataService,
|
|
private dialog: MatDialog,
|
|
private impersonationStorageService: ImpersonationStorageService,
|
|
private router: Router,
|
|
private tokenStorageService: TokenStorageService
|
|
) {
|
|
this.impersonationStorageService
|
|
.onChangeHasImpersonation()
|
|
.subscribe((id) => {
|
|
this.impersonationId = id;
|
|
});
|
|
}
|
|
|
|
public ngOnChanges() {
|
|
this.hasPermissionForSocialLogin = hasPermission(
|
|
this.info?.globalPermissions,
|
|
permissions.enableSocialLogin
|
|
);
|
|
|
|
this.hasPermissionForSubscription = hasPermission(
|
|
this.info?.globalPermissions,
|
|
permissions.enableSubscription
|
|
);
|
|
|
|
this.hasPermissionToAccessAdminControl = hasPermission(
|
|
this.user?.permissions,
|
|
permissions.accessAdminControl
|
|
);
|
|
}
|
|
|
|
public impersonateAccount(aId: string) {
|
|
if (aId) {
|
|
this.impersonationStorageService.setId(aId);
|
|
} else {
|
|
this.impersonationStorageService.removeId();
|
|
}
|
|
|
|
window.location.reload();
|
|
}
|
|
|
|
public onSignOut() {
|
|
this.tokenStorageService.signOut();
|
|
window.location.reload();
|
|
}
|
|
|
|
public openLoginDialog(): void {
|
|
const dialogRef = this.dialog.open(LoginWithAccessTokenDialog, {
|
|
autoFocus: false,
|
|
data: {
|
|
accessToken: '',
|
|
hasPermissionToUseSocialLogin: this.hasPermissionForSocialLogin
|
|
},
|
|
width: '30rem'
|
|
});
|
|
|
|
dialogRef.afterClosed().subscribe((data) => {
|
|
if (data?.accessToken) {
|
|
this.dataService
|
|
.loginAnonymous(data?.accessToken)
|
|
.pipe(
|
|
catchError(() => {
|
|
alert('Oops! Incorrect Security Token.');
|
|
|
|
return EMPTY;
|
|
}),
|
|
takeUntil(this.unsubscribeSubject)
|
|
)
|
|
.subscribe(({ authToken }) => {
|
|
this.setToken(authToken);
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
public setToken(aToken: string) {
|
|
this.tokenStorageService.saveToken(aToken);
|
|
|
|
this.router.navigate(['/']);
|
|
}
|
|
}
|