diff --git a/src/packages/probe/service/probe.service.ts b/src/packages/probe/service/probe.service.ts index 685925e..6e79e6a 100644 --- a/src/packages/probe/service/probe.service.ts +++ b/src/packages/probe/service/probe.service.ts @@ -22,4 +22,11 @@ export class ProbeService { public readAllByDomain(domain: Domain): Observable { return this.rpcService.call('ProbeService.readAllByDomain', domain); } + + public read(id: string): Observable { + const param = { + id: id, + }; + return this.rpcService.call('ProbeService.read', param); + } } diff --git a/src/packages/probe/store/detail/detail.action.ts b/src/packages/probe/store/detail/detail.action.ts new file mode 100644 index 0000000..22911a1 --- /dev/null +++ b/src/packages/probe/store/detail/detail.action.ts @@ -0,0 +1,37 @@ +import { Action } from '@ngrx/store'; + +import { ErrorResponse } from 'packages/commons/service/error-response'; + +import { Probe } from '../../model'; + + +export enum ActionType { + Read = '[probe.detail] Read', + ReadSuccess = '[probe.detail] ReadSuccess', + ReadFailure = '[probe.detail] ReadFailure', +} + +export class Read implements Action { + readonly type = ActionType.Read; + + constructor(public payload: {id: string}) {} +} + +export class ReadSuccess implements Action { + readonly type = ActionType.ReadSuccess; + + constructor(public payload: Probe) {} +} + +export class ReadFailure implements Action { + readonly type = ActionType.ReadFailure; + + constructor(public payload: ErrorResponse) {} +} + + +export type Actions = + | Read + | ReadSuccess + | ReadFailure +; diff --git a/src/packages/probe/store/list/probe.effect.spec.ts b/src/packages/probe/store/detail/detail.effect.spec.ts similarity index 76% rename from src/packages/probe/store/list/probe.effect.spec.ts rename to src/packages/probe/store/detail/detail.effect.spec.ts index d405009..b1c2217 100644 --- a/src/packages/probe/store/list/probe.effect.spec.ts +++ b/src/packages/probe/store/detail/detail.effect.spec.ts @@ -1,8 +1,8 @@ import { TestBed, inject } from '@angular/core/testing'; -import { Effects } from './probe.effect'; +import { Effects } from './detail.effect'; -describe('NoAuth-Probe.Effects', () => { +describe('ProbeDetail.Effects', () => { beforeEach(() => { TestBed.configureTestingModule({ providers: [Effects] diff --git a/src/packages/probe/store/detail/detail.effect.ts b/src/packages/probe/store/detail/detail.effect.ts new file mode 100644 index 0000000..6e75465 --- /dev/null +++ b/src/packages/probe/store/detail/detail.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 { ErrorResponse } from 'packages/commons/service/error-response'; + +import { Probe } from '../../model'; +import { ProbeService } from '../../service/probe.service'; + +import { + Read, + ReadFailure, + ReadSuccess, + ActionType +} from './detail.action'; + +@Injectable() +export class Effects { + + constructor( + private actions$: Actions, + private probeService: ProbeService, + private router: Router + ) { } + + @Effect() + read$: Observable = this.actions$ + .ofType(ActionType.Read) + .map((action: Read) => action.payload) + .switchMap(payload => this.probeService.read(payload.id)) + .map(probe => { + return new ReadSuccess(probe); + }) + .catch((error: ErrorResponse) => { + return of(new ReadFailure(error)); + }); +} diff --git a/src/packages/probe/store/detail/detail.reducer.ts b/src/packages/probe/store/detail/detail.reducer.ts new file mode 100644 index 0000000..2f211b1 --- /dev/null +++ b/src/packages/probe/store/detail/detail.reducer.ts @@ -0,0 +1,50 @@ +import { ErrorResponse } from 'packages/commons/service/error-response'; + +import { + Read, + ReadFailure, + ReadSuccess, + ActionType, + Actions, +} from './detail.action'; + +import { + State, + initialState, +} from './detail.state'; + +import { Probe } from '../../model'; + +export function reducer(state = initialState, action: Actions): State { + switch (action.type) { + case ActionType.Read: { + return { + ...state, + error: null, + isPending: true, + }; + } + + case ActionType.ReadSuccess: { + return { + ...state, + error: null, + isPending: false, + probe: action.payload, + }; + } + + case ActionType.ReadFailure: { + return { + ...state, + error: action.payload, + isPending: false, + probe: null, + }; + } + + default: { + return state; + } + } +} diff --git a/src/packages/probe/store/detail/detail.state.ts b/src/packages/probe/store/detail/detail.state.ts new file mode 100644 index 0000000..5ea411f --- /dev/null +++ b/src/packages/probe/store/detail/detail.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; + probe: Probe | null; +} + +export const initialState: State = { + error: null, + isPending: false, + probe: null, +}; + +export const getProbe = (state: State) => state.probe; +export const getError = (state: State) => state.error; +export const isPending = (state: State) => state.isPending; diff --git a/src/packages/probe/store/detail/index.ts b/src/packages/probe/store/detail/index.ts new file mode 100644 index 0000000..23ae8ab --- /dev/null +++ b/src/packages/probe/store/detail/index.ts @@ -0,0 +1,4 @@ +export * from './detail.action'; +export * from './detail.effect'; +export * from './detail.reducer'; +export * from './detail.state'; diff --git a/src/packages/probe/store/index.ts b/src/packages/probe/store/index.ts index 2f1ff23..9906c04 100644 --- a/src/packages/probe/store/index.ts +++ b/src/packages/probe/store/index.ts @@ -1,13 +1,17 @@ -import * as ProbeStore from './list'; +import * as ProbeListStore from './list'; +import * as ProbeDetailStore from './detail'; export interface State { - probe: ProbeStore.State; + list: ProbeListStore.State; + detail: ProbeDetailStore.State; } export const REDUCERS = { - probe: ProbeStore.reducer, + list: ProbeListStore.reducer, + detail: ProbeDetailStore.reducer, }; export const EFFECTS = [ - ProbeStore.Effects, + ProbeListStore.Effects, + ProbeDetailStore.Effects, ]; diff --git a/src/packages/probe/store/list/index.ts b/src/packages/probe/store/list/index.ts index d8f12e5..7fd86e0 100644 --- a/src/packages/probe/store/list/index.ts +++ b/src/packages/probe/store/list/index.ts @@ -1,4 +1,4 @@ -export * from './probe.action'; -export * from './probe.effect'; -export * from './probe.reducer'; -export * from './probe.state'; +export * from './list.action'; +export * from './list.effect'; +export * from './list.reducer'; +export * from './list.state'; diff --git a/src/packages/probe/store/list/probe.action.ts b/src/packages/probe/store/list/list.action.ts similarity index 100% rename from src/packages/probe/store/list/probe.action.ts rename to src/packages/probe/store/list/list.action.ts diff --git a/src/packages/probe/store/list/list.effect.spec.ts b/src/packages/probe/store/list/list.effect.spec.ts new file mode 100644 index 0000000..fbff64a --- /dev/null +++ b/src/packages/probe/store/list/list.effect.spec.ts @@ -0,0 +1,15 @@ +import { TestBed, inject } from '@angular/core/testing'; + +import { Effects } from './list.effect'; + +describe('ProbeList.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/list.effect.ts similarity index 98% rename from src/packages/probe/store/list/probe.effect.ts rename to src/packages/probe/store/list/list.effect.ts index d886e44..adf3e49 100644 --- a/src/packages/probe/store/list/probe.effect.ts +++ b/src/packages/probe/store/list/list.effect.ts @@ -26,7 +26,7 @@ import { ReadAllByDomainFailure, ReadAllByDomainSuccess, ActionType -} from './probe.action'; +} from './list.action'; @Injectable() export class Effects { diff --git a/src/packages/probe/store/list/probe.reducer.ts b/src/packages/probe/store/list/list.reducer.ts similarity index 94% rename from src/packages/probe/store/list/probe.reducer.ts rename to src/packages/probe/store/list/list.reducer.ts index 02e1fa9..fd925b9 100644 --- a/src/packages/probe/store/list/probe.reducer.ts +++ b/src/packages/probe/store/list/list.reducer.ts @@ -6,12 +6,12 @@ import { ReadAllByDomainSuccess, ActionType, Actions, -} from './probe.action'; +} from './list.action'; import { State, initialState, -} from './probe.state'; +} from './list.state'; import { Probe } from '../../model'; diff --git a/src/packages/probe/store/list/probe.state.ts b/src/packages/probe/store/list/list.state.ts similarity index 100% rename from src/packages/probe/store/list/probe.state.ts rename to src/packages/probe/store/list/list.state.ts