-
Notifications
You must be signed in to change notification settings - Fork 0
/
Index.js
135 lines (123 loc) · 5.2 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
import 'react-native-gesture-handler';
import React, { useState, useEffect, Fragment, useRef } from 'react';
import { connect } from 'react-redux';
import { AsyncStorage, AppState } from 'react-native';
import { loadUser, loadToken, notAuthenticated } from './actions/auth';
import Login from './components/Login';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import DropdownAlert from 'react-native-dropdownalert';
import Home from './components/Home';
import Loading from './components/layout/Loading';
import Landing from './components/Business/Landing';
import Searching from './components/Business/Searching';
import BusinessPage from './components/Business/BusinessPage';
import { Provider } from 'react-redux';
import store from './store';
import Account from './components/Account';
import Checkin from './components/Checkin';
import SearchLoading from './components/layout/SearchLoading';
import ExampleBusinessPage from './components/Business/ExampleBusinessPage';
import UnverifiedBusinessPage from './components/Business/UnverifiedBusinessPage';
import Signup from './components/Signup';
import CheckedIn from './components/Checkedin';
import BusinessHistoryList from './components/Business/BusinessHistoryList';
import Favorites from './components/Business/Favorites';
import UserReservations from './components/Business/UserReservations';
import DbBusinessPage from './components/Business/DbBusinessPage';
import ErrorDisplay from './components/layout/ErrorDisplay';
import MenuPage from './components/Business/Pickup/MenuPage';
const Stack = createStackNavigator();
const Index = ({ auth }) => {
const [isAuth, setAuth] = useState(0);
const initialMount = useRef(true);
const [currentAppState, changeAppState] = useState(AppState.currentState);
useEffect(() => {
if (initialMount.current) {
initialMount.current = false;
} else {
console.log("Auth State Changed");
console.log("Running Auth Check");
if (auth.isAuthenticated) {
setAuth(2);
store.dispatch(loadUser());
console.log("Is Authenticated First Time");
} else {
console.log("Checking local storage");
isAuthenticated();
}
}
}, [auth]);
useEffect(() => {
AppState.addEventListener('change', (nextAppState) => {
if (nextAppState === 'active') {
console.log("returning to active");
isAuthenticated();
}
});
AsyncStorage.getItem('token', function (err, res) {
if (res) {
store.dispatch(loadToken(res));
} else {
store.dispatch(notAuthenticated());
}
});
}, [null]);
const isAuthenticated = async () => {
const token = await AsyncStorage.getItem('token');
if (token) {
store.dispatch(loadUser());
setAuth(2);
console.log("Is Authenticated ");
} else {
console.log("Not Authenticated");
setAuth(1);
}
};
const handleAppStateChange = nextAppState => {
};
const authController = () => {
if (isAuth === 1) {
return (
<Fragment>
<Stack.Screen name="Login" component={Login} />
<Stack.Screen name="Signup" component={Signup} />
</Fragment>
);
}
if (isAuth === 2) {
return (
<Fragment>
<Stack.Screen name="Landing" component={Landing} />
<Stack.Screen name="Searching" component={Searching} />
<Stack.Screen name="BusinessPage" component={BusinessPage} />
<Stack.Screen name="Account" component={Account} />
<Stack.Screen name="Checkin" component={Checkin} />
<Stack.Screen name="SearchLoading" component={SearchLoading}></Stack.Screen>
<Stack.Screen name="ExampleBusinessPage" component={ExampleBusinessPage} />
<Stack.Screen name="UnverifiedBusinessPage" component={UnverifiedBusinessPage} />
<Stack.Screen name="CheckedIn" component={CheckedIn} />
<Stack.Screen name="BusinessHistoryList" component={BusinessHistoryList} />
<Stack.Screen name="Favorites" component={Favorites} />
<Stack.Screen name="UserReservations" component={UserReservations} />
<Stack.Screen name="DbBusinessPage" component={DbBusinessPage} />
<Stack.Screen name="MenuPage" component={MenuPage} />
</Fragment>
);
}
};
return (
<NavigationContainer>
{isAuth === 0 ? <Loading /> :
<Stack.Navigator screenOptions={{ headerShown: false }}>
{authController()}
</Stack.Navigator>
}
<ErrorDisplay />
</NavigationContainer>
);
};
const mapStateToProps = state => ({
auth: state.auth
});
export default connect(mapStateToProps, { loadUser })(Index);