Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add angular router's NavigationExtras support #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@
"@angular/router": "^3.0.0"
},
"devDependencies": {
"@angular/common": "^2.0.0",
"@angular/compiler": "^2.0.0",
"@angular/compiler-cli": "^0.6.2",
"@angular/core": "^2.0.0",
"@angular/platform-browser": "^2.0.0",
"@angular/common": "^2.1.2",
"@angular/compiler": "^2.1.2",
"@angular/compiler-cli": "^2.1.2",
"@angular/core": "^2.1.2",
"@angular/platform-browser": "^2.1.2",
"@angular/platform-browser-dynamic": "^2.0.0",
"@angular/platform-server": "^2.0.0",
"@angular/router": "^3.0.0",
"@angular/router": "^3.1.2",
"@types/core-js": "^0.9.32",
"@types/jasmine": "^2.2.33",
"ng2-redux": "^4.0.0-beta.1",
"redux": "^3.6.0",
"rimraf": "^2.5.4",
"rxjs": "5.0.0-beta.12",
"typescript": "^2.0.2",
"zone.js": "^0.6.23"
"rxjs": "^5.0.0-beta.12",
"typescript": "^2.0.8",
"zone.js": "^0.6.26"
},
"author": "Dag Stuan",
"license": "MIT"
Expand Down
7 changes: 7 additions & 0 deletions src/actions.ts
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
import { NavigationExtras } from '@angular/router';

export const UPDATE_LOCATION: string = "ng2-redux-router::UPDATE_LOCATION";

export interface ReduxRouterState {
location: string,
extras?: NavigationExtras
}
20 changes: 14 additions & 6 deletions src/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import { Action } from 'redux';

import { UPDATE_LOCATION } from './actions';

export const DefaultRouterState: string = '';
import { UPDATE_LOCATION, ReduxRouterState } from './actions';

export interface RouterAction extends Action {
payload: string
payload: ReduxRouterState | string
}

export function routerReducer(state: string = DefaultRouterState, action: RouterAction): string {
export const DefaultRouterState: ReduxRouterState = { location: '' };



export function routerReducer(state:ReduxRouterState = DefaultRouterState, action: RouterAction): ReduxRouterState {
switch (action.type) {
case UPDATE_LOCATION:
return action.payload || DefaultRouterState;
if(typeof action.payload === 'string') {
return {
location: action.payload
};
} else {
return action.payload || DefaultRouterState;
}
default:
return state;
}
Expand Down
46 changes: 29 additions & 17 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/map';
import { Injectable, ApplicationRef } from '@angular/core';
import { Location } from '@angular/common';
import { Router, NavigationEnd, NavigationCancel, DefaultUrlSerializer } from '@angular/router';
import { Router, NavigationEnd, NavigationCancel, DefaultUrlSerializer, NavigationExtras } from '@angular/router';
import { NgRedux } from 'ng2-redux';
import { Observable } from 'rxjs/Observable';
import { ISubscription } from 'rxjs/Subscription'
import { UPDATE_LOCATION } from './actions';
import { UPDATE_LOCATION, ReduxRouterState } from './actions';
import {
RouterAction,
DefaultRouterState
Expand All @@ -18,8 +18,8 @@ export class NgReduxRouter {
private currentLocation: string;
private initialLocation: string;

private selectLocationFromState: (state) => string = (state) => state.router;
private urlState: Observable<string>;
private selectState: (state) => ReduxRouterState = (state) => state.router;
private urlState: Observable<ReduxRouterState>;

private urlStateSubscription: ISubscription;
private reduxSubscription: ISubscription;
Expand Down Expand Up @@ -56,22 +56,22 @@ export class NgReduxRouter {
* example in the constructor of your root component.
*
*
* @param {(state: any) => string} selectLocationFromState Optional: If your
* @param {(state: any) => ReduxRouterState} selectState Optional: If your
* router state is in a custom location, supply this argument to tell the
* bindings where to find the router location in the state.
* @param {Observable<string>} urlState$ Optional: If you have a custom setup
* when listening to router changes, or use a different router than @angular/router
* you can supply this argument as an Observable of the current url state.
*/
initialize(
selectLocationFromState: (state: any) => string = (state) => state.router,
urlState$: Observable<string> = undefined
selectState: (state: any) => ReduxRouterState = (state) => state.router,
urlState$: Observable<ReduxRouterState> = undefined
) {
if (this.initialized) {
throw new Error('ng2-redux-router already initialized! If you meant to re-initialize, call destroy first.');
}

this.selectLocationFromState = selectLocationFromState
this.selectState = selectState

this.urlState = urlState$ || this.getDefaultUrlStateObservable();

Expand All @@ -83,19 +83,27 @@ export class NgReduxRouter {
private getDefaultUrlStateObservable() {
return this.router.events
.filter(event => event instanceof NavigationEnd)
.map(event => this.location.path())
.map(event => ({
location: this.location.path()
}))
.distinctUntilChanged()
}

private getLocationFromStore(useInitial: boolean = false) {
return this.selectLocationFromState(this.ngRedux.getState()) ||
return this.selectState(this.ngRedux.getState()).location ||
(useInitial ? this.initialLocation : '');
}

private listenToRouterChanges() {
const handleLocationChange = (location: string) => {
if(this.currentLocation === location) {
const handleLocationChange = (state: ReduxRouterState) => {

const location = state.location;
const extras = state.extras;

if(this.currentLocation === location || (state.extras && state.extras.skipLocationChange)) {
// Dont dispatch changes if we haven't changed location.
// If we've changed location but don't want a location change, update internal location
this.currentLocation = location;
return;
}

Expand All @@ -114,15 +122,19 @@ export class NgReduxRouter {

this.ngRedux.dispatch(<RouterAction>{
type: UPDATE_LOCATION,
payload: location
payload: {
location:location,
extras: extras
}
});
}

this.urlStateSubscription = this.urlState.subscribe(handleLocationChange);
}

private listenToReduxChanges() {
const handleLocationChange = (location: string) => {
const handleLocationChange = (state: ReduxRouterState) => {

if (this.initialLocation === undefined) {
// Wait for router to set initial location.
return;
Expand All @@ -134,12 +146,12 @@ export class NgReduxRouter {
return;
}

this.currentLocation = location
this.router.navigateByUrl(location);
this.currentLocation = state.location;
this.router.navigateByUrl(state.location, state.extras);
}

this.reduxSubscription = this.ngRedux
.select(state => this.selectLocationFromState(state))
.select(state => this.selectState(state))
.distinctUntilChanged()
.subscribe(handleLocationChange);
}
Expand Down