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

173 lines
4.1 KiB
TypeScript
Raw Normal View History

2018-05-14 08:10:55 +00:00
import {
Component,
OnDestroy,
OnInit,
2018-05-28 05:41:56 +00:00
Output,
2018-05-29 09:32:22 +00:00
EventEmitter, Input,
2018-05-14 08:10:55 +00:00
} from '@angular/core';
2018-05-29 05:24:36 +00:00
import { FormGroup, FormBuilder, Validators, AbstractControl, FormControl, ValidationErrors } from '@angular/forms';
2018-05-02 08:09:39 +00:00
import { Member } from '@overflow/commons-typescript/model/member';
2018-05-31 10:20:14 +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-05-31 12:11:31 +00:00
import { MemberService } from '../service/member.service';
2018-04-06 06:59:49 +00:00
@Component({
selector: 'of-member-signup',
2018-05-28 05:41:56 +00:00
templateUrl: './member-signup.component.html',
2018-04-06 06:59:49 +00:00
})
2018-05-28 05:41:56 +00:00
export class MemberSignupComponent implements OnInit, OnDestroy {
2018-05-31 10:20:14 +00:00
// @Output() signup = new EventEmitter<{member: Member, password: string}>();
@Output() signin = new EventEmitter();
2018-05-28 05:41:56 +00:00
2018-05-31 10:20:14 +00:00
member$: Observable<Member>;
pending$: Observable<boolean>;
error$: Observable<any>;
2018-04-06 06:59:49 +00:00
signupForm: FormGroup;
2018-04-10 10:19:41 +00:00
email: AbstractControl;
password: AbstractControl;
pwConfirm: AbstractControl;
name: AbstractControl;
phone: AbstractControl;
company: AbstractControl;
2018-05-29 09:32:22 +00:00
selectPolicy: AbstractControl;
captcha: AbstractControl;
2018-05-03 07:19:10 +00:00
termsDisplay = false;
policyDisplay = false;
2018-04-06 06:59:49 +00:00
constructor(
2018-05-31 10:20:14 +00:00
private store: Store<any>,
2018-04-12 08:06:15 +00:00
private formBuilder: FormBuilder,
2018-05-31 10:20:14 +00:00
private memberService: MemberService,
2018-04-06 06:59:49 +00:00
) { }
2018-05-14 08:10:55 +00:00
2018-04-06 06:59:49 +00:00
ngOnInit() {
this.initForm();
}
2018-05-14 08:10:55 +00:00
ngOnDestroy() {
}
2018-04-06 06:59:49 +00:00
initForm() {
this.signupForm = this.formBuilder.group({
2018-04-12 08:06:15 +00:00
'email': [
'geekhot@hotmail.co.kr',
[
Validators.required,
Validators.email
]
2018-04-06 06:59:49 +00:00
],
2018-04-12 08:06:15 +00:00
'password': [
'!@#$Qwer1234',
[
Validators.required,
Validators.pattern('^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{6,}$'),
Validators.minLength(6),
Validators.maxLength(25),
]
2018-04-06 06:59:49 +00:00
],
2018-04-12 08:06:15 +00:00
'pwConfirm': [
'!@#$Qwer1234',
[
Validators.compose([
2018-05-31 12:11:31 +00:00
Validators.required, this.pwMatchValidator
])
2018-04-12 08:06:15 +00:00
]
2018-04-06 06:59:49 +00:00
],
2018-04-12 08:06:15 +00:00
'name': [
'Park',
[
Validators.required
]
2018-04-06 06:59:49 +00:00
],
2018-04-12 08:06:15 +00:00
'phone': [
'010-3040-0303',
[
Validators.required
]
2018-04-06 06:59:49 +00:00
],
2018-04-12 08:06:15 +00:00
'company': [
'atgame',
[
Validators.required
]
2018-04-06 06:59:49 +00:00
],
2018-05-29 09:32:22 +00:00
'selectPolicy': [
2018-05-31 12:11:31 +00:00
false, [Validators.requiredTrue],
2018-05-29 09:32:22 +00:00
],
'captcha': [
2018-05-31 12:11:31 +00:00
false, [Validators.requiredTrue],
2018-05-29 09:32:22 +00:00
]
2018-04-10 10:19:41 +00:00
});
this.email = this.signupForm.controls['email'];
this.password = this.signupForm.controls['password'];
this.pwConfirm = this.signupForm.controls['pwConfirm'];
this.name = this.signupForm.controls['name'];
this.phone = this.signupForm.controls['phone'];
this.company = this.signupForm.controls['company'];
2018-05-29 09:32:22 +00:00
this.selectPolicy = this.signupForm.controls['selectPolicy'];
this.captcha = this.signupForm.controls['captcha'];
2018-04-10 07:30:05 +00:00
}
2018-05-29 05:24:36 +00:00
pwMatchValidator(control: FormControl): ValidationErrors {
2018-04-10 10:19:41 +00:00
let pw;
if (control.parent) {
pw = control.parent.controls['password'].value;
}
if (control.value !== pw) {
return { notMatched: true };
}
2018-05-29 05:24:36 +00:00
return null;
2018-04-10 10:19:41 +00:00
}
2018-04-06 06:59:49 +00:00
2018-05-28 05:41:56 +00:00
signupFormSubmit() {
2018-04-12 08:06:15 +00:00
const signupValue = Object.assign({}, this.signupForm.value);
const password = signupValue.password;
2018-04-10 10:19:41 +00:00
const member: Member = {
2018-04-12 08:06:15 +00:00
email: signupValue.email,
name: signupValue.name,
phone: signupValue.phone,
companyName: signupValue.company,
2018-04-10 10:19:41 +00:00
};
2018-05-31 10:20:14 +00:00
this.memberService.signup(member, password)
.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-06-04 08:55:47 +00:00
take(1),
).subscribe();
2018-04-06 06:59:49 +00:00
}
2018-05-03 07:19:10 +00:00
termsDisplayOpen() {
this.termsDisplay = true;
}
policyDisplayOpen() {
this.policyDisplay = true;
}
2018-05-14 08:10:55 +00:00
onScriptLoad() {
console.log('Google reCAPTCHA loaded and is ready for use!');
}
onScriptError() {
console.log('Something went long when loading the Google reCAPTCHA');
}
2018-05-31 10:20:14 +00:00
onSignin() {
this.signin.emit();
}
2018-04-06 06:59:49 +00:00
}