Newer
Older
Website / src / app / login / login.component.ts
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, NgForm, Validators } from '@angular/forms';
import { environment } from 'src/environments/environment';
import { AccountService } from './account.service';

class LoginInformation {
  constructor(public username: string, public password: string) {
  }
}

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
  model = new LoginInformation('', '')
  public form: FormGroup
  public valid = true
  public connectionError = false
  public environment = environment
  constructor(private formBuilder: FormBuilder, private account: AccountService) {
    this.form = this.formBuilder.group({
      username: ['', Validators.required],
      password: ['', Validators.required]
    })
  }

  ngOnInit(): void {
  }

  onSubmit() {
    if (this.form.invalid) {
      this.valid = false
      return
    }
    this.valid = true
    this.connectionError = false
    this.account.login(this.form.controls.username.value, this.form.controls.password.value, 
      () => {this.valid = false}, () => {this.connectionError = true});
  }
}