2018-05-28 05:41:56 +00:00
|
|
|
import { Component, OnInit, OnDestroy, Input, Output, EventEmitter } from '@angular/core';
|
|
|
|
import { Member } from '@overflow/commons-typescript/model/member';
|
2018-06-01 08:21:15 +00:00
|
|
|
import { AbstractControl, FormBuilder, FormGroup, Validators} from '@angular/forms';
|
2018-05-28 11:16:00 +00:00
|
|
|
import { DomainMember } from '@overflow/commons-typescript/model/domain';
|
2018-06-01 08:21:15 +00:00
|
|
|
import { Observable, of } from 'rxjs';
|
|
|
|
import { Store } from '@ngrx/store';
|
2018-06-01 09:45:28 +00:00
|
|
|
import { AuthSelector } from '@overflow/shared/auth/store';
|
|
|
|
import * as AuthStore from '@overflow/shared/auth/store/auth';
|
2018-06-01 08:21:15 +00:00
|
|
|
import { catchError, map, tap } from 'rxjs/operators';
|
|
|
|
import { MemberService } from '../service/member.service';
|
2018-05-28 05:41:56 +00:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'of-member-profile',
|
|
|
|
templateUrl: './member-profile.component.html',
|
|
|
|
})
|
|
|
|
export class MemberProfileComponent implements OnInit, OnDestroy {
|
2018-06-01 08:21:15 +00:00
|
|
|
domainMember: DomainMember;
|
|
|
|
profileForm: FormGroup;
|
2018-05-28 05:41:56 +00:00
|
|
|
|
2018-06-01 08:21:15 +00:00
|
|
|
pending$: Observable<boolean>;
|
|
|
|
error$: Observable<any>;
|
|
|
|
|
|
|
|
email: AbstractControl;
|
|
|
|
name: AbstractControl;
|
|
|
|
phone: AbstractControl;
|
|
|
|
company: AbstractControl;
|
2018-05-28 05:41:56 +00:00
|
|
|
|
|
|
|
constructor(
|
2018-06-01 08:21:15 +00:00
|
|
|
private store: Store<any>,
|
2018-05-28 05:41:56 +00:00
|
|
|
private formBuilder: FormBuilder,
|
2018-06-01 08:21:15 +00:00
|
|
|
private memberService: MemberService,
|
2018-05-28 05:41:56 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
2018-06-01 09:45:28 +00:00
|
|
|
this.store.select(AuthSelector.selectDomainMember).subscribe(
|
2018-06-01 08:21:15 +00:00
|
|
|
(domainMember: DomainMember) => {
|
|
|
|
this.domainMember = domainMember;
|
|
|
|
}
|
|
|
|
);
|
2018-05-28 05:41:56 +00:00
|
|
|
this.initForm();
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
}
|
|
|
|
|
|
|
|
initForm() {
|
2018-06-01 08:21:15 +00:00
|
|
|
this.profileForm = this.formBuilder.group({
|
|
|
|
'email': [
|
|
|
|
this.domainMember.member.email,
|
2018-05-31 12:11:31 +00:00
|
|
|
[
|
|
|
|
Validators.required,
|
|
|
|
Validators.email
|
|
|
|
]],
|
2018-06-01 08:21:15 +00:00
|
|
|
'name': [
|
|
|
|
this.domainMember.member.name, [Validators.required]
|
|
|
|
],
|
|
|
|
'companyName': [
|
|
|
|
this.domainMember.member.companyName, [Validators.required]
|
|
|
|
],
|
|
|
|
'phone': [
|
|
|
|
this.domainMember.member.phone, [Validators.required]
|
|
|
|
],
|
2018-05-28 05:41:56 +00:00
|
|
|
});
|
2018-06-01 08:21:15 +00:00
|
|
|
|
|
|
|
this.email = this.profileForm.controls['email'];
|
|
|
|
this.name = this.profileForm.controls['name'];
|
|
|
|
this.phone = this.profileForm.controls['phone'];
|
|
|
|
this.company = this.profileForm.controls['company'];
|
2018-05-28 05:41:56 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 11:16:00 +00:00
|
|
|
profileFormSubmit() {
|
2018-06-01 08:21:15 +00:00
|
|
|
const formValue = Object.assign({}, this.profileForm.value);
|
|
|
|
|
|
|
|
if (!this.checkProfile(formValue)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-28 05:41:56 +00:00
|
|
|
const member: Member = {
|
2018-06-01 08:21:15 +00:00
|
|
|
email: formValue.email,
|
2018-05-28 05:41:56 +00:00
|
|
|
name: formValue.name,
|
|
|
|
phone: formValue.phone,
|
|
|
|
companyName: formValue.companyName,
|
|
|
|
};
|
2018-06-01 08:21:15 +00:00
|
|
|
|
|
|
|
this.memberService.modify(member)
|
|
|
|
.pipe(
|
|
|
|
tap(() => {
|
|
|
|
this.pending$ = of(true);
|
|
|
|
}),
|
|
|
|
map((rmember: Member) => {
|
2018-06-01 09:45:28 +00:00
|
|
|
this.store.dispatch(new AuthStore.ModifySuccess(rmember));
|
2018-06-01 08:21:15 +00:00
|
|
|
}),
|
|
|
|
catchError(err => {
|
|
|
|
this.error$ = of(err);
|
|
|
|
return of();
|
|
|
|
}),
|
|
|
|
tap(() => {
|
|
|
|
this.pending$ = of(false);
|
|
|
|
}),
|
|
|
|
).take(1).subscribe();
|
2018-05-28 05:41:56 +00:00
|
|
|
}
|
|
|
|
|
2018-06-01 08:21:15 +00:00
|
|
|
private checkProfile(formValue: any): boolean {
|
|
|
|
let isChange = false;
|
|
|
|
if (
|
|
|
|
formValue.email !== this.domainMember.member.email &&
|
|
|
|
formValue.name !== this.domainMember.member.name &&
|
|
|
|
formValue.phone !== this.domainMember.member.phone &&
|
|
|
|
formValue.companyName !== this.domainMember.member.companyName
|
|
|
|
) {
|
|
|
|
isChange = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return isChange;
|
|
|
|
}
|
2018-05-28 05:41:56 +00:00
|
|
|
}
|