ing
This commit is contained in:
4
@overflow/target/store/modify/index.ts
Normal file
4
@overflow/target/store/modify/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export * from './modify.action';
|
||||
export * from './modify.effect';
|
||||
export * from './modify.reducer';
|
||||
export * from './modify.state';
|
||||
34
@overflow/target/store/modify/modify.action.ts
Normal file
34
@overflow/target/store/modify/modify.action.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { Action } from '@ngrx/store';
|
||||
|
||||
import { RPCClientError } from '@loafer/ng-rpc';
|
||||
import { Target } from '@overflow/commons-typescript/model/target';
|
||||
|
||||
export enum ActionType {
|
||||
Modify = '[Target.modify] Modify',
|
||||
ModifySuccess = '[Target.modify] ModifySuccess',
|
||||
ModifyFailure = '[Target.modify] ModifyFailure',
|
||||
}
|
||||
|
||||
export class Modify implements Action {
|
||||
readonly type = ActionType.Modify;
|
||||
|
||||
constructor(public payload: Target) {}
|
||||
}
|
||||
|
||||
export class ModifySuccess implements Action {
|
||||
readonly type = ActionType.ModifySuccess;
|
||||
|
||||
constructor(public payload: Target) {}
|
||||
}
|
||||
|
||||
export class ModifyFailure implements Action {
|
||||
readonly type = ActionType.ModifyFailure;
|
||||
|
||||
constructor(public payload: RPCClientError) {}
|
||||
}
|
||||
|
||||
export type Actions =
|
||||
| Modify
|
||||
| ModifySuccess
|
||||
| ModifyFailure
|
||||
;
|
||||
15
@overflow/target/store/modify/modify.effect.spec.ts
Normal file
15
@overflow/target/store/modify/modify.effect.spec.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { TestBed, inject } from '@angular/core/testing';
|
||||
|
||||
import { Effects } from './modify.effect';
|
||||
|
||||
describe('Target.Effects', () => {
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [Effects]
|
||||
});
|
||||
});
|
||||
|
||||
it('should be created', inject([Effects], (effects: Effects) => {
|
||||
expect(effects).toBeTruthy();
|
||||
}));
|
||||
});
|
||||
44
@overflow/target/store/modify/modify.effect.ts
Normal file
44
@overflow/target/store/modify/modify.effect.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
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 { TargetService } from '../../service/target.service';
|
||||
|
||||
import {
|
||||
Modify,
|
||||
ModifySuccess,
|
||||
ModifyFailure,
|
||||
ActionType,
|
||||
} from './modify.action';
|
||||
|
||||
@Injectable()
|
||||
export class Effects {
|
||||
|
||||
constructor(
|
||||
private actions$: Actions,
|
||||
private targetService: TargetService,
|
||||
private router: Router
|
||||
) { }
|
||||
|
||||
@Effect()
|
||||
modify$: Observable<Action> = this.actions$
|
||||
.ofType(ActionType.Modify)
|
||||
.map((action: Modify) => action.payload)
|
||||
.exhaustMap(target =>
|
||||
this.targetService.modify(target)
|
||||
.map(targets => new ModifySuccess(targets))
|
||||
.catch(error => of(new ModifyFailure(error)))
|
||||
);
|
||||
|
||||
}
|
||||
43
@overflow/target/store/modify/modify.reducer.ts
Normal file
43
@overflow/target/store/modify/modify.reducer.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import {
|
||||
Actions,
|
||||
ActionType,
|
||||
} from './modify.action';
|
||||
|
||||
import {
|
||||
State,
|
||||
initialState,
|
||||
} from './modify.state';
|
||||
|
||||
export function reducer(state = initialState, action: Actions): State {
|
||||
switch (action.type) {
|
||||
case ActionType.Modify: {
|
||||
return {
|
||||
...state,
|
||||
error: null,
|
||||
pending: true,
|
||||
};
|
||||
}
|
||||
|
||||
case ActionType.ModifySuccess: {
|
||||
return {
|
||||
...state,
|
||||
error: null,
|
||||
pending: false,
|
||||
target: action.payload
|
||||
};
|
||||
}
|
||||
|
||||
case ActionType.ModifyFailure: {
|
||||
return {
|
||||
...state,
|
||||
error: action.payload,
|
||||
pending: false,
|
||||
target: null,
|
||||
};
|
||||
}
|
||||
|
||||
default: {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
}
|
||||
14
@overflow/target/store/modify/modify.state.ts
Normal file
14
@overflow/target/store/modify/modify.state.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { RPCClientError } from '@loafer/ng-rpc';
|
||||
import { Target } from '@overflow/commons-typescript/model/target';
|
||||
|
||||
export interface State {
|
||||
error: RPCClientError | null;
|
||||
pending: boolean;
|
||||
target: Target | null;
|
||||
}
|
||||
|
||||
export const initialState: State = {
|
||||
error: null,
|
||||
pending: false,
|
||||
target: null,
|
||||
};
|
||||
Reference in New Issue
Block a user