member_webapp/@overflow/member/component/profile/profile.component.ts

93 lines
2.4 KiB
TypeScript
Raw Normal View History

2018-04-30 12:04:58 +00:00
import { Component, OnInit, OnDestroy } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
2018-05-24 06:44:13 +00:00
// import { PagesComponent } from 'app/pages/pages.component';
2018-04-18 12:57:58 +00:00
import * as AuthStore from '../../store/auth';
2018-04-30 11:50:47 +00:00
import * as ModifyStore from '../../store/modify';
2018-05-02 08:09:39 +00:00
import { Member } from '@overflow/commons-typescript/model/member';
2018-04-18 12:57:58 +00:00
import { AuthSelector } from '../../store';
2018-04-30 12:04:58 +00:00
import { Store, select } from '@ngrx/store';
2018-04-30 11:50:47 +00:00
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
2018-04-30 12:04:58 +00:00
import { ModifySelector } from '../../store';
2018-05-24 06:44:13 +00:00
import { RPCClientError } from '@loafer/ng-rpc';
2018-04-18 12:57:58 +00:00
@Component({
selector: 'of-member-profile',
templateUrl: './profile.component.html',
})
2018-04-30 12:04:58 +00:00
export class ProfileComponent implements OnInit, OnDestroy {
2018-04-18 12:57:58 +00:00
member: Member;
modifyForm: FormGroup;
2018-04-30 12:04:58 +00:00
modifiedMember$ = this.modifyStore.pipe(select(ModifySelector.select('member')));
2018-04-18 12:57:58 +00:00
constructor(
2018-05-24 06:44:13 +00:00
// public app: PagesComponent,
2018-04-18 12:57:58 +00:00
private activatedRoute: ActivatedRoute,
private router: Router,
private store: Store<AuthStore.State>,
private formBuilder: FormBuilder,
2018-04-30 12:04:58 +00:00
private modifyStore: Store<ModifyStore.State>
2018-04-18 12:57:58 +00:00
) {
}
ngOnInit() {
2018-04-18 12:57:58 +00:00
this.store.select(AuthSelector.select('member')).subscribe(
(member: Member) => {
this.member = member;
},
(error) => {
console.log(error);
}
);
this.initForm();
}
2018-04-30 12:04:58 +00:00
ngOnDestroy() {
}
2018-04-18 12:57:58 +00:00
initForm() {
this.modifyForm = this.formBuilder.group({
2018-04-30 12:04:58 +00:00
'email': [this.member.email,
2018-04-30 11:50:47 +00:00
[
Validators.required,
Validators.email
]],
2018-04-30 12:04:58 +00:00
'name': [this.member.name, []],
'companyName': [this.member.companyName, []],
2018-04-30 11:50:47 +00:00
'phone': [this.member.phone, []],
2018-04-18 12:57:58 +00:00
});
}
modifyProfile() {
2018-04-30 11:50:47 +00:00
const modifyValue = Object.assign({}, this.modifyForm.value);
const member: Member = {
email: this.member.email,
name: modifyValue.name,
phone: modifyValue.phone,
companyName: modifyValue.companyName,
};
2018-04-30 12:08:01 +00:00
this.modifyStore.dispatch(new ModifyStore.Modify(member));
2018-04-30 12:04:58 +00:00
const modifiedMemberSubscription$ = this.modifiedMember$.subscribe(
(m: Member) => {
if (m) {
console.log(m);
}
if (modifiedMemberSubscription$) {
modifiedMemberSubscription$.unsubscribe();
}
},
(error: RPCClientError) => {
console.log(error.response.message);
}
);
}
}