This repository has been archived by the owner on Feb 8, 2020. It is now read-only.
generated from satya164/typescript-template
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle route names change when all routes are removed (#86)
- Loading branch information
Showing
4 changed files
with
208 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters