From 31a36dc2df460f91dfcd2598d14162f5cd69f781 Mon Sep 17 00:00:00 2001 From: insanity Date: Fri, 27 Apr 2018 16:31:27 +0900 Subject: [PATCH] test subscription --- .../component/detail/detail.component.html | 12 +++-- .../component/detail/detail.component.ts | 39 +++++++++++++-- src/packages/probe/service/probe.service.ts | 4 ++ .../probe/store/detail/detail.action.ts | 3 ++ .../probe/store/detail/detail.state.ts | 23 --------- src/packages/probe/store/index.ts | 12 ++++- src/packages/probe/store/modify/index.ts | 4 ++ .../probe/store/modify/modify.action.ts | 37 ++++++++++++++ .../probe/store/modify/modify.effect.spec.ts | 15 ++++++ .../probe/store/modify/modify.effect.ts | 49 +++++++++++++++++++ .../probe/store/modify/modify.reducer.ts | 46 +++++++++++++++++ .../probe/store/modify/modify.state.ts | 18 +++++++ 12 files changed, 230 insertions(+), 32 deletions(-) create mode 100644 src/packages/probe/store/modify/index.ts create mode 100644 src/packages/probe/store/modify/modify.action.ts create mode 100644 src/packages/probe/store/modify/modify.effect.spec.ts create mode 100644 src/packages/probe/store/modify/modify.effect.ts create mode 100644 src/packages/probe/store/modify/modify.reducer.ts create mode 100644 src/packages/probe/store/modify/modify.state.ts diff --git a/src/packages/probe/component/detail/detail.component.html b/src/packages/probe/component/detail/detail.component.html index 868f349..d2ad479 100644 --- a/src/packages/probe/component/detail/detail.component.html +++ b/src/packages/probe/component/detail/detail.component.html @@ -1,4 +1,5 @@

Info

+ @@ -6,10 +7,15 @@
+
+ + + + + +
+
-
- -
diff --git a/src/packages/probe/component/detail/detail.component.ts b/src/packages/probe/component/detail/detail.component.ts index 7ac90f2..fd589c4 100644 --- a/src/packages/probe/component/detail/detail.component.ts +++ b/src/packages/probe/component/detail/detail.component.ts @@ -1,12 +1,14 @@ -import { Component, OnInit, Inject, AfterContentInit } from '@angular/core'; +import { Component, OnInit, Inject, AfterContentInit, OnDestroy } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import { Store, select } from '@ngrx/store'; import { RPCClientError } from '@loafer/ng-rpc/protocol'; import * as DetailStore from '../../store/detail'; -import { DetailSelector } from '../../store'; +import * as ModifyStore from '../../store/modify'; +import { DetailSelector, ModifySelector } from '../../store'; import { Probe } from '../../model'; import { ConfirmationService } from 'primeng/primeng'; import * as CIDR from 'ip-cidr'; +import { Subscription } from 'rxjs/Subscription'; // import { SettingComponent as DiscoverySettingComponent } from 'packages/discovery/component/setting/setting.component'; @@ -15,9 +17,12 @@ import * as CIDR from 'ip-cidr'; templateUrl: './detail.component.html', providers: [ConfirmationService] }) -export class DetailComponent implements OnInit, AfterContentInit { +export class DetailComponent implements OnInit, AfterContentInit, OnDestroy { + probeSubscription$: Subscription; + modifySuccessSubscription$: Subscription; probe$ = this.detailStore.pipe(select(DetailSelector.select('probe'))); + modifySuccess$ = this.modifyStore.pipe(select(ModifySelector.select('probe'))); probe: Probe = null; IPRange: string; display = false; @@ -26,11 +31,12 @@ export class DetailComponent implements OnInit, AfterContentInit { private route: ActivatedRoute, private router: Router, private detailStore: Store, + private modifyStore: Store, private confirmationService: ConfirmationService ) { } ngOnInit() { - this.probe$.subscribe( + this.probeSubscription$ = this.probe$.subscribe( (probe: Probe) => { if (probe) { this.probe = probe; @@ -41,6 +47,23 @@ export class DetailComponent implements OnInit, AfterContentInit { console.log(error.response.message); } ); + this.modifySuccessSubscription$ = this.modifySuccess$.subscribe( + (probe: Probe) => { + if (probe) { + console.log('modify success'); + } else { + console.log('modify fail'); + } + }, + (error: RPCClientError) => { + console.log(error.response.message); + } + ); + } + + ngOnDestroy() { + this.probeSubscription$.unsubscribe(); + this.modifySuccessSubscription$.unsubscribe(); } ngAfterContentInit() { @@ -80,5 +103,13 @@ export class DetailComponent implements OnInit, AfterContentInit { } }); } + + onDisplayNameChange(value: string) { + this.probe.displayName = value; + this.modifyStore.dispatch( + new ModifyStore.Modify(this.probe) + ); + } + } diff --git a/src/packages/probe/service/probe.service.ts b/src/packages/probe/service/probe.service.ts index bbe273a..84602f4 100644 --- a/src/packages/probe/service/probe.service.ts +++ b/src/packages/probe/service/probe.service.ts @@ -27,4 +27,8 @@ export class ProbeService { return this.rpcService.call('ProbeService.read', id); } + public modify(probe: Probe): Observable { + return this.rpcService.call('ProbeService.modify', probe); + } + } diff --git a/src/packages/probe/store/detail/detail.action.ts b/src/packages/probe/store/detail/detail.action.ts index e6a02bf..be8c298 100644 --- a/src/packages/probe/store/detail/detail.action.ts +++ b/src/packages/probe/store/detail/detail.action.ts @@ -9,6 +9,9 @@ export enum ActionType { Read = '[probe.detail] Read', ReadSuccess = '[probe.detail] ReadSuccess', ReadFailure = '[probe.detail] ReadFailure', + Modify = '[probe.detail] Modify', + ModifySuccess = '[probe.detail] ModifySuccess', + ModifyFailure = '[probe.detail] ModifyFailure', } export class Read implements Action { diff --git a/src/packages/probe/store/detail/detail.state.ts b/src/packages/probe/store/detail/detail.state.ts index 15b3e4f..0284fdc 100644 --- a/src/packages/probe/store/detail/detail.state.ts +++ b/src/packages/probe/store/detail/detail.state.ts @@ -2,19 +2,6 @@ import { RPCClientError } from '@loafer/ng-rpc/protocol'; import { Probe } from '../../model'; -// export interface State { -// error: RPCError | null; -// isPending: boolean; -// probe: Probe | null; -// } - -// export const initialState: State = { -// error: null, -// isPending: false, -// probe: null, -// }; - - import { createEntityAdapter, EntityAdapter, EntityState } from '@ngrx/entity'; export interface State extends EntityState { @@ -29,13 +16,3 @@ export const initialState: State = adapter.getInitialState({ probe: null, }); -export const getProbe = (state: State) => state.probe; -export const getError = (state: State) => state.error; -export const isPending = (state: State) => state.isPending; - -// export const { -// selectIds, -// selectEntities, -// selectAll, -// selectTotal, -// } = adapter.getSelectors(); diff --git a/src/packages/probe/store/index.ts b/src/packages/probe/store/index.ts index 293eefc..793b4a7 100644 --- a/src/packages/probe/store/index.ts +++ b/src/packages/probe/store/index.ts @@ -12,26 +12,30 @@ import * as ProbeListStore from './list'; import * as ProbeDetailStore from './detail'; import * as ProbeHostStore from './probe-host'; import * as ProbeHostListStore from './probe-host-list'; +import * as ProbeModifyStore from './modify'; export interface State { list: ProbeListStore.State; detail: ProbeDetailStore.State; probeHost: ProbeHostStore.State; probeHosts: ProbeHostListStore.State; + modify: ProbeModifyStore.State; } export const REDUCERS = { list: ProbeListStore.reducer, detail: ProbeDetailStore.reducer, probeHost: ProbeHostStore.reducer, - probeHosts: ProbeHostListStore.reducer + probeHosts: ProbeHostListStore.reducer, + modify: ProbeModifyStore.reducer }; export const EFFECTS = [ ProbeListStore.Effects, ProbeDetailStore.Effects, ProbeHostStore.Effects, - ProbeHostListStore.Effects + ProbeHostListStore.Effects, + ProbeModifyStore.Effects ]; export const selectProbeState = createFeatureSelector(MODULE.name); @@ -44,6 +48,10 @@ export const DetailSelector = new StateSelector(createSe selectProbeState, (state: State) => state.detail )); +export const ModifySelector = new StateSelector(createSelector( + selectProbeState, + (state: State) => state.modify +)); export const ProbeHostSelector = new StateSelector(createSelector( selectProbeState, (state: State) => state.probeHost diff --git a/src/packages/probe/store/modify/index.ts b/src/packages/probe/store/modify/index.ts new file mode 100644 index 0000000..9f23ca0 --- /dev/null +++ b/src/packages/probe/store/modify/index.ts @@ -0,0 +1,4 @@ +export * from './modify.action'; +export * from './modify.effect'; +export * from './modify.reducer'; +export * from './modify.state'; diff --git a/src/packages/probe/store/modify/modify.action.ts b/src/packages/probe/store/modify/modify.action.ts new file mode 100644 index 0000000..8f21838 --- /dev/null +++ b/src/packages/probe/store/modify/modify.action.ts @@ -0,0 +1,37 @@ +import { Action } from '@ngrx/store'; + +import { RPCClientError } from '@loafer/ng-rpc/protocol'; + +import { Probe } from '../../model'; + + +export enum ActionType { + Modify = '[probe.detail] Modify', + ModifySuccess = '[probe.detail] ModifySuccess', + ModifyFailure = '[probe.detail] ModifyFailure', +} + +export class Modify implements Action { + readonly type = ActionType.Modify; + + constructor(public payload: Probe) {} +} + +export class ModifySuccess implements Action { + readonly type = ActionType.ModifySuccess; + + constructor(public payload: Probe) {} +} + +export class ModifyFailure implements Action { + readonly type = ActionType.ModifyFailure; + + constructor(public payload: RPCClientError) {} +} + + +export type Actions = + | Modify + | ModifySuccess + | ModifyFailure +; diff --git a/src/packages/probe/store/modify/modify.effect.spec.ts b/src/packages/probe/store/modify/modify.effect.spec.ts new file mode 100644 index 0000000..b1c2217 --- /dev/null +++ b/src/packages/probe/store/modify/modify.effect.spec.ts @@ -0,0 +1,15 @@ +import { TestBed, inject } from '@angular/core/testing'; + +import { Effects } from './detail.effect'; + +describe('ProbeDetail.Effects', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [Effects] + }); + }); + + it('should be created', inject([Effects], (effects: Effects) => { + expect(effects).toBeTruthy(); + })); +}); diff --git a/src/packages/probe/store/modify/modify.effect.ts b/src/packages/probe/store/modify/modify.effect.ts new file mode 100644 index 0000000..2b34101 --- /dev/null +++ b/src/packages/probe/store/modify/modify.effect.ts @@ -0,0 +1,49 @@ +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 { RPCClientError } from '@loafer/ng-rpc/protocol'; + +import { Probe } from '../../model'; +import { ProbeService } from '../../service/probe.service'; + +import { + Modify, + ModifySuccess, + ModifyFailure, + ActionType +} from './modify.action'; + +@Injectable() +export class Effects { + + constructor( + private actions$: Actions, + private probeService: ProbeService, + private router: Router + ) { } + + @Effect() + modify$: Observable = this.actions$ + .ofType(ActionType.Modify) + .map((action: Modify) => action.payload) + .switchMap(payload => this.probeService.modify(payload)) + .map(probe => { + return new ModifySuccess(probe); + }) + .catch((error: RPCClientError) => { + return of(new ModifyFailure(error)); + }); +} diff --git a/src/packages/probe/store/modify/modify.reducer.ts b/src/packages/probe/store/modify/modify.reducer.ts new file mode 100644 index 0000000..f3542b0 --- /dev/null +++ b/src/packages/probe/store/modify/modify.reducer.ts @@ -0,0 +1,46 @@ +import { + ActionType, + Actions, +} from './modify.action'; + +import { + State, + initialState, +} from './modify.state'; + +import { Probe } from '../../model'; + + +export function reducer(state = initialState, action: Actions): State { + switch (action.type) { + case ActionType.Modify: { + return { + ...state, + error: null, + isPending: true, + }; + } + + case ActionType.ModifySuccess: { + return { + ...state, + error: null, + isPending: false, + probe: action.payload, + }; + } + + case ActionType.ModifyFailure: { + return { + ...state, + error: action.payload, + isPending: false, + probe: null, + }; + } + + default: { + return state; + } + } +} diff --git a/src/packages/probe/store/modify/modify.state.ts b/src/packages/probe/store/modify/modify.state.ts new file mode 100644 index 0000000..0284fdc --- /dev/null +++ b/src/packages/probe/store/modify/modify.state.ts @@ -0,0 +1,18 @@ +import { RPCClientError } from '@loafer/ng-rpc/protocol'; + +import { Probe } from '../../model'; + +import { createEntityAdapter, EntityAdapter, EntityState } from '@ngrx/entity'; + +export interface State extends EntityState { + error: RPCClientError | null; + isPending: boolean; + probe: Probe | null; +} +export const adapter: EntityAdapter = createEntityAdapter(); +export const initialState: State = adapter.getInitialState({ + error: null, + isPending: false, + probe: null, +}); +