login.component.ts 1.21 KB
import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {TranslatePipe} from '@ngx-translate/core';
import {SessionServiceService} from '../../../shared/services/session-service.service';
import {FormGroup, NgForm} from "@angular/forms";



@Component({
  selector: 'moya-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css'],
  providers: [TranslatePipe]
})
export class LoginComponent implements OnInit {

  @Output()
  loginEvent = new EventEmitter<boolean>();

  loginError = false;
  loginOk = false;
  submitting = false;

  constructor(private sessionService: SessionServiceService) { }

  ngOnInit() {
    this.loginError = this.loginOk = this.submitting = false;
  }

  login(userDetails: NgForm) {

    this.loginError = this.loginOk = false;
    this.submitting = true;

    this.sessionService.doLogin(userDetails.value.username, userDetails.value.password).subscribe((x) => {
      this.loginError = this.loginOk = this.submitting = false;
      this.loginOk = true;

      this.loginEvent.emit(true);
    }, (error) => {
      this.loginError = this.loginOk = this.submitting = false;
      this.loginError = true;

      this.loginEvent.emit(false);
    });
  }

}