import { Component, OnInit, Input } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { Observable } from 'rxjs';

import { MemberSigninContainerSelector } from '../store';

import * as MemberEntityStore from '../store/entity/member';
import { Member } from '@overflow/commons-typescript/model/member';

@Component({
  selector: 'of-member-signin-container',
  templateUrl: './member-signin-container.component.html',
})
export class MemberSigninContainerComponent implements OnInit {
  @Input() returnURL: string;
  member$: Observable<Member>;
  pending$: Observable<boolean>;
  error$: Observable<any>;

  constructor(
    private store: Store<any>,
  ) {
  }

  ngOnInit() {
    this.member$ = this.store.pipe(select(MemberSigninContainerSelector.selectMember));
    this.pending$ = this.store.pipe(select(MemberSigninContainerSelector.selectPending));
    this.error$ = this.store.pipe(select(MemberSigninContainerSelector.selectError));
  }

  onSignin(info: {email: string, password: string}) {
    this.store.dispatch(new MemberEntityStore.Signin({...info, returnURL: this.returnURL}));
  }
}
