1 |
f8b9a3a2
|
hlavja
|
import {Injectable} from '@angular/core';
|
2 |
|
|
import {BehaviorSubject, Observable} from 'rxjs';
|
3 |
2c5da396
|
hlavja
|
import {User} from '../models/user';
|
4 |
f8b9a3a2
|
hlavja
|
|
5 |
|
|
@Injectable({
|
6 |
|
|
providedIn: 'root'
|
7 |
|
|
})
|
8 |
|
|
export class UserState {
|
9 |
2c5da396
|
hlavja
|
private userState$: BehaviorSubject<User> = new BehaviorSubject<User>(null);
|
10 |
f8b9a3a2
|
hlavja
|
|
11 |
2c5da396
|
hlavja
|
constructor() {}
|
12 |
f8b9a3a2
|
hlavja
|
|
13 |
2c5da396
|
hlavja
|
setUser(user: User): void {
|
14 |
f8b9a3a2
|
hlavja
|
this.userState$.next(user);
|
15 |
|
|
}
|
16 |
|
|
|
17 |
2c5da396
|
hlavja
|
setLoggedIn(loggedIn: boolean) {
|
18 |
|
|
this.userState$.next({...this.userState$.getValue(), isLoggedIn: loggedIn})
|
19 |
f8b9a3a2
|
hlavja
|
}
|
20 |
|
|
|
21 |
2c5da396
|
hlavja
|
getUser(): User {
|
22 |
|
|
return this.userState$.getValue();
|
23 |
f8b9a3a2
|
hlavja
|
}
|
24 |
|
|
|
25 |
2c5da396
|
hlavja
|
getUser$(refresh: boolean = false): Observable<User> {
|
26 |
|
|
return this.userState$.asObservable();
|
27 |
f8b9a3a2
|
hlavja
|
}
|
28 |
|
|
|
29 |
|
|
getLoggedIn(): boolean {
|
30 |
2c5da396
|
hlavja
|
return this.userState$.getValue().isLoggedIn;
|
31 |
2ddd52c9
|
hlavja
|
}
|
32 |
f8b9a3a2
|
hlavja
|
}
|