aboutsummaryrefslogtreecommitdiff
path: root/Timeline/ClientApp/src/app/user-dialog/user-dialog.component.ts
blob: 368a17755e227ef9e013eeb90eb10201cda6ad5e (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
import { Component, OnInit } from '@angular/core';
import { UserService, UserInfo } from './user.service';
import { LoginEvent, LoginMessage } from '../user-login/user-login.component';

@Component({
  selector: 'app-user-dialog',
  templateUrl: './user-dialog.component.html',
  styleUrls: ['./user-dialog.component.css']
})
export class UserDialogComponent implements OnInit {

  constructor(private userService: UserService) { }

  state: 'loading' | 'login' | 'success' = 'loading';

  loginMessage: LoginMessage;

  userInfo: UserInfo;

  ngOnInit() {
    this.userService.validateUserLoginState().subscribe(result => {
      if (result.state === 'success') {
        this.userInfo = result.userInfo;
        this.state = 'success';
      } else {
        if (result.state === 'invalid') {
          this.loginMessage = 'invalidlogin';
        } else if (result.state === 'nologin') {
          this.loginMessage = 'nologin';
        }
        this.state = 'login';
      }
    });
  }

  login(event: LoginEvent) {
    this.userService.tryLogin(event.username, event.password).subscribe(result => {
      this.userInfo = result;
      this.state = 'success';
    }, (error: Error) => {
      this.loginMessage = error.message;
    });
  }
}