UNPKG

@redux-multipurpose/angular-router

Version:

Redux Multipurpose angular-router is available as a package on NPM:

72 lines (63 loc) 1.94 kB
import { createSlice } from '@reduxjs/toolkit'; import { __decorate } from 'tslib'; import { Injectable } from '@angular/core'; import { dispatch, store, select } from '@redux-multipurpose/core'; import { NavigationEnd } from '@angular/router'; import { filter } from 'rxjs/operators'; const INITIAL_STATE_ROUTER = null; const routerSlice = createSlice({ name: 'router', initialState: INITIAL_STATE_ROUTER, reducers: { updateUrl(state, action) { return action.payload; }, goToUrl(state, action) { return action.payload; } } }); const { actions, reducer } = routerSlice; const routerReducer = reducer; const { goToUrl, updateUrl } = actions; const router = state => state.router; class RouterActions { constructor() { this.goToUrl = (url) => { return goToUrl(url); }; } } RouterActions.decorators = [ { type: Injectable } ]; __decorate([ dispatch() ], RouterActions.prototype, "goToUrl", void 0); class RouterService { constructor(router) { this.router = router; } init() { this.router.events.pipe(filter((event) => event instanceof NavigationEnd)).subscribe((navigationEndEvent) => { store.dispatch(updateUrl(navigationEndEvent.url)); }); this.router$.pipe(filter(url => url != null && url != undefined && this.router.url !== url)).subscribe(url => { this.router.navigateByUrl(url); }); } } __decorate([ select(router) ], RouterService.prototype, "router$", void 0); const configureRouterReducer = (key, router) => { return { key, reducer: routerReducer, service: new RouterService(router) }; }; /* * Public API Surface of angular-router */ /** * Generated bundle index. Do not edit. */ export { RouterActions, configureRouterReducer, goToUrl, router, updateUrl }; //# sourceMappingURL=redux-multipurpose-angular-router.js.map