import { AuthState } from "../store/auth.reducers";
import { Store } from "@ngrx/store";
import { FormGroup, FormControl, Validators } from "@angular/forms";
import { RequestTokenAction } from "../store/auth.actions";
import { Login } from "../model/login.model";
import { OnInit } from "@angular/core";
import { AbstractFormComponent } from "../abstract-form.component";

export class BaseLogin extends AbstractFormComponent implements OnInit{

    loginForm: FormGroup;

    constructor(private store: Store<AuthState>){
        super();
    }

    ngOnInit() {
        this.loginForm = new FormGroup({
            'email': new FormControl(null, [Validators.required, Validators.email]),
            'password': new FormControl(null, [Validators.required])
        });
    }

    onLoginSubmit(){
        let email = this.loginForm.value.email;
        let password = this.loginForm.value.password;
        this.store.dispatch(new RequestTokenAction(new Login(email, password)));
    }

}
