forked from Levi-Ramirez/sleepRecs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndex.js
193 lines (144 loc) · 6.5 KB
/
Index.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// import { StatusBar } from 'expo-status-bar';
import { NavigationContainer, useNavigation } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import LoginScreen from './screens/LoginScreen';
import RegisterScreen from './screens/RegisterScreen';
import RegisterInfo from './screens/RegisterInfo';
// import QuestInfoScreen from './screens/QuestInfoScreen';
import CreatePost from './screens/CreatePost';
import UserLogsScreen from './screens/UserLogsScreen';
import Home from './screens/Home';
import { React, useEffect, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import Tabs from './components/Tab';
import { setUser } from './redux/slices/user';
import { getAuth, onAuthStateChanged, initializeAuth } from 'firebase/auth'
import { collection, getDoc, updateDoc } from 'firebase/firestore';
import { database, doc, setDoc, addDoc } from './firebaseConfig';
import ActivityScreen from './screens/ActivityScreen';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { getUID } from './components/Helpers';
const Stack = createNativeStackNavigator();
// console.log(uid)
// Start a timer for 5 seconds
export default function Index() {
// gives root state in call back: user slice
const user = useSelector(state => state.user.currentUser)
const dispatch = useDispatch()
const auth = getAuth()
// let loggedSleepAsync = false
const [loggedSleepAsync, setLoggedSleepAsync] = useState(false);
// setup logged_sleep async boolean value
useEffect(() => {
// function that checks if user logged sleep for the day
const checkLoggedSleep = async () => {
const uid = getUID()
const userRef = doc(database, 'users', uid);
// gets logged sleep status, current date as a string, and the date user logged sleep
// const logged_sleep = await AsyncStorage.getItem(uid + '|' + 'logged_sleep');
const currentDate = new Date().toDateString()
// const postedDate = await AsyncStorage.getItem(uid + '|' + 'logged_date');
let logged_sleep = ""
let postedDate = ""
try {
const userSnapShot = await getDoc(userRef)
if (userSnapShot.exists()) {
userInfo = userSnapShot.data()
logged_sleep = userInfo.loggedSleep
postedDate = userInfo.loggedDate
}
else {
console.log("failed to get user data")
}
}
catch (error) {
console.log('failed to last entry date ', error.message)
return null
}
console.log("logggedSleep in index", logged_sleep)
// checks if logged sleep exists
if (logged_sleep) {
console.log("logggedSleep in index", logged_sleep)
// if user logged sleep we will check if the usexr entered sleep for the current date
if (logged_sleep === 'true') {
// if the current date and posted date are the same then the user already posted sleep for the current date
if (currentDate === postedDate) {
console.log('Already logged sleep')
setLoggedSleepAsync(true)
// else the user should be prompted to enter the sleep logs since it is a new day
// this only happens when the user opens the app for the first time on a new day
} else {
console.log('New day log sleep again')
// await AsyncStorage.setItem(uid + '|' + "logged_sleep", "false")
try {
await updateDoc(userRef, {
loggedSleep: "false"
})
}
catch (error) {
console.log("error updating data: ", error.message)
}
setLoggedSleepAsync(false)
}
// if the user never entered sleep logs for the current day then make them do it
} else {
console.log('Logged sleep was false')
setLoggedSleepAsync(false)
}
// if the logged sleep doesnt exist then make user log sleep anyways
} else {
console.log('logged value does not exist')
// await AsyncStorage.setItem(uid + '|' + 'logged_sleep', 'false')
try {
await updateDoc(userRef, {
loggedSleep: "false"
})
}
catch (error) {
console.log("error updating data: ", error.message)
}
setLoggedSleepAsync(false)
}
}
if (user) {
checkLoggedSleep();
}
}, [user]);
onAuthStateChanged(auth, (u) => {
let email = null
if (u != null) {
email = u.email
}
dispatch(setUser(email))
})
if (user) {
if (!loggedSleepAsync) {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="CreatePost" component={CreatePost} options={{ headerShown: false }} />
<Stack.Screen name="Tabs" component={Tabs} options={{ headerShown: false }} />
</Stack.Navigator>
</NavigationContainer>
);
}
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Tabs" component={Tabs} options={{ headerShown: false }} />
</Stack.Navigator>
</NavigationContainer>
);
}
else {
return (
<NavigationContainer initialRouteName='Login'>
<Stack.Navigator >
<Stack.Screen name='Login' component={LoginScreen} options={{ headerShown: false }} />
<Stack.Screen name='Register' component={RegisterScreen} options={{ headerShown: false }} />
<Stack.Screen name="Info" component={RegisterInfo} options={{ headerShown: false }} />
</Stack.Navigator>
</NavigationContainer>
);
}
}