From b970bac4bd0b3d417fd7fcca792d83c5258ba6be Mon Sep 17 00:00:00 2001 From: snoop Date: Wed, 7 Mar 2018 17:24:33 +0900 Subject: [PATCH] added discovery modules --- .../discovery/discovery-store.module.ts | 24 ++++++ src/packages/discovery/discovery.constant.ts | 3 + src/packages/discovery/discovery.module.ts | 10 ++- src/packages/discovery/model/index.ts | 7 ++ .../service/discovery.service.spec.ts | 15 ++++ .../discovery/service/discovery.service.ts | 2 +- src/packages/discovery/service/index.ts | 5 ++ src/packages/discovery/store/index.ts | 14 ++++ src/packages/discovery/store/setting/index.ts | 4 + .../discovery/store/setting/setting.action.ts | 42 +++++++++++ .../store/setting/setting.effect.spec.ts | 15 ++++ .../discovery/store/setting/setting.effect.ts | 74 +++++++++++++++++++ .../store/setting/setting.reducer.ts | 50 +++++++++++++ .../discovery/store/setting/setting.state.ts | 22 ++++++ 14 files changed, 285 insertions(+), 2 deletions(-) create mode 100644 src/packages/discovery/discovery-store.module.ts create mode 100644 src/packages/discovery/discovery.constant.ts create mode 100644 src/packages/discovery/model/index.ts create mode 100644 src/packages/discovery/store/index.ts create mode 100644 src/packages/discovery/store/setting/index.ts create mode 100644 src/packages/discovery/store/setting/setting.action.ts create mode 100644 src/packages/discovery/store/setting/setting.effect.spec.ts create mode 100644 src/packages/discovery/store/setting/setting.effect.ts create mode 100644 src/packages/discovery/store/setting/setting.reducer.ts create mode 100644 src/packages/discovery/store/setting/setting.state.ts diff --git a/src/packages/discovery/discovery-store.module.ts b/src/packages/discovery/discovery-store.module.ts new file mode 100644 index 0000000..5e6c480 --- /dev/null +++ b/src/packages/discovery/discovery-store.module.ts @@ -0,0 +1,24 @@ +import { NgModule } from '@angular/core'; +import { StoreModule } from '@ngrx/store'; +import { StoreDevtoolsModule } from '@ngrx/store-devtools'; +import { + StoreRouterConnectingModule, + RouterStateSerializer, +} from '@ngrx/router-store'; +import { EffectsModule } from '@ngrx/effects'; +import { combineReducers, ActionReducer, ActionReducerMap, MetaReducer } from '@ngrx/store'; + +import { + REDUCERS, + EFFECTS, +} from './store'; + +import { MODULE } from './discovery.constant'; + +@NgModule({ + imports: [ + StoreModule.forFeature(MODULE.name, REDUCERS), + EffectsModule.forFeature(EFFECTS), + ], +}) +export class MemberStoreModule { } diff --git a/src/packages/discovery/discovery.constant.ts b/src/packages/discovery/discovery.constant.ts new file mode 100644 index 0000000..e3c6d68 --- /dev/null +++ b/src/packages/discovery/discovery.constant.ts @@ -0,0 +1,3 @@ +export const MODULE = { + name: 'discovery' + }; diff --git a/src/packages/discovery/discovery.module.ts b/src/packages/discovery/discovery.module.ts index b1d1219..5f743ef 100644 --- a/src/packages/discovery/discovery.module.ts +++ b/src/packages/discovery/discovery.module.ts @@ -4,10 +4,15 @@ import { CommonModule } from '@angular/common'; import { COMPONENTS } from './component'; import { MaterialModule } from 'app/commons/ui/material/material.module'; +import { MemberStoreModule } from './discovery-store.module'; + +import { SERVICES } from './service'; + @NgModule({ imports: [ CommonModule, - MaterialModule + MaterialModule, + MemberStoreModule ], declarations: [ COMPONENTS @@ -15,5 +20,8 @@ import { MaterialModule } from 'app/commons/ui/material/material.module'; exports: [ COMPONENTS, ], + providers: [ + SERVICES, + ], }) export class DiscoveryModule { } diff --git a/src/packages/discovery/model/index.ts b/src/packages/discovery/model/index.ts new file mode 100644 index 0000000..c59956a --- /dev/null +++ b/src/packages/discovery/model/index.ts @@ -0,0 +1,7 @@ +export * from './DiscoveryStartInfo'; +export * from './Host'; +export * from './Port'; +export * from './PortType'; +export * from './Service'; +export * from './Zone'; + diff --git a/src/packages/discovery/service/discovery.service.spec.ts b/src/packages/discovery/service/discovery.service.spec.ts index e69de29..5f2d867 100644 --- a/src/packages/discovery/service/discovery.service.spec.ts +++ b/src/packages/discovery/service/discovery.service.spec.ts @@ -0,0 +1,15 @@ +import { TestBed, inject } from '@angular/core/testing'; + +import { DiscoveryService } from './discovery.service'; + +describe('MemberService', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [DiscoveryService] + }); + }); + + it('should be created', inject([DiscoveryService], (service: DiscoveryService) => { + expect(service).toBeTruthy(); + })); +}); diff --git a/src/packages/discovery/service/discovery.service.ts b/src/packages/discovery/service/discovery.service.ts index a008abd..5efb335 100644 --- a/src/packages/discovery/service/discovery.service.ts +++ b/src/packages/discovery/service/discovery.service.ts @@ -5,7 +5,7 @@ import 'rxjs/add/operator/map'; import { RESTService } from 'packages/commons/service/rest.service'; -import { DiscoveryStartInfo } from '../model/DiscoveryStartInfo'; +import { DiscoveryStartInfo } from '../model'; @Injectable() export class DiscoveryService { diff --git a/src/packages/discovery/service/index.ts b/src/packages/discovery/service/index.ts index e69de29..f19161b 100644 --- a/src/packages/discovery/service/index.ts +++ b/src/packages/discovery/service/index.ts @@ -0,0 +1,5 @@ +import { DiscoveryService } from './discovery.service'; + +export const SERVICES = [ + DiscoveryService, +]; diff --git a/src/packages/discovery/store/index.ts b/src/packages/discovery/store/index.ts new file mode 100644 index 0000000..a82a213 --- /dev/null +++ b/src/packages/discovery/store/index.ts @@ -0,0 +1,14 @@ +import * as SettingStore from './setting'; + + +export interface State { + setting: SettingStore.State; +} + +export const REDUCERS = { + setting: SettingStore.reducer, +}; + +export const EFFECTS = [ + SettingStore.Effects, +]; diff --git a/src/packages/discovery/store/setting/index.ts b/src/packages/discovery/store/setting/index.ts new file mode 100644 index 0000000..1393479 --- /dev/null +++ b/src/packages/discovery/store/setting/index.ts @@ -0,0 +1,4 @@ +export * from './setting.action'; +export * from './setting.effect'; +export * from './setting.reducer'; +export * from './setting.state'; diff --git a/src/packages/discovery/store/setting/setting.action.ts b/src/packages/discovery/store/setting/setting.action.ts new file mode 100644 index 0000000..83c4d19 --- /dev/null +++ b/src/packages/discovery/store/setting/setting.action.ts @@ -0,0 +1,42 @@ +import { Action } from '@ngrx/store'; + +import { ErrorResponse } from 'packages/commons/service/error-response'; + +import { DiscoveryStartInfo } from '../../model'; + +export enum ActionType { + Setting = '[discovery.setting] Setting', + SettingSuccess = '[discovery.setting] SettingSuccess', + SettingFailure = '[discovery.setting] SettingFailure', + SettingRedirect = '[discovery.setting] SettingRedirect', +} + +export class Setting implements Action { + readonly type = ActionType.Setting; + + constructor(public payload: { payload: DiscoveryStartInfo}) {} +} + +export class SettingSuccess implements Action { + readonly type = ActionType.SettingSuccess; + + constructor(public payload: DiscoveryStartInfo) {} +} + +export class SettingFailure implements Action { + readonly type = ActionType.SettingFailure; + + constructor(public payload: ErrorResponse) {} +} + +export class SettingRedirect implements Action { + readonly type = ActionType.SettingRedirect; +} + + +export type Actions = + | Setting + | SettingSuccess + | SettingFailure + | SettingRedirect +; diff --git a/src/packages/discovery/store/setting/setting.effect.spec.ts b/src/packages/discovery/store/setting/setting.effect.spec.ts new file mode 100644 index 0000000..cbfff71 --- /dev/null +++ b/src/packages/discovery/store/setting/setting.effect.spec.ts @@ -0,0 +1,15 @@ +import { TestBed, inject } from '@angular/core/testing'; + +import { Effects } from './setting.effect'; + +describe('Auth.Effects', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [Effects] + }); + }); + + it('should be created', inject([Effects], (effects: Effects) => { + expect(effects).toBeTruthy(); + })); +}); diff --git a/src/packages/discovery/store/setting/setting.effect.ts b/src/packages/discovery/store/setting/setting.effect.ts new file mode 100644 index 0000000..508b5d7 --- /dev/null +++ b/src/packages/discovery/store/setting/setting.effect.ts @@ -0,0 +1,74 @@ +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 '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 { ErrorResponse } from 'packages/commons/service/error-response'; + +import { DiscoveryStartInfo } from '../../model'; +import { DiscoveryService } from '../../service/discovery.service'; + +import { + Setting, + SettingSuccess, + SettingFailure, + ActionType, +} from './setting.action'; + +@Injectable() +export class Effects { + + constructor( + private actions$: Actions, + private discoveryService: DiscoveryService, + private router: Router + ) { } + + @Effect() + signin$: Observable = this.actions$ + .ofType(ActionType.Setting) + .map((action: Setting) => action.payload) + .switchMap(payload => this.discoveryService.start(payload.payload)) + .map(discoveryStartInfo => { + return new SettingSuccess(discoveryStartInfo); + }) + .catch((error: ErrorResponse) => { + return of(new SettingFailure(error)); + }); + + // .map((action: Signin) => action.payload) + // .exhaustMap(payload => + // this.memberService + // .signin(payload.email, payload.password) + // .map(member => { + // return new SigninSuccess(member); + // }) + // .catch((error) => { + // return of(new SigninFailure(error.message)); + // }) + // ); + + @Effect({ dispatch: false }) + signinSuccess$ = this.actions$ + .ofType(ActionType.SettingSuccess) + .do(() => this.router.navigate(['/'])); + + @Effect({ dispatch: false }) + signinRedirect$ = this.actions$ + .ofType(ActionType.SettingRedirect) + .do(authed => { + this.router.navigate(['/login']); + }); + +} diff --git a/src/packages/discovery/store/setting/setting.reducer.ts b/src/packages/discovery/store/setting/setting.reducer.ts new file mode 100644 index 0000000..bc9b9ad --- /dev/null +++ b/src/packages/discovery/store/setting/setting.reducer.ts @@ -0,0 +1,50 @@ +import { ErrorResponse } from 'packages/commons/service/error-response'; + +import { + Actions, + ActionType, + Setting, +} from './setting.action'; + +import { + State, + initialState, +} from './setting.state'; + +import { DiscoveryStartInfo } from '../../model'; + +export function reducer(state = initialState, action: Actions): State { + switch (action.type) { + case ActionType.Setting: { + return { + ...state, + error: null, + isPending: true, + }; + } + + case ActionType.SettingSuccess: { + return { + ...state, + isStart: true, + error: null, + isPending: false, + discoveryStartInfo: action.payload, + }; + } + + case ActionType.SettingFailure: { + return { + ...state, + isStart: false, + error: action.payload, + isPending: false, + discoveryStartInfo: null, + }; + } + + default: { + return state; + } + } +} diff --git a/src/packages/discovery/store/setting/setting.state.ts b/src/packages/discovery/store/setting/setting.state.ts new file mode 100644 index 0000000..6f2af78 --- /dev/null +++ b/src/packages/discovery/store/setting/setting.state.ts @@ -0,0 +1,22 @@ +import { ErrorResponse } from 'packages/commons/service/error-response'; + +import { DiscoveryStartInfo } from '../../model'; + +export interface State { + isStart: boolean; + error: ErrorResponse | null; + isPending: boolean; + discoveryStartInfo: DiscoveryStartInfo | null; +} + +export const initialState: State = { + isStart: false, + error: null, + isPending: false, + discoveryStartInfo: null, +}; + +export const isStart = (state: State) => state.isStart; +export const getDiscoveryStartInfo = (state: State) => state.discoveryStartInfo; +export const getError = (state: State) => state.error; +export const isPending = (state: State) => state.isPending;