-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
72 lines (65 loc) · 1.87 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
import React, { useEffect, useState } from 'react'
import { StatusBar } from 'expo-status-bar'
import AsyncStorage from '@react-native-community/async-storage'
import { NavigationContainer } from '@react-navigation/native'
import AppNavigator from './app/navigation/AppNavigator'
import AuthNavigator from './app/navigation/AuthNavigator'
import TripShowContext from './app/context/TripShowContext'
import UserContext from './app/context/userContext'
export default function App() {
const [user, setUser] = useState({
username: '',
userId: '',
})
const [showTrip, setShowTrip] = useState('')
const checkForUser = async () => {
try {
const localUser = await AsyncStorage.getItem('username')
const localId = await AsyncStorage.getItem('userId')
if (localUser)
setUser({
username: localUser,
userId: localId,
})
} catch (error) {
console.log(error)
}
}
useEffect(() => {
checkForUser()
}, [user.userId])
const logout = async () => {
try {
setUser({ username: '', userId: '' })
await AsyncStorage.setItem('username', '')
await AsyncStorage.setItem('userId', '')
await AsyncStorage.setItem('tripActive', 'false')
} catch (error) {
console.log(error)
}
}
return (
<>
<NavigationContainer>
<UserContext.Provider
value={{
username: user.username,
userId: user.userId,
setUser: setUser,
logout: logout,
}}
>
<TripShowContext.Provider
value={{
showTrip: showTrip,
setShowTrip: setShowTrip,
}}
>
{user.username ? <AppNavigator /> : <AuthNavigator />}
</TripShowContext.Provider>
</UserContext.Provider>
</NavigationContainer>
<StatusBar style="auto" />
</>
)
}