added target store

This commit is contained in:
snoop 2018-03-14 16:53:32 +09:00
parent 1fc94ebd7d
commit 766836dcd5
12 changed files with 254 additions and 0 deletions

View File

@ -0,0 +1,5 @@
import { TargetService } from './target.service';
export const SERVICES = [
TargetService,
];

View File

@ -0,0 +1,15 @@
import { TestBed, inject } from '@angular/core/testing';
import { TargetService } from './target.service';
describe('TargetService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [TargetService]
});
});
it('should be created', inject([TargetService], (service: TargetService) => {
expect(service).toBeTruthy();
}));
});

View File

@ -0,0 +1,38 @@
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import { RPCClient } from 'packages/core/rpc/client/RPCClient';
import { Target } from '../model';
import { Domain } from '../../domain/model';
@Injectable()
export class TargetService {
public constructor(
private rpcClient: RPCClient,
) {
}
public readAllByDomain(domain: Domain): Observable<Target[]> {
const body = {
domain: domain,
};
return this.rpcClient.call('TargetService.readAllByDomain', domain);
}
// public readAllByProbe(domain: Domain): Observable<Target[]> {
// const body = {
// domain: domain,
// };
// return this.rpcClient.call('TargetService.readAllByDomain', domain);
// }
}

View File

@ -0,0 +1,30 @@
import {
createSelector,
createFeatureSelector,
ActionReducerMap,
} from '@ngrx/store';
import { StateSelector } from 'packages/commons/util/ngrx/store';
import { MODULE } from '../target.constant';
import * as TargetStore from './target';
export interface State {
readallbydomain: TargetStore.State;
}
export const REDUCERS = {
readallbydomain: TargetStore.reducer,
};
export const EFFECTS = [
TargetStore.Effects,
];
export const selectTargetState = createFeatureSelector<State>(MODULE.name);
export const ReadAllByDomainSelector = new StateSelector<TargetStore.State>(createSelector(
selectTargetState,
(state: State) => state.readallbydomain
));

View File

@ -0,0 +1,4 @@
export * from './target.action';
export * from './target.effect';
export * from './target.reducer';
export * from './target.state';

View File

@ -0,0 +1,36 @@
import { Action } from '@ngrx/store';
import { RPCError } from 'packages/core/rpc/error';
import { Target } from '../../model';
import { Domain } from '../../../domain/model';
export enum ActionType {
ReadAllByDomain = '[Target.ReadAllByDomain] ReadAllByDomain',
ReadAllByDomainSuccess = '[Target.ReadAllByDomainSuccess] ReadAllByDomainSuccess',
ReadAllByDomainFailure = '[Target.ReadAllByDomainFailure] 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: Target[]) {}
}
export class ReadAllByDomainFailure implements Action {
readonly type = ActionType.ReadAllByDomainFailure;
constructor(public payload: RPCError) {}
}
export type Actions =
| ReadAllByDomain
| ReadAllByDomainSuccess
| ReadAllByDomainFailure
;

View File

@ -0,0 +1,15 @@
import { TestBed, inject } from '@angular/core/testing';
import { Effects } from './target.effect';
describe('Target.Effects', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [Effects]
});
});
it('should be created', inject([Effects], (effects: Effects) => {
expect(effects).toBeTruthy();
}));
});

View File

@ -0,0 +1,48 @@
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/map';
import 'rxjs/add/operator/take';
import { RPCError } from 'packages/core/rpc/error';
import { Target } from '../../model';
import { TargetService } from '../../service/target.service';
import {
ReadAllByDomain,
ReadAllByDomainSuccess,
ReadAllByDomainFailure,
ActionType,
} from './target.action';
@Injectable()
export class Effects {
constructor(
private actions$: Actions,
private targetService: TargetService,
private router: Router
) { }
@Effect()
readAllByMember$: Observable<Action> = this.actions$
.ofType(ActionType.ReadAllByDomain)
.map((action: ReadAllByDomain) => action.payload)
.exhaustMap(domain =>
this.targetService
.readAllByDomain(domain)
.map(targets => new ReadAllByDomainSuccess(targets))
.catch(error => of(new ReadAllByDomainFailure(error)))
);
}

View File

@ -0,0 +1,45 @@
import {
Actions,
ActionType,
} from './target.action';
import {
State,
initialState,
} from './target.state';
import { Target } from '../../model';
export function reducer(state = initialState, action: Actions): State {
switch (action.type) {
case ActionType.ReadAllByDomain: {
return {
...state,
error: null,
pending: true,
};
}
case ActionType.ReadAllByDomainSuccess: {
return {
...state,
error: null,
pending: false,
targets: action.payload
};
}
case ActionType.ReadAllByDomainFailure: {
return {
...state,
error: action.payload,
pending: false,
targets: null,
};
}
default: {
return state;
}
}
}

View File

@ -0,0 +1,15 @@
import { RPCError } from 'packages/core/rpc/error';
import { Target } from '../../model';
export interface State {
error: RPCError | null;
pending: boolean;
targets: Target[] | null;
}
export const initialState: State = {
error: null,
pending: false,
targets: null,
};

View File

@ -0,0 +1,3 @@
export const MODULE = {
name: 'Target'
};