Browse Source

Remove redux-sac, multiple named exports.

Now exports relevant navigation parts in basic state
(operating on root of state).
You need to subReducer / subMiddleware / connectNavigator
them yourself.
Herby Vojčík 6 years ago
parent
commit
c49b5a80c1
2 changed files with 35 additions and 57 deletions
  1. 34 55
      index.js
  2. 1 2
      package.json

+ 34 - 55
index.js

@@ -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
-    };
+    },
 };

+ 1 - 2
package.json

@@ -20,7 +20,6 @@
   "dependencies": {
     "react-navigation": "^1.2.1",
     "react-navigation-redux-helpers": "^1.0.1",
-    "react-redux": "^5.0.7",
-    "redux-sac": "^0.6.0"
+    "react-redux": "^5.0.7"
   }
 }