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