|
@@ -1,75 +1,54 @@
|
|
|
"use strict";
|
|
|
|
|
|
-import {addNavigationHelpers, NavigationActions} from 'react-navigation';
|
|
|
+import React, {Component} from 'react';
|
|
|
+import {addNavigationHelpers} from 'react-navigation';
|
|
|
import {createReactNavigationReduxMiddleware, createReduxBoundAddListener} from 'react-navigation-redux-helpers';
|
|
|
-import {subReducer, cowValueModel as cow} from 'redux-sac';
|
|
|
import {connect} from 'react-redux';
|
|
|
|
|
|
-const {navigate, back, reset} = NavigationActions;
|
|
|
+export const navigatorPieces = (BareNavigator, name = "root") => {
|
|
|
+ const middleware = createReactNavigationReduxMiddleware(name, x => x);
|
|
|
+ const addListener = createReduxBoundAddListener(name);
|
|
|
+ const reducer = (state, action) => BareNavigator.router.getStateForAction(action, state);
|
|
|
+
|
|
|
+ class Navigator extends Component {
|
|
|
+ render () {
|
|
|
+ const {dispatch, navigationState} = this.props;
|
|
|
+ return <BareNavigator
|
|
|
+ navigation={addNavigationHelpers({dispatch, state: navigationState, addListener})}
|
|
|
+ />;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return {Navigator, reducer, middleware};
|
|
|
+};
|
|
|
|
|
|
-export default navigationStateKey => {
|
|
|
- const accessNavigationState = cow(navigationStateKey);
|
|
|
+export const connectNavigator = selectorFn => connect(state => ({navigationState: selectorFn(state)}));
|
|
|
|
|
|
- const createNavigationReduxConnector = name => ({
|
|
|
- middleware: createReactNavigationReduxMiddleware(name, accessNavigationState),
|
|
|
- connectNavigation: connect(
|
|
|
- state => ({state: accessNavigationState(state)}),
|
|
|
- null,
|
|
|
- ({state}, {dispatch}, ownProps) => ({
|
|
|
- navigation: addNavigationHelpers({dispatch, state, addListener: createReduxBoundAddListener(name)}),
|
|
|
- ...ownProps
|
|
|
- })
|
|
|
- )
|
|
|
- });
|
|
|
+import {NavigationActions} from 'react-navigation';
|
|
|
|
|
|
- const backButtonHandler = ({dispatch, getState}) => {
|
|
|
- if (accessNavigationState(getState()).index === 0) return false;
|
|
|
- dispatch(back());
|
|
|
- return true;
|
|
|
- };
|
|
|
+const {navigate, back, reset} = NavigationActions;
|
|
|
|
|
|
- const topOfNavigationStack = state => {
|
|
|
- const {routes} = accessNavigationState(state);
|
|
|
- return routes[routes.length - 1] || {};
|
|
|
- };
|
|
|
+export const backButtonHandler = ({dispatch, getState}) => {
|
|
|
+ if (getState().index === 0) return false;
|
|
|
+ dispatch(back());
|
|
|
+ return true;
|
|
|
+};
|
|
|
|
|
|
- const createConnectedNavigatorPieces = (BareNavigator, name = "root") => {
|
|
|
- const {middleware, connectNavigation} = createNavigationReduxConnector(name);
|
|
|
- const bareReducer = (state, action) => BareNavigator.router.getStateForAction(action, state);
|
|
|
- return {
|
|
|
- Navigator: connectNavigation(BareNavigator),
|
|
|
- reducer: subReducer(navigationStateKey, bareReducer),
|
|
|
- middleware
|
|
|
- };
|
|
|
- };
|
|
|
+export const navigationHelpers = {
|
|
|
+ topOfNavigationStack: ({routes}) => routes[routes.length - 1] || {},
|
|
|
|
|
|
- const resetRoutes = routeNames => reset({
|
|
|
+ resetRoutes: routeNames => reset({
|
|
|
actions: routeNames.map(routeName => navigate({routeName})),
|
|
|
index: routeNames.length - 1
|
|
|
- });
|
|
|
+ }),
|
|
|
|
|
|
- const isRoute = expected => ({routeName}) => routeName === expected;
|
|
|
+ isRoute: expected => ({routeName}) => routeName === expected,
|
|
|
|
|
|
- const isTopRoute = expected => state => isRoute(expected)(topOfNavigationStack(state));
|
|
|
+ isTopRoute: expected => state => isRoute(expected)(topOfNavigationStack(state)),
|
|
|
|
|
|
- const goBackFrom = routeName => ({dispatch, getState}) => {
|
|
|
+ goBackFrom: routeName => ({dispatch, getState}) => {
|
|
|
if (isTopRoute(routeName)(getState())) {
|
|
|
dispatch(back());
|
|
|
}
|
|
|
- };
|
|
|
-
|
|
|
- const getNavigationState = state => accessNavigationState(state);
|
|
|
-
|
|
|
- return {
|
|
|
- backButtonHandler,
|
|
|
- api: {
|
|
|
- getNavigationState,
|
|
|
- topOfNavigationStack,
|
|
|
- resetRoutes,
|
|
|
- isRoute,
|
|
|
- isTopRoute,
|
|
|
- goBackFrom
|
|
|
- },
|
|
|
- createConnectedNavigatorPieces
|
|
|
- };
|
|
|
+ },
|
|
|
};
|