2022-03-03 15:44:22 +01:00
|
|
|
import { Component, OnDestroy, OnInit } from '@angular/core';
|
2022-03-03 13:38:39 +01:00
|
|
|
import { Router } from '@angular/router';
|
2022-03-03 15:44:22 +01:00
|
|
|
import { AutoUnsubscribe } from 'ngx-auto-unsubscribe-decorator';
|
|
|
|
|
import { EUser } from 'picsur-shared/dist/entities/user.entity';
|
|
|
|
|
import { HasFailed } from 'picsur-shared/dist/types';
|
|
|
|
|
import { UserService } from 'src/app/api/user.service';
|
|
|
|
|
import { SnackBarType } from 'src/app/models/snack-bar-type';
|
|
|
|
|
import { UtilService } from 'src/app/util/util.service';
|
2022-02-28 21:09:27 +01:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'app-header',
|
|
|
|
|
templateUrl: './header.component.html',
|
|
|
|
|
styleUrls: ['./header.component.scss'],
|
|
|
|
|
})
|
2022-03-03 15:44:22 +01:00
|
|
|
export class HeaderComponent implements OnInit {
|
|
|
|
|
private currentUser: EUser | null = null;
|
|
|
|
|
|
|
|
|
|
public get user() {
|
|
|
|
|
return this.currentUser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get isLoggedIn() {
|
|
|
|
|
return this.currentUser !== null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
private router: Router,
|
|
|
|
|
private userService: UserService,
|
|
|
|
|
private utilService: UtilService
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
|
|
|
|
this.subscribeUser();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@AutoUnsubscribe()
|
|
|
|
|
subscribeUser() {
|
|
|
|
|
return this.userService.liveUser.subscribe((user) => {
|
|
|
|
|
console.log('user', user);
|
|
|
|
|
this.currentUser = user;
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-03-03 13:38:39 +01:00
|
|
|
|
|
|
|
|
doLogin() {
|
|
|
|
|
this.router.navigate(['/login']);
|
|
|
|
|
}
|
2022-03-03 15:44:22 +01:00
|
|
|
|
|
|
|
|
doLogout() {
|
|
|
|
|
const user = this.userService.logout();
|
|
|
|
|
if (HasFailed(user)) {
|
|
|
|
|
this.utilService.showSnackBar(user.getReason(), SnackBarType.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.utilService.showSnackBar('Logout successful', SnackBarType.Success);
|
|
|
|
|
}
|
2022-03-03 13:38:39 +01:00
|
|
|
}
|