-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
90 lines (86 loc) · 2.81 KB
/
App.js
File metadata and controls
90 lines (86 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import React, { useEffect, useState } from "react";
import {
StyleSheet,
Text,
View,
StatusBar,
ProgressBarAndroid,
ToastAndroid,
} from "react-native";
import Dashboard from "./components/Dashboard";
import AsyncStorage from "@react-native-community/async-storage";
import { NavigationContainer } from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
import Login from "./components/Login";
import SignUp from "./components/SignUp";
import { PRIMARY_COLOR } from "./styles/Styles";
import AuthenticationStatus from "./components/Authentication";
const Stack = createStackNavigator();
export default function App() {
const [loggedInState, setLoggedInState] = useState(null);
useEffect(() => {
// Check whether the User has already logged in.
AsyncStorage.getItem("loggedIn").then((value) => {
// Delay for clear UI transition and API response time.
setTimeout(() => {
setLoggedInState(value !== null);
}, 500);
});
}, []);
return (
<NavigationContainer>
<StatusBar backgroundColor={PRIMARY_COLOR} />
<View style={{ ...styles.container }}>
{loggedInState === null ? (
// Page for showing Authntication State
<AuthenticationStatus />
) : (
<Stack.Navigator
initialRouteName={loggedInState ? "Dashboard" : "Login"}
screenOptions={{ headerStyle: { backgroundColor: PRIMARY_COLOR } }}
>
{/* If loggedInState changes from null, based on condition initial route is decided */}
<Stack.Screen name="Login" component={Login} />
<Stack.Screen
name="SignUp"
component={SignUp}
options={{ title: "Sign Up" }}
/>
<Stack.Screen
name="Dashboard"
component={Dashboard}
options={({ navigation }) => ({
headerRight: () => (
<Text
onPress={() => {
// Log out operation in Dashboard screen.
AsyncStorage.removeItem("loggedIn").then(() => {
ToastAndroid.show(
"Logged out successfully.",
ToastAndroid.LONG
);
navigation.replace("Login");
});
}}
>
Log Out
</Text>
),
headerRightContainerStyle: {
marginRight: 8,
},
})}
/>
</Stack.Navigator>
)}
</View>
</NavigationContainer>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
justifyContent: "center",
},
});