Created
April 15, 2020 07:18
-
-
Save kaplan81/71e52e5971bde668815820154d061ed3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NavRoute } from '@akelius-bsc/ng-kit'; | |
import { Location } from '@angular/common'; | |
import { Injectable } from '@angular/core'; | |
import { NavigationExtras, Router } from '@angular/router'; | |
import * as fromRootModels from '@asset-app/models'; | |
import * as routerActions from '@asset-app/store/actions/router.action'; | |
import { Actions, Effect, ofType } from '@ngrx/effects'; | |
import { map, tap } from 'rxjs/operators'; | |
@Injectable() | |
export class RouterEffects { | |
@Effect({ dispatch: false }) | |
navigate$ = this.actions$.pipe( | |
ofType(routerActions.RouterActionTypes.Go), | |
map((action: routerActions.Go) => action.payload), | |
tap((navRoute: NavRoute<fromRootModels.NavPath>) => { | |
this.navigate(navRoute); | |
}) | |
); | |
@Effect({ dispatch: false }) | |
navigateBack$ = this.actions$.pipe( | |
ofType(routerActions.RouterActionTypes.Back), | |
tap(() => this.location.back()) | |
); | |
@Effect({ dispatch: false }) | |
navigateForward$ = this.actions$.pipe( | |
ofType(routerActions.RouterActionTypes.Forward), | |
tap(() => this.location.forward()) | |
); | |
constructor(private actions$: Actions, private router: Router, private location: Location) {} | |
private navigate(navRoute: NavRoute<fromRootModels.NavPath>): void { | |
const navigationExtras: NavigationExtras = this.setNavigationExtras(navRoute); | |
if (navRoute.callback !== undefined) { | |
this.router.navigate(navRoute.path, navigationExtras).then(navRoute.callback); | |
} else { | |
this.router.navigate(navRoute.path, navigationExtras); | |
} | |
} | |
private setNavigationExtras(navRoute: NavRoute<fromRootModels.NavPath>): NavigationExtras { | |
return { | |
...(navRoute.query && { queryParams: navRoute.query }), | |
...(navRoute.extras && { ...navRoute.extras }) | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment