This commit is contained in:
crusader 2018-05-28 18:33:48 +09:00
parent e980403faf
commit 312ea95f07
6 changed files with 119 additions and 97 deletions

View File

@ -32,13 +32,13 @@ export enum ActionType {
export class Signin implements Action { export class Signin implements Action {
readonly type = ActionType.Signin; readonly type = ActionType.Signin;
constructor(public payload: { email: string, password: string }) { } constructor(public payload: { email: string, password: string, returnURL: string }) { }
} }
export class SigninCookie implements Action { export class SigninCookie implements Action {
readonly type = ActionType.SigninCookie; readonly type = ActionType.SigninCookie;
constructor(public payload: { authToken: string }) { } constructor(public payload: { authToken: string, returnURL: string }) { }
} }
export class Signout implements Action { export class Signout implements Action {

View File

@ -1,8 +1,8 @@
import { TestBed, inject } from '@angular/core/testing'; import { TestBed, inject } from '@angular/core/testing';
import { Effects } from './signup.effect'; import { Effects } from './member.effect';
describe('Signup.Effects', () => { describe('member-entity.Effects', () => {
beforeEach(() => { beforeEach(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
providers: [Effects] providers: [Effects]

View File

@ -5,13 +5,8 @@ import { Effect, Actions, ofType } from '@ngrx/effects';
import { Action } from '@ngrx/store'; import { Action } from '@ngrx/store';
import { Observable } from 'rxjs/Observable'; import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of'; import { of } from 'rxjs';
import { catchError, exhaustMap, map, tap } from 'rxjs/operators';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/exhaustMap';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/take';
import { Member } from '@overflow/commons-typescript/model/member'; import { Member } from '@overflow/commons-typescript/model/member';
import { MemberService } from '../../../service/member.service'; import { MemberService } from '../../../service/member.service';
@ -52,73 +47,100 @@ export class Effects {
private memberService: MemberService, private memberService: MemberService,
private router: Router private router: Router
) { } ) { }
@Effect()
signin$: Observable<Action> = this.actions$
.ofType(ActionType.Signin)
.map((action: Signin) => action.payload)
.switchMap(payload => {
return this.memberService.signin(payload.email, payload.password);
})
.map((result: { authToken: string, domainMember: DomainMember }) => {
return new SigninSuccess({ authToken: result.authToken, domainMember: result.domainMember });
})
.catch((error: RESTClientError) => {
return of(new SigninFailure(error));
});
@Effect() @Effect()
signinCookie$: Observable<Action> = this.actions$ signin$ = this.actions$.pipe(
.ofType(ActionType.SigninCookie) ofType(ActionType.Signin),
.map((action: SigninCookie) => action.payload) map((action: Signin) => action.payload),
.switchMap((payload) => { exhaustMap((signinInfo: { email: string, password: string, returnURL: string }) =>
return this.memberService.signin_cookie(payload.authToken);
})
.map((domainMember: DomainMember) => {
return new SigninCookieSuccess(domainMember);
})
.catch((error: RESTClientError) => {
return of(new SigninFailure(error));
});
@Effect()
signup$: Observable<Action> = this.actions$
.ofType(ActionType.Signup)
.map((action: Signup) => action.payload)
.exhaustMap(payload =>
this.memberService.signup(payload.member, payload.password)
.map(member => new SignupSuccess(member))
.catch(error => of(new SignupFailure(error)))
);
@Effect()
modify$: Observable<Action> = this.actions$
.ofType(ActionType.Modify)
.map((action: Modify) => action.payload)
.exhaustMap(payload =>
this.memberService.modify(payload)
.map(member => new ModifySuccess(member))
.catch(error => of(new ModifyFailure(error)))
);
@Effect()
resetPassword$: Observable<Action> = this.actions$
.ofType(ActionType.ResetPassword)
.map((action: ResetPassword) => action.payload)
.exhaustMap(payload =>
this.memberService this.memberService
.sendEmailResetPassword(payload) .signin(signinInfo.email, signinInfo.password)
.map(member => new ResetPasswordSuccess(member)) .pipe(
.catch(error => of(new ResetPasswordFailure(error))) map((result: { authToken: string, domainMember: DomainMember }) => {
return new SigninSuccess({ authToken: result.authToken, domainMember: result.domainMember, returnURL: signinInfo.returnURL });
}),
catchError(error => of(new SigninFailure(error)))
)
)
); );
@Effect() @Effect()
ModifyPassword$: Observable<Action> = this.actions$ signinCookie$ = this.actions$.pipe(
.ofType(ActionType.ModifyPassword) ofType(ActionType.SigninCookie),
.map((action: ModifyPassword) => action.payload) map((action: SigninCookie) => action.payload),
.exhaustMap(payload => exhaustMap((signinInfo: { authToken: string, returnURL: string }) =>
this.memberService this.memberService
.resetPassword(payload.token, payload.password, payload.confirmPassword) .signin_cookie(signinInfo.authToken)
.map(member => new ModifyPasswordSuccess(member)) .pipe(
.catch(error => of(new ModifyPasswordFailure(error))) map((domainMember: DomainMember) => {
return new SigninCookieSuccess({ domainMember: domainMember, returnURL: signinInfo.returnURL });
}),
catchError(error => of(new SigninCookieFailure(error)))
)
)
);
@Effect()
signup$ = this.actions$.pipe(
ofType(ActionType.Signup),
map((action: Signup) => action.payload),
exhaustMap((signupInfo: { member: Member, password: string }) =>
this.memberService
.signup(signupInfo.member, signupInfo.password)
.pipe(
map((member: Member) => {
return new SignupSuccess(member);
}),
catchError(error => of(new SignupFailure(error)))
)
)
);
@Effect()
modify$ = this.actions$.pipe(
ofType(ActionType.Modify),
map((action: Modify) => action.payload),
exhaustMap((member: Member) =>
this.memberService
.modify(member)
.pipe(
map((newMember: Member) => {
return new ModifySuccess(newMember);
}),
catchError(error => of(new ModifyFailure(error)))
)
)
);
@Effect()
resetPassword$ = this.actions$.pipe(
ofType(ActionType.ResetPassword),
map((action: ResetPassword) => action.payload),
exhaustMap((email: string) =>
this.memberService
.sendEmailResetPassword(email)
.pipe(
map((newMember: Member) => {
return new ResetPasswordSuccess(newMember);
}),
catchError(error => of(new ResetPasswordFailure(error)))
)
)
);
@Effect()
modifyPassword$ = this.actions$.pipe(
ofType(ActionType.ModifyPassword),
map((action: ModifyPassword) => action.payload),
exhaustMap((info: { token: string, password: string, confirmPassword: string }) =>
this.memberService
.resetPassword(info.token, info.password, info.confirmPassword)
.pipe(
map((newMember: Member) => {
return new ModifyPasswordSuccess(newMember);
}),
catchError(error => of(new ModifyPasswordFailure(error)))
)
)
); );
} }

View File

@ -18,7 +18,7 @@ export enum ActionType {
export class SigninSuccess implements Action { export class SigninSuccess implements Action {
readonly type = ActionType.SigninSuccess; readonly type = ActionType.SigninSuccess;
constructor(public payload: {authToken: string, domainMember: DomainMember}) {} constructor(public payload: {authToken: string, domainMember: DomainMember, returnURL: string}) {}
} }
export class SigninFailure implements Action { export class SigninFailure implements Action {
@ -30,7 +30,7 @@ export class SigninFailure implements Action {
export class SigninCookieSuccess implements Action { export class SigninCookieSuccess implements Action {
readonly type = ActionType.SigninCookieSuccess; readonly type = ActionType.SigninCookieSuccess;
constructor(public payload: DomainMember) {} constructor(public payload: {domainMember: DomainMember, returnURL: string}) {}
} }
export class SigninCookieFailure implements Action { export class SigninCookieFailure implements Action {

View File

@ -27,7 +27,7 @@ export function reducer(state: State = initialState, action: Actions): State {
case ActionType.SigninCookieSuccess: { case ActionType.SigninCookieSuccess: {
return { return {
signined: true, signined: true,
domainMember: action.payload, domainMember: action.payload.domainMember,
}; };
} }