1 |
f8b9a3a2
|
hlavja
|
import {Injectable} from '@angular/core';
|
2 |
|
|
import {BehaviorSubject, Observable} from 'rxjs';
|
3 |
2c5da396
|
hlavja
|
import {User} from '../models/user';
|
4 |
87c1bdd1
|
hlavja
|
import {AuthService} from '../services/auth.service';
|
5 |
|
|
import {LoginService} from '../../shared/api/endpoints/services/login.service';
|
6 |
f8b9a3a2
|
hlavja
|
|
7 |
|
|
@Injectable({
|
8 |
|
|
providedIn: 'root'
|
9 |
|
|
})
|
10 |
|
|
export class UserState {
|
11 |
2c5da396
|
hlavja
|
private userState$: BehaviorSubject<User> = new BehaviorSubject<User>(null);
|
12 |
f8b9a3a2
|
hlavja
|
|
13 |
87c1bdd1
|
hlavja
|
constructor(
|
14 |
|
|
private loginService: LoginService
|
15 |
|
|
) {}
|
16 |
f8b9a3a2
|
hlavja
|
|
17 |
2c5da396
|
hlavja
|
setUser(user: User): void {
|
18 |
f8b9a3a2
|
hlavja
|
this.userState$.next(user);
|
19 |
|
|
}
|
20 |
|
|
|
21 |
2c5da396
|
hlavja
|
setLoggedIn(loggedIn: boolean) {
|
22 |
|
|
this.userState$.next({...this.userState$.getValue(), isLoggedIn: loggedIn})
|
23 |
f8b9a3a2
|
hlavja
|
}
|
24 |
|
|
|
25 |
2c5da396
|
hlavja
|
getUser(): User {
|
26 |
|
|
return this.userState$.getValue();
|
27 |
f8b9a3a2
|
hlavja
|
}
|
28 |
|
|
|
29 |
2c5da396
|
hlavja
|
getUser$(refresh: boolean = false): Observable<User> {
|
30 |
87c1bdd1
|
hlavja
|
if (this.userState$.getValue()){
|
31 |
|
|
this.loginService.getUserInfo().subscribe(res => this.userState$.next({...this.userState$.getValue(), userInfo: res}));
|
32 |
|
|
}
|
33 |
2c5da396
|
hlavja
|
return this.userState$.asObservable();
|
34 |
f8b9a3a2
|
hlavja
|
}
|
35 |
|
|
|
36 |
|
|
getLoggedIn(): boolean {
|
37 |
2c5da396
|
hlavja
|
return this.userState$.getValue().isLoggedIn;
|
38 |
2ddd52c9
|
hlavja
|
}
|
39 |
f8b9a3a2
|
hlavja
|
}
|