ing
This commit is contained in:
4
@overflow/probe/store/modify/index.ts
Normal file
4
@overflow/probe/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';
|
||||
45
@overflow/probe/store/modify/modify.action.ts
Normal file
45
@overflow/probe/store/modify/modify.action.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { Action } from '@ngrx/store';
|
||||
|
||||
import { RPCClientError } from '@loafer/ng-rpc';
|
||||
|
||||
import { Probe } from '@overflow/commons-typescript/model/probe';
|
||||
|
||||
|
||||
export enum ActionType {
|
||||
Modify = '[probe.modify] Modify',
|
||||
ModifyDisplayName = '[probe.modify] ModifyDisplayName',
|
||||
ModifySuccess = '[probe.modify] ModifySuccess',
|
||||
ModifyFailure = '[probe.modify] 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 class ModifyDisplayName implements Action {
|
||||
readonly type = ActionType.ModifyDisplayName;
|
||||
|
||||
constructor(public payload: {id: string, displayName: string}) {}
|
||||
}
|
||||
|
||||
|
||||
export type Actions =
|
||||
| Modify
|
||||
| ModifySuccess
|
||||
| ModifyFailure
|
||||
| ModifyDisplayName
|
||||
;
|
||||
15
@overflow/probe/store/modify/modify.effect.spec.ts
Normal file
15
@overflow/probe/store/modify/modify.effect.spec.ts
Normal file
@@ -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();
|
||||
}));
|
||||
});
|
||||
62
@overflow/probe/store/modify/modify.effect.ts
Normal file
62
@overflow/probe/store/modify/modify.effect.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
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';
|
||||
|
||||
import { Probe } from '@overflow/commons-typescript/model/probe';
|
||||
import { ProbeService } from '../../service/probe.service';
|
||||
|
||||
import {
|
||||
Modify,
|
||||
ModifyDisplayName,
|
||||
ModifySuccess,
|
||||
ModifyFailure,
|
||||
ActionType
|
||||
} from './modify.action';
|
||||
|
||||
@Injectable()
|
||||
export class Effects {
|
||||
|
||||
constructor(
|
||||
private actions$: Actions,
|
||||
private probeService: ProbeService,
|
||||
private router: Router
|
||||
) { }
|
||||
|
||||
@Effect()
|
||||
modify$: Observable<Action> = 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));
|
||||
});
|
||||
|
||||
@Effect()
|
||||
modifyDisplayName$: Observable<Action> = this.actions$
|
||||
.ofType(ActionType.ModifyDisplayName)
|
||||
.map((action: ModifyDisplayName) => action.payload)
|
||||
.switchMap(payload => this.probeService.modifyDisplayName(payload.id, payload.displayName))
|
||||
.map(probe => {
|
||||
return new ModifySuccess(probe);
|
||||
})
|
||||
.catch((error: RPCClientError) => {
|
||||
return of(new ModifyFailure(error));
|
||||
});
|
||||
}
|
||||
54
@overflow/probe/store/modify/modify.reducer.ts
Normal file
54
@overflow/probe/store/modify/modify.reducer.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import {
|
||||
ActionType,
|
||||
Actions,
|
||||
} from './modify.action';
|
||||
|
||||
import {
|
||||
State,
|
||||
initialState,
|
||||
} from './modify.state';
|
||||
|
||||
import { Probe } from '@overflow/commons-typescript/model/probe';
|
||||
|
||||
|
||||
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,
|
||||
modifiedProbe: action.payload,
|
||||
};
|
||||
}
|
||||
|
||||
case ActionType.ModifyFailure: {
|
||||
return {
|
||||
...state,
|
||||
error: action.payload,
|
||||
isPending: false,
|
||||
modifiedProbe: null,
|
||||
};
|
||||
}
|
||||
|
||||
case ActionType.ModifyDisplayName: {
|
||||
return {
|
||||
...state,
|
||||
error: null,
|
||||
isPending: true,
|
||||
};
|
||||
}
|
||||
|
||||
default: {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
}
|
||||
18
@overflow/probe/store/modify/modify.state.ts
Normal file
18
@overflow/probe/store/modify/modify.state.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { RPCClientError } from '@loafer/ng-rpc';
|
||||
|
||||
import { Probe } from '@overflow/commons-typescript/model/probe';
|
||||
|
||||
import { createEntityAdapter, EntityAdapter, EntityState } from '@ngrx/entity';
|
||||
|
||||
export interface State extends EntityState<Probe> {
|
||||
error: RPCClientError | null;
|
||||
isPending: boolean;
|
||||
modifiedProbe: Probe | null;
|
||||
}
|
||||
export const adapter: EntityAdapter<Probe> = createEntityAdapter<Probe>();
|
||||
export const initialState: State = adapter.getInitialState({
|
||||
error: null,
|
||||
isPending: false,
|
||||
modifiedProbe: null,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user