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,9 +1,9 @@
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { OAuthService,UserInfo } from 'angular-oauth2-oidc';
import { Store, Action } from '@ngrx/store';
import { Effect, Actions,ofType } from '@ngrx/effects';
import { Observable , defer , of,from,zip } from 'rxjs';
import { Store } from '@ngrx/store';
import { Actions,ofType,createEffect } from '@ngrx/effects';
import { of,from,zip } from 'rxjs';
import { withLatestFrom,mergeMap,switchMap,map,catchError,first} from 'rxjs/operators';
import * as appCommonActions from '../actions/app-common.actions';
import * as appCommonReducers from '../reducers/app-common.reducer';
@@ -12,43 +12,37 @@ import { FolderService } from '../services/folder.service';
import { UserService } from '../services/user.service';
import { IItemTypes } from '../models/item.types';
import { IListItem } from '../models/list.item';
import { IUser } from '../models/user';
import {IQueryState} from '../models/query.state';
import {StateSerializerService} from '../services/state-serializer.service';
@Injectable()
export class AppCommonEffects {
@Effect({ dispatch: false })
login$: Observable<Action> = this.actions$.pipe(
login$ = createEffect(() => this.actions$.pipe(
ofType(appCommonActions.LOGIN),
withLatestFrom(this.store$.select(appCommonReducers.selectGetInitialized)),
mergeMap(([action, initialized]) => {
var a = (action as appCommonActions.Login);
this.oauthService$.initCodeFlow(a.url,{"prompt":"login"});
return [];
}));
})),{dispatch:false});
@Effect({ dispatch: false })
logout$: Observable<Action> = this.actions$.pipe(
logout$ = createEffect(() => this.actions$.pipe(
ofType(appCommonActions.LOGOUT),
mergeMap((action) => {
this.oauthService$.revokeTokenAndLogout();
return [];
}));
})),{dispatch:false});
@Effect()
loadItemTypes$: Observable<Action> = this.actions$.pipe(
loadItemTypes$ = createEffect(() => this.actions$.pipe(
ofType(appCommonActions.LOADITEMTYPES),
switchMap((action) => {
return this.itemService$.getItemTypes().pipe(
map((itemTypes: IItemTypes) => new appCommonActions.LoadItemTypesSuccess(itemTypes)),
catchError(error => of(new appCommonActions.Fail(error))))
}
));
)));
@Effect()
initUser$: Observable<Action> = this.actions$.pipe(
initUser$ = createEffect(() => this.actions$.pipe(
ofType(appCommonActions.INITUSER),
first(),
switchMap((action) => {
@@ -56,11 +50,10 @@ export class AppCommonEffects {
switchMap(([user,userInfo]) => {return of(new appCommonActions.InitUserSuccess(user,userInfo as UserInfo))} ),
catchError(error => of(new appCommonActions.Fail(error))))
}
));
)));
@Effect()
initUserPackages$:Observable<Action> = this.actions$.pipe(
initUserPackages$ = createEffect(() => this.actions$.pipe(
ofType(appCommonActions.INITUSERPACKAGES),
switchMap(() => {
return this.itemService$.getItemList('vnd.farmmaps.itemtype.package').pipe(
@@ -68,10 +61,9 @@ export class AppCommonEffects {
catchError(error => of(new appCommonActions.Fail(error)))
)
})
);
));
@Effect()
userPackagesChanged$:Observable<Action> = this.actions$.pipe(
userPackagesChanged$ = createEffect(() => this.actions$.pipe(
ofType(appCommonActions.ITEMCHANGEDEVENT),
switchMap((action) => {
let a = action as appCommonActions.ItemChangedEvent;
@@ -80,10 +72,9 @@ export class AppCommonEffects {
else
return [];
})
);
));
@Effect()
initUserSettingsRoot$:Observable<Action> = this.actions$.pipe(
initUserSettingsRoot$ = createEffect(() => this.actions$.pipe(
ofType(appCommonActions.INITUSERSETTINGSROOT),
withLatestFrom(this.store$.select(appCommonReducers.SelectGetUser)),
switchMap(([, user]) => {
@@ -92,10 +83,9 @@ export class AppCommonEffects {
catchError(error => of(new appCommonActions.Fail(error)))
)
})
);
));
@Effect()
initUserSettingsRootChanged$:Observable<Action> = this.actions$.pipe(
initUserSettingsRootChanged$ = createEffect(() => this.actions$.pipe(
ofType(appCommonActions.ITEMCHANGEDEVENT),
switchMap((action) => {
let a = action as appCommonActions.ItemChangedEvent;
@@ -104,38 +94,34 @@ export class AppCommonEffects {
else
return [];
})
);
));
@Effect()
initUserSuccess$: Observable<Action> = this.actions$.pipe(
initUserSuccess$ = createEffect(() => this.actions$.pipe(
ofType(appCommonActions.INITUSERSUCCESS),
switchMap(() => {
return [new appCommonActions.InitRoot(),new appCommonActions.InitUserPackages(),new appCommonActions.InitUserSettingsRoot()];
}
));
)));
@Effect()
initRoot$: Observable<Action> = this.actions$.pipe(
initRoot$ = createEffect(() => this.actions$.pipe(
ofType(appCommonActions.INITROOT),
switchMap(() => {
return this.folderService$.getMyRoots().pipe(
map((folders: IListItem[]) => new appCommonActions.InitRootSuccess(folders)),
catchError(error => of(new appCommonActions.Fail(error))))
}
));
)));
@Effect()
deleteItems$: Observable<Action> = this.actions$.pipe(
deleteItems$ = createEffect(() => this.actions$.pipe(
ofType(appCommonActions.DELETEITEMS),
switchMap((action:appCommonActions.DeleteItems) => {
return this.itemService$.deleteItems(action.itemCodes).pipe(
map((deletedItemCodes: string[]) => new appCommonActions.DeleteItemsSuccess(deletedItemCodes)),
catchError(error => of(new appCommonActions.Fail(error))))
}
));
)));
@Effect()
editItem$: Observable<Action> = this.actions$.pipe(
editItem$ = createEffect(() => this.actions$.pipe(
ofType(appCommonActions.EDITITEM),
withLatestFrom(this.store$.select(appCommonReducers.selectGetItemTypes)),
switchMap(([action, itemtypes]) => {
@@ -148,10 +134,9 @@ export class AppCommonEffects {
this.router$.navigate(['/editor',editor,'item', a.item.code])
return [];
}
));
)));
@Effect()
viewItem$: Observable<Action> = this.actions$.pipe(
viewItem$ = createEffect(() => this.actions$.pipe(
ofType(appCommonActions.VIEWITEM),
withLatestFrom(this.store$.select(appCommonReducers.selectGetItemTypes)),
switchMap(([action, itemtypes]) => {
@@ -181,20 +166,18 @@ export class AppCommonEffects {
}
return [];
}
));
)));
@Effect({ dispatch: false })
fail$: Observable<Action> = this.actions$.pipe(
fail$ = createEffect(() => this.actions$.pipe(
ofType(appCommonActions.FAIL),
map((action) => {
let failAction = action as appCommonActions.Fail;
console.debug(failAction.payload)
return null;
}));
})),{dispatch:false});
@Effect({ dispatch: false })
online$: Observable<Action> = this.actions$.pipe(
online$ = createEffect(() => this.actions$.pipe(
ofType(appCommonActions.ONLINE),
switchMap((action) => {
console.debug("Online: Check token");
@@ -206,7 +189,7 @@ export class AppCommonEffects {
}
}
return of(undefined);
}));
})),{dispatch:false});