member_webapp/@overflow/sensor/store/detail/detail.effect.ts
crusader d59d9379f9 ing
2018-05-24 15:44:13 +09:00

50 lines
1.2 KiB
TypeScript

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 { Sensor } from '@overflow/commons-typescript/model/sensor';
import { SensorService } from '../../service/sensor.service';
import {
Read,
ReadFailure,
ReadSuccess,
ActionType
} from './detail.action';
@Injectable()
export class Effects {
constructor(
private actions$: Actions,
private sensorService: SensorService,
private router: Router
) { }
@Effect()
read$: Observable<Action> = this.actions$
.ofType(ActionType.Read)
.map((action: Read) => action.payload)
.switchMap(payload => this.sensorService.read(payload.id))
.map(sensor => {
return new ReadSuccess(sensor);
})
.catch((error: RPCClientError) => {
return of(new ReadFailure(error));
});
}