Skip to content
This repository has been archived by the owner on Feb 8, 2020. It is now read-only.

Commit

Permalink
fix: handle route names change when all routes are removed (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 authored and osdnk committed Aug 30, 2019
1 parent b38ee1c commit 1b2983e
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 3 deletions.
128 changes: 128 additions & 0 deletions packages/example/src/Screens/AuthFlow.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import * as React from 'react';
import { View, TextInput, ActivityIndicator, StyleSheet } from 'react-native';
import { Title, Button } from 'react-native-paper';
import { ParamListBase } from '@react-navigation/core';
import {
createStackNavigator,
HeaderBackButton,
StackNavigationProp,
} from '@react-navigation/stack';

type AuthStackParams = {
splash: undefined;
home: undefined;
'sign-in': undefined;
};

const SplashScreen = () => (
<View style={styles.content}>
<ActivityIndicator />
</View>
);

const SignInScreen = ({
setUserToken,
}: {
setUserToken: (token: string) => void;
}) => {
return (
<View style={styles.content}>
<TextInput placeholder="Username" style={styles.input} />
<TextInput placeholder="Password" secureTextEntry style={styles.input} />
<Button
mode="contained"
onPress={() => setUserToken('token')}
style={styles.button}
>
Sign in
</Button>
</View>
);
};

const HomeScreen = ({
setUserToken,
}: {
setUserToken: (token: undefined) => void;
}) => {
return (
<View style={styles.content}>
<Title style={styles.text}>Signed in successfully 🎉</Title>
<Button onPress={() => setUserToken(undefined)} style={styles.button}>
Sign out
</Button>
</View>
);
};

const SimpleStack = createStackNavigator<AuthStackParams>();

type Props = {
navigation: StackNavigationProp<ParamListBase>;
};

export default function SimpleStackScreen({ navigation }: Props) {
const [isLoading, setIsLoading] = React.useState(true);
const [userToken, setUserToken] = React.useState<string | undefined>(
undefined
);

React.useEffect(() => {
const timer = setTimeout(() => setIsLoading(false), 1000);

return () => clearTimeout(timer);
}, []);

navigation.setOptions({
header: null,
});

return (
<SimpleStack.Navigator
screenOptions={{
headerLeft: () => (
<HeaderBackButton onPress={() => navigation.goBack()} />
),
}}
>
{isLoading ? (
<SimpleStack.Screen
name="splash"
component={SplashScreen}
options={{ title: `Auth Flow` }}
/>
) : userToken === undefined ? (
<SimpleStack.Screen name="sign-in" options={{ title: `Sign in` }}>
{() => <SignInScreen setUserToken={setUserToken} />}
</SimpleStack.Screen>
) : (
<SimpleStack.Screen name="home" options={{ title: 'Home' }}>
{() => <HomeScreen setUserToken={setUserToken} />}
</SimpleStack.Screen>
)}
</SimpleStack.Navigator>
);
}

const styles = StyleSheet.create({
content: {
flex: 1,
padding: 16,
justifyContent: 'center',
},
input: {
margin: 8,
padding: 10,
backgroundColor: 'white',
borderRadius: 3,
borderWidth: StyleSheet.hairlineWidth,
borderColor: 'rgba(0, 0, 0, 0.08)',
},
button: {
margin: 8,
},
text: {
textAlign: 'center',
margin: 8,
},
});
5 changes: 5 additions & 0 deletions packages/example/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import SimpleStackScreen from './Screens/SimpleStack';
import BottomTabsScreen from './Screens/BottomTabs';
import MaterialTopTabsScreen from './Screens/MaterialTopTabs';
import MaterialBottomTabs from './Screens/MaterialBottomTabs';
import AuthFlow from './Screens/AuthFlow';

YellowBox.ignoreWarnings(['Require cycle:', 'Warning: Async Storage']);

Expand All @@ -47,6 +48,10 @@ const SCREENS = {
title: 'Material Bottom Tabs',
component: MaterialBottomTabs,
},
'auth-flow': {
title: 'Auth Flow',
component: AuthFlow,
},
};

const Drawer = createDrawerNavigator<RootDrawerParamList>();
Expand Down
62 changes: 60 additions & 2 deletions packages/routers/__tests__/StackRouter.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ it('gets state on route names change', () => {
expect(
router.getStateForRouteNamesChange(
{
index: 0,
index: 2,
key: 'stack-test',
routeNames: ['bar', 'baz', 'qux'],
routes: [
Expand All @@ -157,7 +157,7 @@ it('gets state on route names change', () => {
}
)
).toEqual({
index: 0,
index: 1,
key: 'stack-test',
routeNames: ['qux', 'baz', 'foo', 'fiz'],
routes: [
Expand All @@ -166,6 +166,64 @@ it('gets state on route names change', () => {
],
stale: false,
});

expect(
router.getStateForRouteNamesChange(
{
index: 1,
key: 'stack-test',
routeNames: ['foo', 'bar'],
routes: [
{ key: 'foo-test', name: 'foo' },
{ key: 'bar-test', name: 'bar' },
],
stale: false,
},
{
routeNames: ['baz', 'qux'],
routeParamList: {
baz: { name: 'John' },
},
}
)
).toEqual({
index: 0,
key: 'stack-test',
routeNames: ['baz', 'qux'],
routes: [{ key: 'baz-test', name: 'baz', params: { name: 'John' } }],
stale: false,
});
});

it('gets state on route names change with initialRouteName', () => {
const router = StackRouter({ initialRouteName: 'qux' });

expect(
router.getStateForRouteNamesChange(
{
index: 1,
key: 'stack-test',
routeNames: ['foo', 'bar'],
routes: [
{ key: 'foo-test', name: 'foo' },
{ key: 'bar-test', name: 'bar' },
],
stale: false,
},
{
routeNames: ['baz', 'qux'],
routeParamList: {
baz: { name: 'John' },
},
}
)
).toEqual({
index: 0,
key: 'stack-test',
routeNames: ['baz', 'qux'],
routes: [{ key: 'qux-test', name: 'qux' }],
stale: false,
});
});

it('handles navigate action', () => {
Expand Down
16 changes: 15 additions & 1 deletion packages/routers/src/StackRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,25 @@ export default function StackRouter(options: StackRouterOptions) {
};
},

getStateForRouteNamesChange(state, { routeNames }) {
getStateForRouteNamesChange(state, { routeNames, routeParamList }) {
const routes = state.routes.filter(route =>
routeNames.includes(route.name)
);

if (routes.length === 0) {
const initialRouteName =
options.initialRouteName !== undefined &&
routeNames.includes(options.initialRouteName)
? options.initialRouteName
: routeNames[0];

routes.push({
key: `${initialRouteName}-${shortid()}`,
name: initialRouteName,
params: routeParamList[initialRouteName],
});
}

return {
...state,
routeNames,
Expand Down

0 comments on commit 1b2983e

Please sign in to comment.