Projekt

Obecné

Profil

Stáhnout (1.24 KB) Statistiky
| Větev: | Tag: | Revize:
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
import {ToastService} from '../../shared/services/toast.service';
7

    
8
@Injectable({
9
  providedIn: 'root'
10
})
11
export class UserState {
12
  private userState$: BehaviorSubject<User> = new BehaviorSubject<User>(null);
13

    
14
  constructor(
15
    private loginService: LoginService,
16
    private toastService: ToastService
17
  ) {}
18

    
19
  setUser(user: User): void {
20
    this.userState$.next(user);
21
  }
22

    
23
  setLoggedIn(loggedIn: boolean) {
24
    this.userState$.next({...this.userState$.getValue(), isLoggedIn: loggedIn})
25
  }
26

    
27
  getUser(): User {
28
    return this.userState$.getValue();
29
  }
30

    
31
  getUser$(refresh: boolean = false): Observable<User> {
32
   if (this.userState$.getValue()){
33
      this.loginService.getUserInfo$Response().subscribe(res => {
34
        this.userState$.next({...this.userState$.getValue(), userInfo: res.body});
35
      }, err => this.toastService.showError(err.error.message));
36
   }
37
    return this.userState$.asObservable();
38
  }
39

    
40
  getLoggedIn(): boolean {
41
    return this.userState$.getValue().isLoggedIn;
42
  }
43
}
    (1-1/1)