From b674002c58114faeaf75809579c265fe46b13cfb Mon Sep 17 00:00:00 2001 From: insanity Date: Wed, 7 Mar 2018 15:23:45 +0900 Subject: [PATCH] probe list store --- src/packages/member/member.module.ts | 2 +- .../probe/component/list/list.component.html | 12 ++--- .../probe/component/list/list.component.ts | 36 ++++++------- src/packages/probe/probe-store.module.ts | 24 +++++++++ src/packages/probe/probe.constant.ts | 3 ++ src/packages/probe/probe.module.ts | 6 +++ src/packages/probe/service/probe.service.ts | 7 +++ src/packages/probe/store/index.ts | 13 +++++ src/packages/probe/store/list/index.ts | 4 ++ src/packages/probe/store/list/probe.action.ts | 38 ++++++++++++++ .../probe/store/list/probe.effect.spec.ts | 15 ++++++ src/packages/probe/store/list/probe.effect.ts | 51 +++++++++++++++++++ .../probe/store/list/probe.reducer.ts | 50 ++++++++++++++++++ src/packages/probe/store/list/probe.state.ts | 19 +++++++ yarn.lock | 6 +++ 15 files changed, 261 insertions(+), 25 deletions(-) create mode 100644 src/packages/probe/probe-store.module.ts create mode 100644 src/packages/probe/probe.constant.ts create mode 100644 src/packages/probe/store/index.ts create mode 100644 src/packages/probe/store/list/index.ts create mode 100644 src/packages/probe/store/list/probe.action.ts create mode 100644 src/packages/probe/store/list/probe.effect.spec.ts create mode 100644 src/packages/probe/store/list/probe.effect.ts create mode 100644 src/packages/probe/store/list/probe.reducer.ts create mode 100644 src/packages/probe/store/list/probe.state.ts diff --git a/src/packages/member/member.module.ts b/src/packages/member/member.module.ts index 4a43768..e539a73 100644 --- a/src/packages/member/member.module.ts +++ b/src/packages/member/member.module.ts @@ -5,7 +5,7 @@ import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { MaterialModule } from 'app/commons/ui/material/material.module'; -import { AuthGuard } from './guard/auth.guard'; +// import { AuthGuard } from './guard/auth.guard'; import { MemberStoreModule } from './member-store.module'; diff --git a/src/packages/probe/component/list/list.component.html b/src/packages/probe/component/list/list.component.html index 3721ce5..422fbd3 100644 --- a/src/packages/probe/component/list/list.component.html +++ b/src/packages/probe/component/list/list.component.html @@ -3,17 +3,17 @@ Name - {{element.name}} + {{element.displayName}} IP - {{element.ip}} + {{element.host.ip}} OS - {{element.os}} + {{element.host.os.meta}} @@ -23,17 +23,17 @@ Targets - {{element.targetCnt}} + {{element.targets.length}} Date - {{element.date}} + {{element.authorizeDate.toLocaleString()}} AuthBy - {{element.authBy}} + {{element.authorizeMember.name}} diff --git a/src/packages/probe/component/list/list.component.ts b/src/packages/probe/component/list/list.component.ts index 913d43f..6cdbdc2 100644 --- a/src/packages/probe/component/list/list.component.ts +++ b/src/packages/probe/component/list/list.component.ts @@ -2,17 +2,7 @@ import { Component, OnInit, AfterViewInit, ViewChild } from '@angular/core'; import { MatTableDataSource, MatSort } from '@angular/material'; import { AfterContentInit } from '@angular/core/src/metadata/lifecycle_hooks'; import { Router } from '@angular/router'; - -export interface Probe { - id: string; - name: string; - ip: string; - os: string; - cidr: string; - targetCnt: number; - date: string; - authBy: string; -} +import { Probe } from '../../model'; @Component({ @@ -35,14 +25,24 @@ export class ListComponent implements OnInit, AfterContentInit { const data: Probe[] = new Array(); for (let i = 0; i < 10; i++) { const p: Probe = { - id: String('id' + i), - name: String('name' + i), - ip: String('ip' + i), - os: String('os' + i), + id: i, + displayName: String('displayName' + i), + host: { + ip: i, + os: { + meta: 'blahblahblah' + }, + }, cidr: String('cidr' + i), - targetCnt: i, - date: String('date' + i), - authBy: String('insanity') + targets: [ + { + id: i, + }, + ], + authorizeDate: new Date(), + authorizeMember: { + 'name': String('insanity') + }, }; data.push(p); } diff --git a/src/packages/probe/probe-store.module.ts b/src/packages/probe/probe-store.module.ts new file mode 100644 index 0000000..50f957b --- /dev/null +++ b/src/packages/probe/probe-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 './probe.constant'; + +@NgModule({ + imports: [ + StoreModule.forFeature(MODULE.name, REDUCERS), + EffectsModule.forFeature(EFFECTS), + ], +}) +export class ProbeStoreModule { } diff --git a/src/packages/probe/probe.constant.ts b/src/packages/probe/probe.constant.ts new file mode 100644 index 0000000..e12f632 --- /dev/null +++ b/src/packages/probe/probe.constant.ts @@ -0,0 +1,3 @@ +export const MODULE = { + name: 'probe' +}; diff --git a/src/packages/probe/probe.module.ts b/src/packages/probe/probe.module.ts index c2b9b72..0acbb83 100644 --- a/src/packages/probe/probe.module.ts +++ b/src/packages/probe/probe.module.ts @@ -4,12 +4,15 @@ import { MaterialModule } from 'app/commons/ui/material/material.module'; import { InfoTableModule } from 'app/commons/component/info-table/info-table.module'; import { COMPONENTS } from './component'; +import { ProbeStoreModule } from './probe-store.module'; +import { SERVICES } from './service'; @NgModule({ imports: [ CommonModule, MaterialModule, InfoTableModule, + ProbeStoreModule ], declarations: [ COMPONENTS, @@ -17,5 +20,8 @@ import { COMPONENTS } from './component'; exports: [ COMPONENTS, ], + providers: [ + SERVICES, + ], }) export class ProbeModule { } diff --git a/src/packages/probe/service/probe.service.ts b/src/packages/probe/service/probe.service.ts index 3aaac2a..685925e 100644 --- a/src/packages/probe/service/probe.service.ts +++ b/src/packages/probe/service/probe.service.ts @@ -5,6 +5,10 @@ import 'rxjs/add/operator/map'; import { RPCService } from 'packages/commons/service/rpc.service'; +import { Domain } from 'packages/domain/model'; + +import { Probe } from '../model'; + @Injectable() export class ProbeService { @@ -15,4 +19,7 @@ export class ProbeService { } + public readAllByDomain(domain: Domain): Observable { + return this.rpcService.call('ProbeService.readAllByDomain', domain); + } } diff --git a/src/packages/probe/store/index.ts b/src/packages/probe/store/index.ts new file mode 100644 index 0000000..2f1ff23 --- /dev/null +++ b/src/packages/probe/store/index.ts @@ -0,0 +1,13 @@ +import * as ProbeStore from './list'; + +export interface State { + probe: ProbeStore.State; +} + +export const REDUCERS = { + probe: ProbeStore.reducer, +}; + +export const EFFECTS = [ + ProbeStore.Effects, +]; diff --git a/src/packages/probe/store/list/index.ts b/src/packages/probe/store/list/index.ts new file mode 100644 index 0000000..d8f12e5 --- /dev/null +++ b/src/packages/probe/store/list/index.ts @@ -0,0 +1,4 @@ +export * from './probe.action'; +export * from './probe.effect'; +export * from './probe.reducer'; +export * from './probe.state'; diff --git a/src/packages/probe/store/list/probe.action.ts b/src/packages/probe/store/list/probe.action.ts new file mode 100644 index 0000000..fae1ee2 --- /dev/null +++ b/src/packages/probe/store/list/probe.action.ts @@ -0,0 +1,38 @@ +import { Action } from '@ngrx/store'; + +import { ErrorResponse } from 'packages/commons/service/error-response'; + +import { Domain } from 'packages/domain/model'; +import { Probe } from '../../model'; + + +export enum ActionType { + ReadAllByDomain = '[probe.list] ReadAllByDomain', + ReadAllByDomainSuccess = '[probe.list] ReadAllByDomainSuccess', + ReadAllByDomainFailure = '[probe.list] ReadAllByDomainFailure', +} + +export class ReadAllByDomain implements Action { + readonly type = ActionType.ReadAllByDomain; + + constructor(public payload: Domain) {} +} + +export class ReadAllByDomainSuccess implements Action { + readonly type = ActionType.ReadAllByDomainSuccess; + + constructor(public payload: Probe[]) {} +} + +export class ReadAllByDomainFailure implements Action { + readonly type = ActionType.ReadAllByDomainFailure; + + constructor(public payload: ErrorResponse) {} +} + + +export type Actions = + | ReadAllByDomain + | ReadAllByDomainSuccess + | ReadAllByDomainFailure +; diff --git a/src/packages/probe/store/list/probe.effect.spec.ts b/src/packages/probe/store/list/probe.effect.spec.ts new file mode 100644 index 0000000..d405009 --- /dev/null +++ b/src/packages/probe/store/list/probe.effect.spec.ts @@ -0,0 +1,15 @@ +import { TestBed, inject } from '@angular/core/testing'; + +import { Effects } from './probe.effect'; + +describe('NoAuth-Probe.Effects', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [Effects] + }); + }); + + it('should be created', inject([Effects], (effects: Effects) => { + expect(effects).toBeTruthy(); + })); +}); diff --git a/src/packages/probe/store/list/probe.effect.ts b/src/packages/probe/store/list/probe.effect.ts new file mode 100644 index 0000000..d886e44 --- /dev/null +++ b/src/packages/probe/store/list/probe.effect.ts @@ -0,0 +1,51 @@ +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 { Domain } from 'packages/domain/model'; + +import { Probe } from '../../model'; +import { ProbeService } from '../../service/probe.service'; + +import { + ReadAllByDomain, + ReadAllByDomainFailure, + ReadAllByDomainSuccess, + ActionType +} from './probe.action'; + +@Injectable() +export class Effects { + + constructor( + private actions$: Actions, + private probeService: ProbeService, + private router: Router + ) { } + + @Effect() + readAllByDomain$: Observable = this.actions$ + .ofType(ActionType.ReadAllByDomain) + .map((action: ReadAllByDomain) => action.payload) + .switchMap(payload => this.probeService.readAllByDomain(payload)) + .map(probes => { + return new ReadAllByDomainSuccess(probes); + }) + .catch((error: ErrorResponse) => { + return of(new ReadAllByDomainFailure(error)); + }); +} diff --git a/src/packages/probe/store/list/probe.reducer.ts b/src/packages/probe/store/list/probe.reducer.ts new file mode 100644 index 0000000..02e1fa9 --- /dev/null +++ b/src/packages/probe/store/list/probe.reducer.ts @@ -0,0 +1,50 @@ +import { ErrorResponse } from 'packages/commons/service/error-response'; + +import { + ReadAllByDomain, + ReadAllByDomainFailure, + ReadAllByDomainSuccess, + ActionType, + Actions, +} from './probe.action'; + +import { + State, + initialState, +} from './probe.state'; + +import { Probe } from '../../model'; + +export function reducer(state = initialState, action: Actions): State { + switch (action.type) { + case ActionType.ReadAllByDomain: { + return { + ...state, + error: null, + isPending: true, + }; + } + + case ActionType.ReadAllByDomainSuccess: { + return { + ...state, + error: null, + isPending: false, + probes: action.payload, + }; + } + + case ActionType.ReadAllByDomainFailure: { + return { + ...state, + error: action.payload, + isPending: false, + probes: null, + }; + } + + default: { + return state; + } + } +} diff --git a/src/packages/probe/store/list/probe.state.ts b/src/packages/probe/store/list/probe.state.ts new file mode 100644 index 0000000..f5e8f20 --- /dev/null +++ b/src/packages/probe/store/list/probe.state.ts @@ -0,0 +1,19 @@ +import { ErrorResponse } from 'packages/commons/service/error-response'; + +import { Probe } from '../../model'; + +export interface State { + error: ErrorResponse | null; + isPending: boolean; + probes: Probe[] | null; +} + +export const initialState: State = { + error: null, + isPending: false, + probes: null, +}; + +export const getProbes = (state: State) => state.probes; +export const getError = (state: State) => state.error; +export const isPending = (state: State) => state.isPending; diff --git a/yarn.lock b/yarn.lock index c18ba11..cff6d60 100644 --- a/yarn.lock +++ b/yarn.lock @@ -403,6 +403,12 @@ amqplib@^0.5.2: readable-stream "1.x >=1.1.9" safe-buffer "^5.0.1" +angular-l10n@^4.1.5: + version "4.1.5" + resolved "https://registry.yarnpkg.com/angular-l10n/-/angular-l10n-4.1.5.tgz#4b65cdb819be4daaaadc006ecf873d6ed1fd0fd4" + dependencies: + tslib "^1.7.1" + ansi-html@0.0.7: version "0.0.7" resolved "https://registry.yarnpkg.com/ansi-html/-/ansi-html-0.0.7.tgz#813584021962a9e9e6fd039f940d12f56ca7859e"