Projekt

Obecné

Profil

Stáhnout (1.32 KB) Statistiky
| Větev: | Tag: | Revize:
1
import {Injectable} from '@angular/core';
2
import {UserService} from '../api/user.service';
3
import {UserProfile} from '../../models/user.model';
4
import {Observable} from 'rxjs';
5

    
6
@Injectable({
7
  providedIn: 'root'
8
})
9
export class ProfileService {
10
  private profile: UserProfile;
11

    
12
  constructor(private userService: UserService) {
13

    
14
  }
15

    
16
  /**
17
   * Returns logged user profile if the server responds
18
   * with valid profile otherwise observer returns error
19
   * with message 'Cannot log in'
20
   *
21
   * The idea was to cache the logged user profile but
22
   * the changes to logged user would not be seen until
23
   * the user logged off and then logged back in
24
   */
25
  public getLoggedUser() {
26
    return new Observable<UserProfile>((obs) => {
27
      this.userService.getLoggedUserProfile()
28
        .subscribe((userProfile: UserProfile) => {
29
            this.profile = {...userProfile};
30
            obs.next(this.profile);
31
            obs.complete();
32
          },
33
          error1 => {
34
            obs.error(error1);
35
            obs.complete();
36
          });
37
    });
38
  }
39

    
40
  /**
41
   * Do not use at the start of the application
42
   * User might be logged but the service hasn't
43
   * finished the request for the user profile
44
   */
45
  public isUserLogged(): boolean {
46
    return this.profile == null;
47
  }
48

    
49
  public logUserOff(): void {
50
    this.profile = null;
51
  }
52
}
(4-4/4)