Skip to content

Commit 72e2aef

Browse files
committed
fix: Fixed an issue where stack nav actions were throwing errors
1 parent 162e6e4 commit 72e2aef

5 files changed

Lines changed: 53 additions & 24 deletions

File tree

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"fix:lint": "eslint src bluebase --ext=ts,tsx --fix .",
3030
"fix:md": "remark . -o",
3131
"test": "run-s build test:*",
32-
"test:only": "jest --coverage",
32+
"test:only": "jest --coverage --runInBand",
3333
"test:lint": "npm run lint",
3434
"test:prettier": "prettier \"src/**/*.ts\" --list-different",
3535
"test:md": "remark . -f",
@@ -79,6 +79,7 @@
7979
"@react-navigation/native-stack": "^5.0.5",
8080
"@react-navigation/stack": "^5.9.0",
8181
"expo": "^38.0.0",
82+
"lodash.get": "^4.4.2",
8283
"react-native": "https://github.com/expo/react-native/archive/sdk-38.0.2.tar.gz",
8384
"react-native-reanimated": "~1.9.0",
8485
"react-native-safe-area-context": "~3.0.7",
@@ -102,6 +103,7 @@
102103
"@types/enzyme-adapter-react-16": "^1.0.6",
103104
"@types/enzyme-async-helpers": "^0.9.1",
104105
"@types/jest": "^26.0.8",
106+
"@types/lodash.get": "^4.4.6",
105107
"@types/react": "~16.9.41",
106108
"@types/react-dom": "^16.9.5",
107109
"@types/react-native": "~0.62.13",

src/helpers/__tests__/navigationToActionObject.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable camelcase */
12
import { execAction, navigationToActionObject } from '../navigationToActionObject';
23

34
import { NavigationActionsObject } from '@bluebase/components';
@@ -26,7 +27,7 @@ const route = {
2627
isTransitioning: false,
2728
key: 'kjdkj',
2829
name: 'Home',
29-
params: { foo: 'bar' },
30+
params: { foo: 'bar', __path_url__: '/' },
3031
path: '/',
3132
routes: [
3233
{
@@ -72,10 +73,13 @@ describe('navigationToActionObject', () => {
7273
navigate: () => ({}),
7374
router: {},
7475
state: {},
76+
dispatch: jest.fn(),
7577
};
7678
const result = navigationToActionObject(nav as any, route);
7779
result.push('');
7880
expect(result.push).toBeTruthy();
81+
result.pop();
82+
expect(result.pop).toBeTruthy();
7983
result.replace('');
8084
expect(result.replace).toBeTruthy();
8185
});

src/helpers/navigationToActionObject.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,34 @@ import {
55
NavitionActionRouteNamePayload,
66
} from '@bluebase/components';
77

8-
const noop = (..._params: any[]) => {
9-
return null;
10-
};
8+
import { StackActions } from '@react-navigation/native';
119

1210
/**
1311
* Convert a react-navigation's navigation prop to NavigationActionsObject
1412
* @param navigation
1513
*/
1614
export const navigationToActionObject = (navigation: any, state: any): NavigationActionsObject => {
17-
const {
18-
navigate,
19-
push = noop,
20-
pop = noop,
21-
replace = noop,
22-
goBack,
23-
setParams,
24-
} = navigation as any;
15+
const { navigate, goBack, setParams, dispatch } = navigation as any;
16+
17+
const push = (routeName: string, params: any) => {
18+
const pushAction = StackActions.push(routeName, params);
19+
dispatch(pushAction);
20+
};
21+
22+
const pop = () => {
23+
const pushAction = StackActions.pop();
24+
dispatch(pushAction);
25+
};
26+
27+
const replace = (routeName: string, params: any) => {
28+
const pushAction = StackActions.replace(routeName, params);
29+
dispatch(pushAction);
30+
};
2531

2632
const otherParams: any = { ...state.params };
2733

2834
// Extract internal variables
29-
const url = `/${otherParams.__path_url__}`;
35+
const url = otherParams.__path_url__ ? `/${otherParams.__path_url__}` : undefined;
3036
const search = otherParams.__path_search__;
3137

3238
// Delete internal flags

src/helpers/preparePaths.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { NavigatorProps, RouteConfig } from '@bluebase/components';
33

44
import { ScreenProps } from '../types';
55
import { createNavigatorScreenComponent } from './createNavigatorScreenComponent';
6+
import get from 'lodash.get';
67
import { getNavigatorFn } from './getNavigatorFn';
78

89
export interface PreparedNavigatorProps extends NavigatorProps {
@@ -29,16 +30,20 @@ export const preparePaths = (
2930

3031
// If routes prop is a thunk, resolve it.
3132
// Then map it to have new paths
32-
const routes = resolveThunk(navigator.routes || [], screenProps).map((r: RouteConfig) => {
33-
// Do we have a navigator here? If yes then recurcively prepare its paths as well
34-
const resolvedNavigator = r.navigator ? preparePaths(r.navigator, screenProps, BB) : undefined;
35-
36-
// Do we have a screen here? If yes then resolve the screen component
37-
const component = createNavigatorScreenComponent(r, BB);
38-
39-
// Return the final object
40-
return { ...r, navigator: resolvedNavigator, component };
41-
});
33+
const routes = resolveThunk(get(navigator, 'routes', [] as RouteConfig[]), screenProps).map(
34+
(r: RouteConfig) => {
35+
// Do we have a navigator here? If yes then recurcively prepare its paths as well
36+
const resolvedNavigator = r.navigator
37+
? preparePaths(r.navigator, screenProps, BB)
38+
: undefined;
39+
40+
// Do we have a screen here? If yes then resolve the screen component
41+
const component = createNavigatorScreenComponent(r, BB);
42+
43+
// Return the final object
44+
return { ...r, navigator: resolvedNavigator, component };
45+
}
46+
);
4247

4348
// Merge and return incoming navigator with newer routes
4449
return { ...navigator, NavigatorComponent, routes };

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,6 +2622,18 @@
26222622
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
26232623
integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4=
26242624

2625+
"@types/lodash.get@^4.4.6":
2626+
version "4.4.6"
2627+
resolved "https://registry.yarnpkg.com/@types/lodash.get/-/lodash.get-4.4.6.tgz#0c7ac56243dae0f9f09ab6f75b29471e2e777240"
2628+
integrity sha512-E6zzjR3GtNig8UJG/yodBeJeIOtgPkMgsLjDU3CbgCAPC++vJ0eCMnJhVpRZb/ENqEFlov1+3K9TKtY4UdWKtQ==
2629+
dependencies:
2630+
"@types/lodash" "*"
2631+
2632+
"@types/lodash@*":
2633+
version "4.14.161"
2634+
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.161.tgz#a21ca0777dabc6e4f44f3d07f37b765f54188b18"
2635+
integrity sha512-EP6O3Jkr7bXvZZSZYlsgt5DIjiGr0dXP1/jVEwVLTFgg0d+3lWVQkRavYVQszV7dYUwvg0B8R0MBDpcmXg7XIA==
2636+
26252637
"@types/minimatch@*":
26262638
version "3.0.3"
26272639
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"

0 commit comments

Comments
 (0)