member_webapp/@overflow/member/component/member-signin.component.ts

95 lines
2.3 KiB
TypeScript
Raw Normal View History

2018-05-28 05:41:56 +00:00
import { Component, OnInit, Output, EventEmitter, Input } from '@angular/core';
2018-05-31 12:11:31 +00:00
import { FormGroup, FormBuilder, Validators, AbstractControl } from '@angular/forms';
2018-06-01 10:16:04 +00:00
import { Store, select } from '@ngrx/store';
import { Observable, of } from 'rxjs';
2018-06-04 08:55:47 +00:00
import { catchError, exhaustMap, map, tap, take } from 'rxjs/operators';
2018-06-01 10:16:04 +00:00
import { MemberService } from '../service/member.service';
import {DomainMember} from '@overflow/commons-typescript/model/domain';
import * as AuthStore from '../../shared/auth/store/auth';
2018-05-28 05:41:56 +00:00
@Component({
selector: 'of-member-signin',
templateUrl: './member-signin.component.html',
})
export class MemberSigninComponent implements OnInit {
2018-05-30 05:52:39 +00:00
2018-06-01 10:16:04 +00:00
domainMember$: Observable<DomainMember>;
pending$: Observable<boolean>;
error$: Observable<any>;
@Input() returnURL;
2018-05-28 05:41:56 +00:00
@Output() resetPassword = new EventEmitter();
@Output() signup = new EventEmitter();
2018-06-01 10:16:04 +00:00
// @Output() signin = new EventEmitter<{ email: string, password: string }>();
2018-05-28 05:41:56 +00:00
errorMessage: string | null;
2018-05-30 05:52:39 +00:00
email: AbstractControl;
password: AbstractControl;
2018-05-28 05:41:56 +00:00
signinForm: FormGroup;
constructor(
2018-06-01 10:16:04 +00:00
private store: Store<any>,
2018-05-28 05:41:56 +00:00
private formBuilder: FormBuilder,
2018-06-01 10:16:04 +00:00
private memberService: MemberService,
2018-05-28 05:41:56 +00:00
) {
}
ngOnInit() {
this.initForm();
}
initForm() {
this.signinForm = this.formBuilder.group({
'email': [
'',
[
Validators.required,
Validators.email
]
],
'password': [
'',
[
2018-05-30 05:52:39 +00:00
Validators.required
2018-05-28 05:41:56 +00:00
]
],
});
2018-05-30 05:52:39 +00:00
this.email = this.signinForm.controls['email'];
this.password = this.signinForm.controls['password'];
2018-05-28 05:41:56 +00:00
}
signinFormSubmit() {
const formValue = Object.assign({}, this.signinForm.value);
2018-06-01 10:16:04 +00:00
this.memberService.signin(formValue.email, formValue.password)
.pipe(
tap(() => {
this.pending$ = of(true);
}),
map((r: any) => {
this.domainMember$ = r.domainMember;
r.returnURL = this.returnURL;
this.store.dispatch(new AuthStore.SigninSuccess(r));
}),
catchError(err => {
this.error$ = of(err);
return of();
}),
tap(() => {
this.pending$ = of(false);
}),
2018-06-04 08:55:47 +00:00
take(1),
).subscribe();
2018-05-28 05:41:56 +00:00
}
2018-05-30 05:52:39 +00:00
onResetPassword() {
this.resetPassword.emit();
}
onSignup() {
this.signup.emit();
}
2018-05-28 05:41:56 +00:00
}