Replace @Effect() with createEffect
All checks were successful
FarmMaps.Develop/FarmMapsLib/pipeline/head This commit looks good

This commit is contained in:
Willem Dantuma
2021-07-27 16:26:57 +02:00
parent 76c431b9c2
commit 28e75d5a0d
3 changed files with 111 additions and 124 deletions

View File

@@ -1,11 +1,12 @@
import { Injectable } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Store, Action } from '@ngrx/store';
import { Effect, Actions,ofType } from '@ngrx/effects';
import { Store, Action,createFeatureSelector } from '@ngrx/store';
import { ROUTER_NAVIGATED, RouterReducerState } from '@ngrx/router-store';
import * as fromRouter from '@ngrx/router-store';
import { createEffect, Actions,ofType } from '@ngrx/effects';
import { EMPTY, Observable , of,merge} from 'rxjs';
import { withLatestFrom, switchMap, map, catchError, mergeMap,tap } from 'rxjs/operators';
import { EMPTY, Observable , of} from 'rxjs';
import { withLatestFrom, switchMap, map, catchError, mergeMap } from 'rxjs/operators';
import {GeoJSON,WKT} from 'ol/format';
import {Feature} from 'ol';
@@ -30,11 +31,24 @@ import * as style from 'ol/style';
import { ItemTypeService,IQueryState } from '@farmmaps/common';
import { TemporalItemLayer } from '../models/item.layer'
export const getRouterState = createFeatureSelector<RouterReducerState>('router');
export const {
selectCurrentRoute, // select the current route
selectQueryParams, // select the current route query params
selectQueryParam, // factory function to select a query param
selectRouteParams, // select the current route params
selectRouteParam, // factory function to select a route param
selectRouteData, // select the current route data
selectUrl, // select the current url
} = fromRouter.getSelectors(getRouterState);
@Injectable()
export class MapEffects {
private _geojsonFormat: GeoJSON;
private _wktFormat: WKT;
private overrideSelectedItemLayer
private toPointFeature(updateEvent:commonActions.DeviceUpdateEvent): Feature {
var f = this._wktFormat.readFeature(updateEvent.attributes["geometry"],{
@@ -47,8 +61,7 @@ export class MapEffects {
return f;
}
@Effect()
init$: Observable<Action> = this.actions$.pipe(
init$ = createEffect(() => this.actions$.pipe(
ofType(mapActions.INIT),
withLatestFrom(this.store$.select(commonReducers.selectGetRootItems)),
switchMap(([action, rootItems]) => {
@@ -88,25 +101,23 @@ export class MapEffects {
return actions;
}
));
)));
@Effect()
initBaseLayers$: Observable<Action> = this.actions$.pipe(
initBaseLayers$ = createEffect(() => this.actions$.pipe(
ofType(mapActions.INIT),
withLatestFrom(this.store$.select(mapReducers.selectGetProjection)),
map(([action, projection]) => new mapActions.LoadBaseLayers(projection)));
map(([action, projection]) => new mapActions.LoadBaseLayers(projection)))
);
@Effect()
loadBaseLayers$: Observable<Action> = this.actions$.pipe(
loadBaseLayers$ = createEffect(() => this.actions$.pipe(
ofType(mapActions.LOADBASELAYERS),
switchMap((action: mapActions.LoadBaseLayers) => {
return this.itemService$.getItemList("vnd.farmmaps.itemtype.layer", { "isBaseLayer": true }).pipe(
map((items: IItem[]) => new mapActions.LoadBaseLayersSuccess(items)),
catchError(error => of(new commonActions.Fail(error))));
}));
})));
@Effect()
startSearch$: Observable<Action> = this.actions$.pipe(
startSearch$ = createEffect(() => this.actions$.pipe(
ofType(mapActions.STARTSEARCH),
switchMap((action) => {
let a = action as mapActions.StartSearch;
@@ -129,11 +140,10 @@ export class MapEffects {
return [];
}
return newAction;
}));
})));
@Effect()
zoomToExtent$: Observable<Action> = this.actions$.pipe(
zoomToExtent$ = createEffect(() => this.actions$.pipe(
ofType(mapActions.STARTSEARCHSUCCESS),
mergeMap((action: mapActions.StartSearchSuccess) => {
let actions =[];
@@ -150,10 +160,9 @@ export class MapEffects {
}
}
return actions;
}));
})));
@Effect()
zoomToExtent2$: Observable<Action> = this.actions$.pipe(
zoomToExtent2$ = createEffect(() => this.actions$.pipe(
ofType(mapActions.SETFEATURES),
switchMap((action: mapActions.SetFeatures) => {
let extent = createEmpty();
@@ -164,17 +173,15 @@ export class MapEffects {
if(action.features.length>0) return of(new mapActions.SetExtent(extent));
}
return EMPTY;
}));
})));
@Effect()
hideMenu$: Observable<Action> = this.actions$.pipe(
hideMenu$ = createEffect(() => this.actions$.pipe(
ofType(mapActions.STARTSEARCHSUCCESS),
mergeMap((action: mapActions.StartSearchSuccess) => {
return of(new commonActions.SetMenuVisible(false));
}));
})));
@Effect()
selectItem$: Observable<Action> = this.actions$.pipe(
selectItem$ = createEffect(() => this.actions$.pipe(
ofType(mapActions.SELECTITEM),
withLatestFrom(this.store$.select(mapReducers.selectGetSelectedItem)),
switchMap(([action, selectedItem]) => {
@@ -195,26 +202,19 @@ export class MapEffects {
return [];
}
}
));
)));
@Effect()
selectItemSuccessSetLayer$: Observable<Action> = this.actions$.pipe(
selectItemSuccessSetLayer$ = createEffect(() => this.actions$.pipe(
ofType(mapActions.SELECTITEMSUCCESS),
map((action:mapActions.SelectItemSuccess) =>
new mapActions.SetSelectedItemLayer(action.item)
)
);
));
@Effect()
selectItemSuccess$: Observable<Action> = this.actions$.pipe(
selectItemSuccess$ = createEffect(() => this.actions$.pipe(
ofType(mapActions.SELECTITEMSUCCESS),
switchMap((action:mapActions.SelectItemSuccess) => {
let overrideSelectedItemLayer = false;
if(this.route$ && this.route$.snapshot && this.route$.snapshot.data && this.route$.snapshot.data["fm-map-map"]) {
let params = this.route$.snapshot.data["fm-map-map"];
overrideSelectedItemLayer = params["overrideSelectedItemlayer"] ? params["overrideSelectedItemlayer"] : false;
}
if(!overrideSelectedItemLayer) {
switchMap((action:mapActions.SelectItemSuccess) => {
if(!this.overrideSelectedItemLayer) {
return this.itemService$.getFeature(action.item.code, "EPSG:3857").pipe(
map((feature: any) => {
let f = this._geojsonFormat.readFeature(feature);
@@ -226,10 +226,9 @@ export class MapEffects {
return EMPTY;
}
}
));
)));
@Effect()
selectItemSuccessTemporal$: Observable<Action> = this.actions$.pipe(
selectItemSuccessTemporal$ = createEffect(() => this.actions$.pipe(
ofType(mapActions.SELECTITEMSUCCESS),
switchMap((action:mapActions.SelectItemSuccess) => {
if(action.item.itemType == "vnd.farmmaps.itemtype.temporal") {
@@ -244,16 +243,14 @@ export class MapEffects {
return [];
}
}
));
)));
@Effect()
uploadedItemClick$: Observable<Action> = this.actions$.pipe(
uploadedItemClick$ = createEffect(() => this.actions$.pipe(
ofType(commonActions.UPLOADEDFILECLICK),
switchMap((action: commonActions.UploadedFileClick) => of(new mapActions.DoQuery(tassign(mapReducers.initialState.query, {itemCode:action.itemCode})))
));
)));
@Effect()
featureUpdate$: Observable<Action> = this.actions$.pipe(
featureUpdate$ = createEffect(() => this.actions$.pipe(
ofType(commonActions.DEVICEUPDATEEVENT),
withLatestFrom(this.store$.select(mapReducers.selectGetFeatures)),
mergeMap(([action, features]) => {
@@ -270,10 +267,9 @@ export class MapEffects {
} else {
return [];
}
}));
})));
@Effect()
itemUpdate$: Observable<Action> = this.actions$.pipe(
itemUpdate$ = createEffect(() => this.actions$.pipe(
ofType(commonActions.ITEMCHANGEDEVENT),
withLatestFrom(this.store$.select(mapReducers.selectGetSelectedItem)),
mergeMap(([action, selectedItem]) => {
@@ -292,7 +288,7 @@ export class MapEffects {
} else {
return [];
}
}));
})));
getActionFromQueryState(queryState:IQueryState, inSearch:boolean):Observable<Action>|[] {
if(!inSearch && (queryState.itemType || queryState.parentCode || queryState.itemCode || queryState.query || queryState.tags)) {
@@ -309,8 +305,7 @@ export class MapEffects {
return of(newAction);
}
@Effect()
getLayerValue$: Observable<Action> = this.actions$.pipe(
getLayerValue$ = createEffect(() => this.actions$.pipe(
ofType(mapActions.GETLAYERVALUE),
mergeMap((action:mapActions.GetLayerValue) => {
var l = action.itemLayer.item.data["layers"][action.itemLayer.layerIndex];
@@ -334,19 +329,17 @@ export class MapEffects {
return a;
}))
}
));
)));
@Effect()
updateLayerValuesOnLayerAddedOrRemoved$: Observable<Action> = this.actions$.pipe(
updateLayerValuesOnLayerAddedOrRemoved$ = createEffect(() => this.actions$.pipe(
ofType(mapActions.ADDLAYER,mapActions.REMOVELAYER,mapActions.SELECTITEM,mapActions.NEXTTEMPORAL,mapActions.PREVIOUSTEMPORAL,mapActions.TOGGLELAYERVALUESENABLED),
withLatestFrom(this.store$.select(mapReducers.selectGetLayerValuesX)),
withLatestFrom(this.store$.select(mapReducers.selectGetLayerValuesY)),
map(([[action,x],y]) => new mapActions.SetLayerValuesLocation(x,y))
);
));
@Effect()
getLayerValues$: Observable<Action> = this.actions$.pipe(
getLayerValues$ = createEffect(() => this.actions$.pipe(
ofType(mapActions.SETLAYERVALUESLOCATION),
withLatestFrom(this.store$.select(mapReducers.selectGetSelectedItemLayer)),
withLatestFrom(this.store$.select(mapReducers.selectGetLayerValuesEnabled)),
@@ -372,20 +365,18 @@ export class MapEffects {
});
}
return actions;
}));
})));
@Effect()
setState$: Observable<Action> = this.actions$.pipe(
setState$ = createEffect(() => this.actions$.pipe(
ofType(mapActions.SETSTATE),
withLatestFrom(this.store$.select(mapReducers.selectGetInSearch)),
switchMap(([action,inSearch]) => {
let a = action as mapActions.SetState;
return this.getActionFromQueryState(a.queryState,inSearch);
}));
})));
@Effect()
escape$:Observable<Action> = this.actions$.pipe(
escape$ = createEffect(() => this.actions$.pipe(
ofType(commonActions.ESCAPE),
switchMap((action) => {
let a = action as commonActions.Escape;
@@ -394,9 +385,21 @@ export class MapEffects {
} else {
return EMPTY;
}
}));
})));
constructor(private actions$: Actions, private store$: Store<mapReducers.State>, private folderService$: FolderService, private itemService$: ItemService,private featureIconService$:FeatureIconService,private itemTypeService$:ItemTypeService, private route$: ActivatedRoute) {
setOverride$ = createEffect(() => this.actions$.pipe(
ofType(ROUTER_NAVIGATED),
switchMap(() => this.store$.select(selectRouteData as any)),
switchMap((data: any) => {
if(data && data["fm-map-map"]) {
let params = data["fm-map-map"];
this.overrideSelectedItemLayer = params["overrideSelectedItemlayer"] ? params["overrideSelectedItemlayer"] : false;
}
return [];
})
));
constructor(private actions$: Actions, private store$: Store<mapReducers.State>, private folderService$: FolderService, private itemService$: ItemService,private featureIconService$:FeatureIconService,private itemTypeService$:ItemTypeService) {
this._geojsonFormat = new GeoJSON();
this._wktFormat = new WKT();
}