From eb374a388b0bd34adcbcf27d0ee7935295331feb Mon Sep 17 00:00:00 2001 From: geek Date: Mon, 28 May 2018 21:37:47 +0900 Subject: [PATCH] ing --- .../member-signin-container.component.ts | 2 - .../member/service/member-totp.service.ts | 4 +- .../member/store/container/totp/index.ts | 3 + .../container/totp/member-totp.reducer.ts | 44 +++++++++ .../store/container/totp/member-totp.state.ts | 25 +++++ .../entity/member-totp/member-totp.action.ts | 4 +- .../entity/member-totp/member-totp.effect.ts | 98 +++++++++---------- .../store/entity/member/member.action.ts | 3 - .../store/entity/member/member.effect.ts | 4 - 9 files changed, 119 insertions(+), 68 deletions(-) create mode 100644 @overflow/member/store/container/totp/index.ts create mode 100644 @overflow/member/store/container/totp/member-totp.reducer.ts create mode 100644 @overflow/member/store/container/totp/member-totp.state.ts diff --git a/@overflow/member/container/member-signin-container.component.ts b/@overflow/member/container/member-signin-container.component.ts index 833de45..f2b831b 100644 --- a/@overflow/member/container/member-signin-container.component.ts +++ b/@overflow/member/container/member-signin-container.component.ts @@ -1,6 +1,4 @@ import { Component, OnInit, Input } from '@angular/core'; -import { ActivatedRoute, Router } from '@angular/router'; -import { FormGroup, FormBuilder, Validators } from '@angular/forms'; import { Store, select } from '@ngrx/store'; import { Observable } from 'rxjs'; diff --git a/@overflow/member/service/member-totp.service.ts b/@overflow/member/service/member-totp.service.ts index 8f390b1..72154ba 100644 --- a/@overflow/member/service/member-totp.service.ts +++ b/@overflow/member/service/member-totp.service.ts @@ -13,8 +13,8 @@ export class MemberTotpService { } - public createTotp(member: Member): Observable { - return this.rpcService.call('MemberTotpService.createTotp', member); + public createTotp(member: Member): Observable { + return this.rpcService.call('MemberTotpService.createTotp', member); } public regist(member: Member, secretCode: string, code: string): Observable { diff --git a/@overflow/member/store/container/totp/index.ts b/@overflow/member/store/container/totp/index.ts new file mode 100644 index 0000000..a69f856 --- /dev/null +++ b/@overflow/member/store/container/totp/index.ts @@ -0,0 +1,3 @@ +export * from './member-totp.reducer'; +export * from './member-totp.state'; + diff --git a/@overflow/member/store/container/totp/member-totp.reducer.ts b/@overflow/member/store/container/totp/member-totp.reducer.ts new file mode 100644 index 0000000..8819b6a --- /dev/null +++ b/@overflow/member/store/container/totp/member-totp.reducer.ts @@ -0,0 +1,44 @@ +import { + Actions, + ActionType, +} from '../../entity/member-totp/member-totp.action'; + +import { + State, + initialState, +} from './member-totp.state'; + +export function reducer(state = initialState, action: Actions): State { + switch (action.type) { + case ActionType.CreateTotp: { + return { + secretKey: null, + sourceURI: null, + pending: true, + error: null, + }; + } + + case ActionType.CreateTotpSuccess: { + return { + secretKey: action.payload.key, + sourceURI: action.payload.uri, + pending: false, + error: null, + }; + } + + case ActionType.CreateTotpFailure: { + return { + secretKey: null, + sourceURI: null, + pending: false, + error: action.payload, + }; + } + + default: { + return state; + } + } +} diff --git a/@overflow/member/store/container/totp/member-totp.state.ts b/@overflow/member/store/container/totp/member-totp.state.ts new file mode 100644 index 0000000..010bf0f --- /dev/null +++ b/@overflow/member/store/container/totp/member-totp.state.ts @@ -0,0 +1,25 @@ +import { Selector, createSelector } from '@ngrx/store'; +import { RESTClientError } from '@loafer/ng-rest'; + +export interface State { + secretKey: string; + sourceURI: string; + pending: boolean; + error: RESTClientError; +} + +export const initialState: State = { + secretKey: null, + sourceURI: null, + pending: false, + error: null, +}; + +export function getSelectors(selector: Selector) { + return { + selectSecretKey: createSelector(selector, (state: State) => state.secretKey), + selectSourceURI: createSelector(selector, (state: State) => state.sourceURI), + selectPending: createSelector(selector, (state: State) => state.pending), + selectError: createSelector(selector, (state: State) => state.error), + }; +} diff --git a/@overflow/member/store/entity/member-totp/member-totp.action.ts b/@overflow/member/store/entity/member-totp/member-totp.action.ts index eba1e17..8202b86 100644 --- a/@overflow/member/store/entity/member-totp/member-totp.action.ts +++ b/@overflow/member/store/entity/member-totp/member-totp.action.ts @@ -46,8 +46,6 @@ export class Regist implements Action { export class RegistSuccess implements Action { readonly type = ActionType.RegistSuccess; - - constructor(public payload: void) {} } export class RegistFailure implements Action { @@ -67,7 +65,7 @@ export class CheckCodeForMember implements Action { export class CheckCodeForMemberSuccess implements Action { readonly type = ActionType.CheckCodeForMemberSuccess; - constructor(public payload: void) {} + constructor(public payload: boolean) {} } export class CheckCodeForMemberFailure implements Action { diff --git a/@overflow/member/store/entity/member-totp/member-totp.effect.ts b/@overflow/member/store/entity/member-totp/member-totp.effect.ts index 57e0049..d14445b 100644 --- a/@overflow/member/store/entity/member-totp/member-totp.effect.ts +++ b/@overflow/member/store/entity/member-totp/member-totp.effect.ts @@ -1,20 +1,9 @@ import { Injectable } from '@angular/core'; -import { Router } from '@angular/router'; import { Effect, Actions, ofType } from '@ngrx/effects'; -import { Action } from '@ngrx/store'; - -import { Observable } from 'rxjs/Observable'; import { of } from 'rxjs/observable/of'; +import { catchError, exhaustMap, map } from 'rxjs/operators'; -import 'rxjs/add/operator/catch'; -import 'rxjs/add/operator/do'; -import 'rxjs/add/operator/exhaustMap'; -import 'rxjs/add/operator/switchMap'; -import 'rxjs/add/operator/map'; -import 'rxjs/add/operator/take'; - -import { RESTClientError } from '@loafer/ng-rest'; import { MemberTotpService } from '../../../service/member-totp.service'; import { @@ -32,6 +21,7 @@ import { ActionType, } from './member-totp.action'; +import {Member} from '@overflow/commons-typescript/model/member'; @Injectable() export class Effects { @@ -39,53 +29,53 @@ export class Effects { constructor( private actions$: Actions, private memberTotpService: MemberTotpService, - private router: Router ) { } @Effect() - createTotp$: Observable = this.actions$ - .ofType(ActionType.CreateTotp) - .map((action: CreateTotp) => action.payload) - .switchMap(payload => { - // this._returnURL = payload.returnURL; - return this.memberTotpService.createTotp(payload); - }) - .map((result: any) => { - const key = result['key']; - const uri = result['uri']; - return new CreateTotpSuccess({ key: key, uri: uri }); - }) - .catch((error: RESTClientError) => { - return of(new CreateTotpFailure(error)); - }); + createTotp$ = this.actions$.pipe( + ofType(ActionType.CreateTotp), + map(( action: CreateTotp ) => action.payload), + exhaustMap(( member: Member ) => + this.memberTotpService + .createTotp(member) + .pipe( + map((result: any) => { + return new CreateTotpSuccess({ key: result.secretKey, uri: result.sourceURI}); + }), + catchError(error => of(new CreateTotpFailure(error))) + ) + ) + ); @Effect() - regist: Observable = this.actions$ - .ofType(ActionType.Regist) - .map((action: Regist) => action.payload) - .switchMap((payload) => { - // this._returnURL = payload.returnURL; - return this.memberTotpService.regist(payload.member, payload.secretCode, payload.code); - }) - .map((result: any) => { - return new RegistSuccess(result); - }) - .catch((error: RESTClientError) => { - return of(new RegistFailure(error)); - }); - + regist$ = this.actions$.pipe( + ofType(ActionType.Regist), + map(( action: Regist ) => action.payload), + exhaustMap(( totpInfo: { member: Member, secretCode: string, code: string } ) => + this.memberTotpService + .regist(totpInfo.member, totpInfo.secretCode, totpInfo.code) + .pipe( + map(() => { + return new RegistSuccess(); + }), + catchError(error => of(new RegistFailure(error))) + ) + ) + ); @Effect() - checkCodeForMember: Observable = this.actions$ - .ofType(ActionType.CheckCodeForMember) - .map((action: Regist) => action.payload) - .switchMap((payload) => { - return this.memberTotpService.checkCodeForMember(payload.member, payload.code); - }) - .map((result: any) => { - return new CheckCodeForMemberSuccess(result); - }) - .catch((error: RESTClientError) => { - return of(new CheckCodeForMemberFailure(error)); - }); + checkCodeForMember$ = this.actions$.pipe( + ofType(ActionType.CheckCodeForMember), + map(( action: CheckCodeForMember ) => action.payload), + exhaustMap(( totpInfo: { member: Member, code: string } ) => + this.memberTotpService + .checkCodeForMember(totpInfo.member, totpInfo.code) + .pipe( + map((result: boolean) => { + return new CheckCodeForMemberSuccess(result); + }), + catchError(error => of(new CheckCodeForMemberFailure(error))) + ) + ) + ); } diff --git a/@overflow/member/store/entity/member/member.action.ts b/@overflow/member/store/entity/member/member.action.ts index 8a01793..65f3c7c 100644 --- a/@overflow/member/store/entity/member/member.action.ts +++ b/@overflow/member/store/entity/member/member.action.ts @@ -1,9 +1,6 @@ import { Action } from '@ngrx/store'; - import { RESTClientError } from '@loafer/ng-rest'; - import { Member } from '@overflow/commons-typescript/model/member'; -import { DomainMember } from '@overflow/commons-typescript/model/domain'; export enum ActionType { Signin = '[member.member] Signin', diff --git a/@overflow/member/store/entity/member/member.effect.ts b/@overflow/member/store/entity/member/member.effect.ts index fab0c2c..dc58d93 100644 --- a/@overflow/member/store/entity/member/member.effect.ts +++ b/@overflow/member/store/entity/member/member.effect.ts @@ -2,15 +2,11 @@ import { Injectable } from '@angular/core'; import { Router } from '@angular/router'; import { Effect, Actions, ofType } from '@ngrx/effects'; -import { Action } from '@ngrx/store'; - -import { Observable } from 'rxjs/Observable'; import { of } from 'rxjs'; import { catchError, exhaustMap, map, tap } from 'rxjs/operators'; import { Member } from '@overflow/commons-typescript/model/member'; import { MemberService } from '../../../service/member.service'; -import { RESTClientError } from '@loafer/ng-rest'; import { Signin,