aboutsummaryrefslogtreecommitdiff
path: root/Timeline/ClientApp/src/app/user/user.service.ts
blob: 6cae2d319dfee5b3f5d743ab2588df14e60fe379 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { Injectable } from '@angular/core';
import { MatDialog, MatDialogRef } from '@angular/material';
import { Router, ActivationStart } from '@angular/router';

import { Observable } from 'rxjs';

import { UserInfo } from './entities';
import { InternalUserService } from './internal-user-service/internal-user.service';
import { UserDialogComponent } from './user-dialog/user-dialog.component';


/**
 * This service provides public api of user module.
 */
@Injectable({
  providedIn: 'root'
})
export class UserService {

  private dialogRef: MatDialogRef<UserDialogComponent> | null = null;

  constructor(router: Router, private dialog: MatDialog, private internalService: InternalUserService) {
    router.events.subscribe(event => {
      if (event instanceof ActivationStart && event.snapshot.outlet === 'user') {
        if (!this.dialogRef) {
          setTimeout(() => this.openUserDialog(), 0);
        }
      }
    });
  }

  get currentUserInfo(): UserInfo | null | undefined {
    return this.internalService.currentUserInfo;
  }

  get userInfo$(): Observable<UserInfo | null> {
    return this.internalService.userInfo$;
  }

  private openUserDialog() {
    if (this.dialogRef) {
      return;
    }

    this.dialogRef = this.dialog.open(UserDialogComponent, {
      width: '300px'
    });

    const subscription = this.dialogRef.afterClosed().subscribe(_ => {
      this.internalService.userRouteNavigate(null);
      this.dialogRef = null;
      subscription.unsubscribe();
    });
  }
}