added target store
This commit is contained in:
parent
1fc94ebd7d
commit
766836dcd5
5
src/packages/target/service/index.ts
Normal file
5
src/packages/target/service/index.ts
Normal file
|
@ -0,0 +1,5 @@
|
|||
import { TargetService } from './target.service';
|
||||
|
||||
export const SERVICES = [
|
||||
TargetService,
|
||||
];
|
15
src/packages/target/service/target.service.spec.ts
Normal file
15
src/packages/target/service/target.service.spec.ts
Normal 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();
|
||||
}));
|
||||
});
|
38
src/packages/target/service/target.service.ts
Normal file
38
src/packages/target/service/target.service.ts
Normal 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);
|
||||
// }
|
||||
|
||||
|
||||
}
|
30
src/packages/target/store/index.ts
Normal file
30
src/packages/target/store/index.ts
Normal 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
|
||||
));
|
4
src/packages/target/store/target/index.ts
Normal file
4
src/packages/target/store/target/index.ts
Normal file
|
@ -0,0 +1,4 @@
|
|||
export * from './target.action';
|
||||
export * from './target.effect';
|
||||
export * from './target.reducer';
|
||||
export * from './target.state';
|
36
src/packages/target/store/target/target.action.ts
Normal file
36
src/packages/target/store/target/target.action.ts
Normal 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
|
||||
;
|
15
src/packages/target/store/target/target.effect.spec.ts
Normal file
15
src/packages/target/store/target/target.effect.spec.ts
Normal 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();
|
||||
}));
|
||||
});
|
48
src/packages/target/store/target/target.effect.ts
Normal file
48
src/packages/target/store/target/target.effect.ts
Normal 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)))
|
||||
);
|
||||
|
||||
}
|
45
src/packages/target/store/target/target.reducer.ts
Normal file
45
src/packages/target/store/target/target.reducer.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
15
src/packages/target/store/target/target.state.ts
Normal file
15
src/packages/target/store/target/target.state.ts
Normal 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,
|
||||
};
|
0
src/packages/target/target-store.module.ts
Normal file
0
src/packages/target/target-store.module.ts
Normal file
3
src/packages/target/target.constant.ts
Normal file
3
src/packages/target/target.constant.ts
Normal file
|
@ -0,0 +1,3 @@
|
|||
export const MODULE = {
|
||||
name: 'Target'
|
||||
};
|
Loading…
Reference in New Issue
Block a user