From 0560e36eb12ac39cfe9b6d1c866758832ca36c0c Mon Sep 17 00:00:00 2001 From: Isman Usoh Date: Wed, 24 Feb 2016 09:10:54 +0700 Subject: [PATCH 1/9] duplicate for legacy version (2.x-3.x) --- .../react-router-redux-2.x-tests.ts | 20 ++++++++ .../react-router-redux-2.x.d.ts | 48 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 react-router-redux/react-router-redux-2.x-tests.ts create mode 100644 react-router-redux/react-router-redux-2.x.d.ts diff --git a/react-router-redux/react-router-redux-2.x-tests.ts b/react-router-redux/react-router-redux-2.x-tests.ts new file mode 100644 index 0000000000..9b3d59fbb1 --- /dev/null +++ b/react-router-redux/react-router-redux-2.x-tests.ts @@ -0,0 +1,20 @@ +/// +/// +/// + + + +import { createStore, combineReducers, applyMiddleware } from 'redux'; +import { browserHistory } from 'react-router'; +import { syncHistory, routeReducer } from 'react-router-redux'; + +const reducer = combineReducers({ routing: routeReducer }); + +// Sync dispatched route actions to the history +const reduxRouterMiddleware = syncHistory(browserHistory); +const createStoreWithMiddleware = applyMiddleware(reduxRouterMiddleware)(createStore); + +const store = createStoreWithMiddleware(reducer); + +// Required for replaying actions from devtools to +reduxRouterMiddleware.listenForReplays(store); diff --git a/react-router-redux/react-router-redux-2.x.d.ts b/react-router-redux/react-router-redux-2.x.d.ts new file mode 100644 index 0000000000..7248fb3afc --- /dev/null +++ b/react-router-redux/react-router-redux-2.x.d.ts @@ -0,0 +1,48 @@ +// Type definitions for react-router-redux v2.1.0 +// Project: https://github.com/rackt/react-router-redux +// Definitions by: Isman Usoh , Noah Shipley , Dimitri Rosenberg +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// +/// + +declare namespace ReactRouterRedux { + import R = Redux; + import H = HistoryModule; + + const TRANSITION: string; + const UPDATE_LOCATION: string; + + const push: PushAction; + const replace: ReplaceAction; + const go: GoAction; + const goBack: GoForwardAction; + const goForward: GoBackAction; + const routeActions: RouteActions; + + type LocationDescriptor = H.Location | H.Path; + type PushAction = (nextLocation: LocationDescriptor) => void; + type ReplaceAction = (nextLocation: LocationDescriptor) => void; + type GoAction = (n: number) => void; + type GoForwardAction = () => void; + type GoBackAction = () => void; + + interface RouteActions { + push: PushAction; + replace: ReplaceAction; + go: GoAction; + goForward: GoForwardAction; + goBack: GoBackAction; + } + interface HistoryMiddleware extends R.Middleware { + listenForReplays(store: R.Store, selectLocationState?: Function): void; + unsubscribe(): void; + } + + function routeReducer(state?: any, options?: any): R.Reducer; + function syncHistory(history: H.History): HistoryMiddleware; +} + +declare module "react-router-redux" { + export = ReactRouterRedux; +} From 48f543f2e44fc7b5cebabb3ed18f5a064945b59a Mon Sep 17 00:00:00 2001 From: Isman Usoh Date: Wed, 24 Feb 2016 09:16:56 +0700 Subject: [PATCH 2/9] update support 4.0.0 --- react-router-redux/react-router-redux-tests.ts | 15 +++++---------- react-router-redux/react-router-redux.d.ts | 16 ++++++++-------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/react-router-redux/react-router-redux-tests.ts b/react-router-redux/react-router-redux-tests.ts index 98fcdca1be..50978fafd4 100644 --- a/react-router-redux/react-router-redux-tests.ts +++ b/react-router-redux/react-router-redux-tests.ts @@ -6,15 +6,10 @@ import { createStore, combineReducers, applyMiddleware } from 'redux'; import { browserHistory } from 'react-router'; -import { syncHistory, routeReducer } from 'react-router-redux'; +import { syncHistoryWithStore, routerReducer } from 'react-router-redux'; -const reducer = combineReducers({ routing: routeReducer }); +const reducer = combineReducers({ routing: routerReducer }); +const store = createStore(reducer); -// Sync dispatched route actions to the history -const reduxRouterMiddleware = syncHistory(browserHistory); -const createStoreWithMiddleware = applyMiddleware(reduxRouterMiddleware)(createStore); - -const store = createStoreWithMiddleware(reducer); - -// Required for replaying actions from devtools to -reduxRouterMiddleware.listenForReplays(store); +// Create an enhanced history that syncs navigation events with the store +const history = syncHistoryWithStore(browserHistory, store) diff --git a/react-router-redux/react-router-redux.d.ts b/react-router-redux/react-router-redux.d.ts index 7248fb3afc..42d34baf64 100644 --- a/react-router-redux/react-router-redux.d.ts +++ b/react-router-redux/react-router-redux.d.ts @@ -1,4 +1,4 @@ -// Type definitions for react-router-redux v2.1.0 +// Type definitions for react-router-redux v4.0.0 // Project: https://github.com/rackt/react-router-redux // Definitions by: Isman Usoh , Noah Shipley , Dimitri Rosenberg // Definitions: https://github.com/borisyankov/DefinitelyTyped @@ -10,15 +10,15 @@ declare namespace ReactRouterRedux { import R = Redux; import H = HistoryModule; - const TRANSITION: string; - const UPDATE_LOCATION: string; + const CALL_HISTORY_METHOD: string; + const LOCATION_CHANGE: string; const push: PushAction; const replace: ReplaceAction; const go: GoAction; const goBack: GoForwardAction; const goForward: GoBackAction; - const routeActions: RouteActions; + const routerActions: RouteActions; type LocationDescriptor = H.Location | H.Path; type PushAction = (nextLocation: LocationDescriptor) => void; @@ -34,13 +34,13 @@ declare namespace ReactRouterRedux { goForward: GoForwardAction; goBack: GoBackAction; } - interface HistoryMiddleware extends R.Middleware { - listenForReplays(store: R.Store, selectLocationState?: Function): void; + interface HistoryMiddleware extends H.History { unsubscribe(): void; } - function routeReducer(state?: any, options?: any): R.Reducer; - function syncHistory(history: H.History): HistoryMiddleware; + function routerReducer(state?: any, options?: any): R.Reducer; + function syncHistoryWithStore(history: H.History, store: R.Store, options?: any): HistoryMiddleware; + function routerMiddleware(history: H.History): Function; } declare module "react-router-redux" { From 5799bec969f85fb3fe8885f623f0de24e379b2c7 Mon Sep 17 00:00:00 2001 From: Isman Usoh Date: Wed, 24 Feb 2016 09:21:55 +0700 Subject: [PATCH 3/9] Update react-router-redux-2.x.d.ts --- react-router-redux/react-router-redux-2.x.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/react-router-redux/react-router-redux-2.x.d.ts b/react-router-redux/react-router-redux-2.x.d.ts index 7248fb3afc..a51766fcf9 100644 --- a/react-router-redux/react-router-redux-2.x.d.ts +++ b/react-router-redux/react-router-redux-2.x.d.ts @@ -1,4 +1,4 @@ -// Type definitions for react-router-redux v2.1.0 +// Type definitions for react-router-redux v2.x - v3.x // Project: https://github.com/rackt/react-router-redux // Definitions by: Isman Usoh , Noah Shipley , Dimitri Rosenberg // Definitions: https://github.com/borisyankov/DefinitelyTyped From 9b42d75b988eb4b8a0db3387e2cefee711732cfe Mon Sep 17 00:00:00 2001 From: Isman Usoh Date: Mon, 29 Feb 2016 12:42:07 +0700 Subject: [PATCH 4/9] add syncHistoryWithStore option type --- react-router-redux/react-router-redux.d.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/react-router-redux/react-router-redux.d.ts b/react-router-redux/react-router-redux.d.ts index 42d34baf64..0d1288fe1a 100644 --- a/react-router-redux/react-router-redux.d.ts +++ b/react-router-redux/react-router-redux.d.ts @@ -37,9 +37,18 @@ declare namespace ReactRouterRedux { interface HistoryMiddleware extends H.History { unsubscribe(): void; } + + interface DefaultSelectLocationState extends Function { + (state: any): any; + } + + interface SyncHistoryWithStoreOptions { + selectLocationState?: DefaultSelectLocationState; + adjustUrlOnReplay?: boolean; + } function routerReducer(state?: any, options?: any): R.Reducer; - function syncHistoryWithStore(history: H.History, store: R.Store, options?: any): HistoryMiddleware; + function syncHistoryWithStore(history: H.History, store: R.Store, options?: SyncHistoryWithStoreOptions): HistoryMiddleware; function routerMiddleware(history: H.History): Function; } From 7c31aba5f6c80ea09bbb9aaafa7ab82748319abb Mon Sep 17 00:00:00 2001 From: Isman Usoh Date: Mon, 29 Feb 2016 12:47:41 +0700 Subject: [PATCH 5/9] change routerMiddleware return R.Middleware --- react-router-redux/react-router-redux.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/react-router-redux/react-router-redux.d.ts b/react-router-redux/react-router-redux.d.ts index 0d1288fe1a..b2df9011ae 100644 --- a/react-router-redux/react-router-redux.d.ts +++ b/react-router-redux/react-router-redux.d.ts @@ -49,7 +49,7 @@ declare namespace ReactRouterRedux { function routerReducer(state?: any, options?: any): R.Reducer; function syncHistoryWithStore(history: H.History, store: R.Store, options?: SyncHistoryWithStoreOptions): HistoryMiddleware; - function routerMiddleware(history: H.History): Function; + function routerMiddleware(history: H.History): R.Middleware; } declare module "react-router-redux" { From 67b4d6a45e43a5467ef0772ee8415092dd1db6ae Mon Sep 17 00:00:00 2001 From: Isman Usoh Date: Mon, 29 Feb 2016 12:53:10 +0700 Subject: [PATCH 6/9] rename HistoryMiddleware change HistoryMiddleware to ReactRouterReduxHistory --- react-router-redux/react-router-redux.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/react-router-redux/react-router-redux.d.ts b/react-router-redux/react-router-redux.d.ts index b2df9011ae..e3d285dc85 100644 --- a/react-router-redux/react-router-redux.d.ts +++ b/react-router-redux/react-router-redux.d.ts @@ -34,7 +34,7 @@ declare namespace ReactRouterRedux { goForward: GoForwardAction; goBack: GoBackAction; } - interface HistoryMiddleware extends H.History { + interface ReactRouterReduxHistory extends H.History { unsubscribe(): void; } From 88d5eb95aa67ed89f7bdffee85568733c374cd94 Mon Sep 17 00:00:00 2001 From: Isman Usoh Date: Mon, 29 Feb 2016 12:55:59 +0700 Subject: [PATCH 7/9] rename HistoryMiddleware rename HistoryMiddleware to ReactRouterReduxHistory --- react-router-redux/react-router-redux.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/react-router-redux/react-router-redux.d.ts b/react-router-redux/react-router-redux.d.ts index e3d285dc85..f37c25d3d2 100644 --- a/react-router-redux/react-router-redux.d.ts +++ b/react-router-redux/react-router-redux.d.ts @@ -48,7 +48,7 @@ declare namespace ReactRouterRedux { } function routerReducer(state?: any, options?: any): R.Reducer; - function syncHistoryWithStore(history: H.History, store: R.Store, options?: SyncHistoryWithStoreOptions): HistoryMiddleware; + function syncHistoryWithStore(history: H.History, store: R.Store, options?: SyncHistoryWithStoreOptions): ReactRouterReduxHistory; function routerMiddleware(history: H.History): R.Middleware; } From 6275548680004af04cfb2557cf0308ab3ad26493 Mon Sep 17 00:00:00 2001 From: Isman Usoh Date: Mon, 29 Feb 2016 13:28:07 +0700 Subject: [PATCH 8/9] Update react-router-redux-tests.ts --- .../react-router-redux-tests.ts | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/react-router-redux/react-router-redux-tests.ts b/react-router-redux/react-router-redux-tests.ts index 50978fafd4..baaec2a9c5 100644 --- a/react-router-redux/react-router-redux-tests.ts +++ b/react-router-redux/react-router-redux-tests.ts @@ -6,10 +6,25 @@ import { createStore, combineReducers, applyMiddleware } from 'redux'; import { browserHistory } from 'react-router'; -import { syncHistoryWithStore, routerReducer } from 'react-router-redux'; +import { syncHistoryWithStore, routerReducer, routerMiddleware, push, replace, go, goForward, goBack } from 'react-router-redux'; const reducer = combineReducers({ routing: routerReducer }); -const store = createStore(reducer); + +// Apply the middleware to the store +const middleware = routerMiddleware(browserHistory); +const store = createStore( + reducer, + applyMiddleware(middleware) +); // Create an enhanced history that syncs navigation events with the store -const history = syncHistoryWithStore(browserHistory, store) +const history = syncHistoryWithStore(browserHistory, store); +history.listen(location => console.log(location) ); +history.unsubscribe(); + +// Dispatch from anywhere like normal. +store.dispatch(push('/foo')); +store.dispatch(replace('/foo')); +store.dispatch(go(1)); +store.dispatch(goForward()); +store.dispatch(goBack()); From 9a81f3dc342a3734c2e0f75110d0d91f627c9ed4 Mon Sep 17 00:00:00 2001 From: Isman Usoh Date: Mon, 29 Feb 2016 14:29:19 +0700 Subject: [PATCH 9/9] Update react-router-redux.d.ts --- react-router-redux/react-router-redux.d.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/react-router-redux/react-router-redux.d.ts b/react-router-redux/react-router-redux.d.ts index f37c25d3d2..168161a179 100644 --- a/react-router-redux/react-router-redux.d.ts +++ b/react-router-redux/react-router-redux.d.ts @@ -21,12 +21,17 @@ declare namespace ReactRouterRedux { const routerActions: RouteActions; type LocationDescriptor = H.Location | H.Path; - type PushAction = (nextLocation: LocationDescriptor) => void; - type ReplaceAction = (nextLocation: LocationDescriptor) => void; - type GoAction = (n: number) => void; - type GoForwardAction = () => void; - type GoBackAction = () => void; + type PushAction = (nextLocation: LocationDescriptor) => RouterAction; + type ReplaceAction = (nextLocation: LocationDescriptor) => RouterAction; + type GoAction = (n: number) => RouterAction; + type GoForwardAction = () => RouterAction; + type GoBackAction = () => RouterAction; + type RouterAction = { + type: string + payload?: any + } + interface RouteActions { push: PushAction; replace: ReplaceAction;