-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApp.tsx
274 lines (232 loc) · 8.09 KB
/
App.tsx
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { MaterialCommunityIcons } from '@expo/vector-icons';
import { AntDesign } from '@expo/vector-icons';
import React, { useEffect, useState } from 'react';
import { SafeAreaView, StyleSheet } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { useFonts } from 'expo-font';
import { LogBox } from 'react-native';
import ChallengeDashboardScreen from './screens/ChallengeDashboardScreen';
import ChallengeScreen from './screens/ChallengeScreen';
import ChartsScreen from './screens/ChartsScreen';
import HomeScreen from './screens/HomeScreen';
import PocketsScreen from './screens/PocketsScreen';
import LoginScreen from './screens/LoginScreen';
import { getUser } from './services/UserServices';
import { getTransactionsByUserId } from './services/TransactionServices';
import { getGoalsByUserId } from './services/GoalServices';
import Header from './components/Header';
import { User, onAuthStateChanged } from '@firebase/auth';
import { FirebaseAuth } from './FirebaseConfig';
LogBox.ignoreLogs([
'Constants.platform.ios.model has been deprecated in favor of expo-device\'s Device.modelName property.',
]);
// Creating the stacks
const Stack = createNativeStackNavigator();
const LoginStack = createNativeStackNavigator();
const InsideStack = createNativeStackNavigator();
type AppProps = {};
export default function App(_: AppProps) {
const [currentUser, setCurrentUser] = useState<any>(null);
const [userTransactions, setUserTransactions] = useState([]);
//Pocket State
const [goals, setGoals] = useState<any[]>([]);
// Fetch & set goals for currentUser when currentUser changes
useEffect(() => {
if (currentUser) {
getGoalsByUserId(currentUser.id)
.then((goals) => setGoals(goals))
.catch((error) => console.log('Error fetching goals:', error));
} else {
// If there is no current user, reset goals to an empty array
setGoals([]);
}
}, [currentUser]);
// // Pocket State
// const [goals, setGoals] = useState(currentUser?.goals);
// // console.log("Goals (App) ", goals)
// useEffect(() => {
// if (currentUser) {
// // console.log("Current User")
// // console.log(goals.length)
// goals.length == 0 &&
// getGoalsByUserId(currentUser.id)
// .then((goals) => setGoals(goals))
// .catch((error) => console.log('Error fetching goals:', error));
// } else {
// setGoals([])
// console.log("empty goals")
// }
// }, [currentUser?.goals, currentUser]);
useEffect(() => {
onAuthStateChanged(FirebaseAuth, (user) => {
if (user) {
getUser(user.uid)
.then((newUser) => {
setCurrentUser(newUser);
})
.catch((error) => console.log('Error fetching user:', error));
} else {
setCurrentUser(null);
}
});
}, []);
useEffect(() => {
if (currentUser)
getTransactionsByUserId(currentUser.id)
.then((transactions) => setUserTransactions(transactions))
.catch((error) => console.log('Error fetching transactions:', userTransactions));
}, [currentUser]);
// Creating the component for inside stack layout
function InsideLayout() {
return (
<>
<Header/>
<InsideStack.Navigator>
<InsideStack.Screen name="bottomTabs" component={TabNavigator} options={{ headerShown: false }} />
</InsideStack.Navigator>
</>
);
}
// Creating the bottom tab navigator & view functions
const Tab = createBottomTabNavigator();
function TabNavigator() {
return (
<Tab.Navigator>
<Tab.Screen
name="HO"
component={Home}
options={{
tabBarLabel: 'HOME',
tabBarShowLabel: false,
tabBarIcon: ({ color }) => (
<MaterialCommunityIcons name="home" color={color} size={45} />
),
tabBarActiveTintColor: '#35d0ba',
tabBarInactiveTintColor: 'black',
headerShown: false,
}}
/>
<Tab.Screen
name="DC"
options={{
tabBarLabel: 'GAME',
tabBarShowLabel: false,
tabBarIcon: ({ color }) => (
<MaterialCommunityIcons name="gamepad" color={color} size={45} />
),
tabBarActiveTintColor: '#ff9234',
tabBarInactiveTintColor: 'black',
headerShown: false,
}}>
{(props) => <GamesHub {...props} currentUser={currentUser} setCurrentUser={setCurrentUser} />}
</Tab.Screen>
<Tab.Screen
name="HT"
component={Habits}
options={{
tabBarLabel: 'CHART',
tabBarShowLabel: false,
tabBarIcon: ({ color }) => (
<AntDesign name="barschart" color={color} size={45} />
),
tabBarActiveTintColor: '#f15c55',
tabBarInactiveTintColor: 'black',
headerShown: false,
}}
/>
<Tab.Screen
name="SG"
component={Pockets}
options={{
tabBarLabel: 'POCKETS',
tabBarShowLabel: false,
tabBarIcon: ({ color }) => (
<MaterialCommunityIcons name="wallet" color={color} size={45} />
),
tabBarActiveTintColor: '#ffcd3c',
tabBarInactiveTintColor: 'black',
headerShown: false,
}}
/>
</Tab.Navigator>
);
}
// View Functions (Bottom Bar Tabs)
function GamesHub({ currentUser, setCurrentUser }) {
return (
<Stack.Navigator>
<Stack.Screen
name="ChallengeDashBoardScreen"
component={ChallengeDashboardScreen}
options={{ headerShown: false }}
/>
<Stack.Screen
name="ChallengeScreen"
options={{ headerShown: false }}
>
{(props) => <ChallengeScreen {...props} currentUser={currentUser} setCurrentUser={setCurrentUser} />}
</Stack.Screen>
</Stack.Navigator>
);
}
function Habits() {
return <ChartsScreen currentUser={currentUser} userTransactions={userTransactions} />;
}
function Pockets() {
return <PocketsScreen currentUser={currentUser} goals={goals} setGoals={setGoals} />;
}
function Home() {
const [availableAmount, setAvailableAmount] = useState<number>(0);
return (
<HomeScreen
currentUser={currentUser}
setCurrentUser={setCurrentUser}
availableAmount={availableAmount}
setAvailableAmount={setAvailableAmount}
userTransactions={userTransactions}
setUserTransactions={setUserTransactions}
goals={goals}
setGoals={setGoals}
/>
);
}
const [fontsLoaded] = useFonts({
'OpenDyslexic-Regular': require('./assets/font/OpenDyslexic-Regular.otf'),
'OpenDyslexic-Italic': require('./assets/font/OpenDyslexic-Italic.otf'),
'OpenDyslexic-Bold': require('./assets/font/OpenDyslexic-Bold.otf'),
'OpenDyslexic-BoldItalic': require('./assets/font/OpenDyslexic-BoldItalic.otf'),
});
if (!fontsLoaded) {
return null; // Render a loading indicator or return an empty view
}
return (
<SafeAreaView style={styles.container}>
<NavigationContainer >
<LoginStack.Navigator initialRouteName="Login">
{currentUser ? (
<>
<LoginStack.Screen name="Inside" component={InsideLayout} options={{ headerShown: false }} />
</>
) : (
<LoginStack.Screen
name="Smart Savers Login"
options={{ headerShown: false }}>
{(props) => (
<LoginScreen {...props} setCurrentUser={setCurrentUser} />
)}
</LoginStack.Screen>
)}
</LoginStack.Navigator>
</NavigationContainer>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
fontFamily: 'OpenDyslexic-Regular',
flex: 1,
// backgroundColor: '#FDE9B1', // Pastel yellow color
},
});