-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
175 lines (159 loc) · 4.86 KB
/
App.js
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { StatusBar } from 'expo-status-bar';
import { useContext, useState, useEffect } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { Ionicons } from '@expo/vector-icons';
import AsyncStorage from '@react-native-async-storage/async-storage';
import ManageExpense from './screens/ManageExpense';
import RecentExpenses from './screens/RecentExpenses';
import AllExpenses from './screens/AllExpenses';
import LoginScreen from './screens/LoginScreen';
import SignupScreen from './screens/SignupScreen';
import AuthContextProvider, { AuthContext } from './store/auth-context';
import LoadingOverlay from './components/UI/LoadingOverlay';
import { GlobalStyles } from './constants/styles';
import IconButton from './components/UI/IconButton';
import ExpensesContextProvider from './store/expenses-context';
const Stack = createNativeStackNavigator();
const BottomTabs = createBottomTabNavigator();
function AuthStack() {
return (
<Stack.Navigator
screenOptions={{
headerStyle: { backgroundColor: GlobalStyles.colors.primary500 },
headerTintColor: 'white',
contentStyle: { backgroundColor: GlobalStyles.colors.primary100 },
}}
>
<Stack.Screen name="Login" component={LoginScreen}
options={{ headerShown: false }}
/>
<Stack.Screen name="Signup" component={SignupScreen}
options={{ headerShown: false }}
/>
</Stack.Navigator>
);
}
function ExpensesOverview() {
const authCtx = useContext(AuthContext);
return (
<BottomTabs.Navigator
screenOptions={({ navigation }) => ({
headerStyle: { backgroundColor: GlobalStyles.colors.primary500 },
headerTintColor: 'white',
tabBarStyle: { backgroundColor: GlobalStyles.colors.primary500 },
tabBarActiveTintColor: GlobalStyles.colors.accent500,
headerRight: ({ tintColor }) => (
<View style={styles.uppericoncontainer}>
<IconButton icon="add" size={24}
color={tintColor}
onPress={() => {
navigation.navigate('ManageExpense');
}}
/>
<IconButton icon="exit" size={24}
color={tintColor}
onPress={authCtx.logout}
/>
</View>
),
})}
>
<BottomTabs.Screen
name="RecentExpenses"
component={RecentExpenses}
options={{
title: 'Recent Expenses',
tabBarLabel: 'Recent',
tabBarIcon: ({ color, size }) => (
<Ionicons name="hourglass" size={size} color={color} />
),
}}
/>
<BottomTabs.Screen
name="AllExpenses"
component={AllExpenses}
options={{
title: 'All Expenses',
tabBarLabel: 'All Expenses',
tabBarIcon: ({ color, size }) => (
<Ionicons name="calendar" size={size} color={color} />
),
}}
/>
</BottomTabs.Navigator>
);
}
function AuthenticatedStack() {
const authCtx = useContext(AuthContext);
return (
<>
<StatusBar style="auto" />
<ExpensesContextProvider>
<Stack.Navigator
screenOptions={{
headerStyle: { backgroundColor: GlobalStyles.colors.primary500 },
headerTintColor: 'white',
}}
>
<Stack.Screen
name="ExpensesOverview"
component={ExpensesOverview}
options={{ headerShown: false }}
/>
<Stack.Screen
name="ManageExpense"
component={ManageExpense}
options={{
presentation: 'modal',
}}
/>
</Stack.Navigator>
</ExpensesContextProvider>
</>
);
}
function Navigation() {
const authCtx = useContext(AuthContext);
return (
<NavigationContainer>
{!authCtx.isAuthenticated ? <AuthStack /> : <AuthenticatedStack />}
</NavigationContainer>
);
}
function Root() {
const [isTryingLogin, setIsTryingLogin] = useState(true);
const authCtx = useContext(AuthContext);
useEffect(() => {
async function fetchToken() {
const storedToken = await AsyncStorage.getItem('token');
if (storedToken) {
authCtx.authenticate(storedToken);
}
setIsTryingLogin(false);
}
fetchToken();
}, []);
if (isTryingLogin) {
//return <AppLoading />;
return <LoadingOverlay message="Loading..." />;
}
return <Navigation />;
}
export default function App() {
return (
<>
<StatusBar style="light" />
<AuthContextProvider>
<Root />
</AuthContextProvider>
</>
);
}
const styles = StyleSheet.create({
uppericoncontainer: {
flexDirection: 'row',
}
})