member_webapp/@overflow/member/component/member-modify-password.component.ts

105 lines
2.9 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, FormControl, ValidationErrors, AbstractControl } from '@angular/forms';
2018-05-31 10:20:14 +00:00
import { Member } from '@overflow/commons-typescript/model/member';
import { Store, select } from '@ngrx/store';
import { Observable, of } from 'rxjs';
import { catchError, exhaustMap, map, tap } from 'rxjs/operators';
import { MemberService } from '../service/member.service';
2018-05-28 05:41:56 +00:00
@Component({
selector: 'of-member-modify-password',
templateUrl: './member-modify-password.component.html',
})
export class MemberModifyPasswordComponent implements OnInit {
2018-05-31 10:20:14 +00:00
member$: Observable<Member>;
pending$: Observable<boolean>;
error$: Observable<any>;
2018-05-28 05:41:56 +00:00
@Input() token: string;
2018-05-31 10:20:14 +00:00
// @Input() member: Member;
2018-05-30 07:44:21 +00:00
2018-05-31 10:20:14 +00:00
// @Output() modifyPassword = new EventEmitter<{token: string, password: string, confirmPassword: string}>();
2018-05-28 05:41:56 +00:00
@Output() signin = new EventEmitter();
modifyPasswordForm: FormGroup;
2018-05-30 07:44:21 +00:00
password: AbstractControl;
pwConfirm: AbstractControl;
2018-05-28 05:41:56 +00:00
constructor(
2018-05-31 10:20:14 +00:00
private store: Store<any>,
2018-05-28 05:41:56 +00:00
private formBuilder: FormBuilder,
2018-05-31 10:20:14 +00:00
private memberService: MemberService,
2018-05-28 05:41:56 +00:00
) { }
ngOnInit() {
this.initForm();
}
initForm() {
this.modifyPasswordForm = this.formBuilder.group({
2018-05-30 07:44:21 +00:00
'password': [
2018-05-28 05:41:56 +00:00
'',
[
2018-05-30 07:44:21 +00:00
Validators.required,
Validators.pattern('^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{6,}$'),
Validators.minLength(6),
Validators.maxLength(25),
2018-05-28 05:41:56 +00:00
]
],
2018-05-30 07:44:21 +00:00
'pwConfirm': [
2018-05-28 05:41:56 +00:00
'',
[
2018-05-30 07:44:21 +00:00
Validators.compose([
2018-05-31 12:11:31 +00:00
Validators.required, this.pwMatchValidator
2018-05-30 07:44:21 +00:00
])
2018-05-28 05:41:56 +00:00
]
2018-05-30 07:44:21 +00:00
]
2018-05-28 05:41:56 +00:00
});
2018-05-30 07:44:21 +00:00
this.password = this.modifyPasswordForm.controls['password'];
this.pwConfirm = this.modifyPasswordForm.controls['pwConfirm'];
}
pwMatchValidator(control: FormControl): ValidationErrors {
let pw;
if (control.parent) {
pw = control.parent.controls['password'].value;
}
if (control.value !== pw) {
return { notMatched: true };
}
return null;
2018-05-28 05:41:56 +00:00
}
modifyPasswordFormSubmit() {
const formValue = Object.assign({}, this.modifyPasswordForm.value);
2018-05-31 10:20:14 +00:00
// this.modifyPassword.emit({token: this.token, password: formValue.pw, confirmPassword: formValue.confirmPw});
2018-06-01 03:50:07 +00:00
console.log(this.token);
console.log(formValue.password);
console.log(formValue.pwConfirm);
2018-05-31 10:20:14 +00:00
this.memberService.resetPassword(this.token, formValue.password, formValue.pwConfirm)
.pipe(
tap(() => {
this.pending$ = of(true);
}),
map((rmember: Member) => {
this.member$ = of(rmember);
}),
2018-05-31 12:11:31 +00:00
catchError(err => {
2018-05-31 10:20:14 +00:00
this.error$ = of(err);
return of();
}),
tap(() => {
this.pending$ = of(false);
}),
2018-05-31 12:11:31 +00:00
).take(1).subscribe();
2018-05-28 05:41:56 +00:00
}
2018-05-30 07:44:21 +00:00
onSignin() {
this.signin.emit();
}
2018-05-28 05:41:56 +00:00
}