-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
121 lines (112 loc) · 3.19 KB
/
App.js
File metadata and controls
121 lines (112 loc) · 3.19 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { Spinner } from "native-base";
import React, { useEffect, useReducer, useMemo, useCallback } from "react";
import { StyleSheet } from "react-native";
import * as Font from "expo-font";
import { Ionicons } from "@expo/vector-icons";
import Splash from "./src/screens/Splash";
import Dashboard from "./src/screens/Dashboard";
import Details from "./src/screens/Details";
import Persona from "./src/screens/Persona";
import Login from "./src/screens/Login";
import { NavigationContainer } from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { AuthContext } from "./src/contexts/AuthContext";
const reducer = (prevState, action) => {
switch (action.type) {
case "RESTORE_TOKEN":
return {
...prevState,
userToken: action.token,
isLoading: false,
};
case "SIGN_IN":
return {
...prevState,
isSignedOut: false,
userToken: action.token,
};
case "SIGN_OUT":
return {
...prevState,
isSignedOut: true,
userToken: null,
};
case "FONTS_READY":
return {
...prevState,
fontsReady: true,
};
}
};
const initialState = {
isLoading: true,
fontsReady: false,
isSignedOut: false,
userToken: null,
};
export default function App() {
const [state, dispatch] = useReducer(reducer, initialState);
const initializeExpoFonts = async () => {
await Font.loadAsync({
Roboto: require("native-base/Fonts/Roboto.ttf"),
Roboto_medium: require("native-base/Fonts/Roboto_medium.ttf"),
...Ionicons.font,
});
dispatch({ type: "FONTS_READY" });
};
useEffect(() => {
const extractTokenFromAsyncStorage = async () => {
try {
let token = await AsyncStorage.getItem("id_token");
dispatch({ type: "RESTORE_TOKEN", token });
} catch (e) {
console.error(e.message);
}
};
extractTokenFromAsyncStorage();
initializeExpoFonts();
}, []);
const authContext = useMemo(
() => ({
signIn: async (data) => {
dispatch({ type: "SIGN_IN", token: "dummy-auth-token" });
},
signOut: () => {
dispatch({ type: "SIGN_OUT" });
},
}),
[]
);
const RootStack = createStackNavigator();
return state.isLoading || !state.fontsReady ? (
<Spinner size="large" />
) : (
<AuthContext.Provider value={authContext}>
{state.userToken ? (
<NavigationContainer>
<RootStack.Navigator headerMode="none">
<RootStack.Screen name="Splash" component={Splash} />
<RootStack.Screen
name="Dashboard"
component={Dashboard}
initialParams={{ itemId: -2 }}
/>
<RootStack.Screen name="Details" component={Details} />
<RootStack.Screen name="Persona" component={Persona} />
</RootStack.Navigator>
</NavigationContainer>
) : (
<Login />
)}
</AuthContext.Provider>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
});