-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
120 lines (104 loc) · 3.23 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
import React, { useEffect, useRef, useState } from "react";
import {
Provider as ReduxProvider,
useSelector,
useDispatch,
} from "react-redux";
import { AppLoading } from "expo";
import * as SecureStore from "expo-secure-store";
import { NavigationContainer } from "@react-navigation/native";
import { createDrawerNavigator } from "@react-navigation/drawer";
import { createStackNavigator } from "@react-navigation/stack";
import { DefaultTheme, Provider as PaperProvider } from "react-native-paper";
import SideBar from "./components/SideBar.js";
import AppSnackbar from "./components/AppSnackbar";
import AppNavigation from "./screens/AppNavigation";
import Login from "./screens/Login";
import store, { login, offlineLogin } from "./store";
import * as notifications from "./util/notifications.js";
const theme = {
...DefaultTheme,
colors: {
...DefaultTheme.colors,
primary: "#2f80ed",
accent: "#f1c40f",
},
};
const DrawerNavigator = createDrawerNavigator();
const AuthNavigator = createStackNavigator();
export default function App() {
const navRef = useRef();
return (
<PaperProvider theme={theme}>
<ReduxProvider store={store}>
<NavigationContainer ref={navRef}>
<AuthController navRef={navRef}>
<DrawerNavigator.Navigator
initialRouteName="AppNavigation"
drawerContent={SideBar}
>
<DrawerNavigator.Screen
name="AppNavigation"
component={AppNavigation}
/>
</DrawerNavigator.Navigator>
</AuthController>
</NavigationContainer>
<AppSnackbar />
</ReduxProvider>
</PaperProvider>
);
}
function AuthController({ children, navRef }) {
const dispatch = useDispatch();
const auth = useSelector((s) => s.auth);
const isInternetReachable = useSelector(
(s) => s.connection.isInternetReachable
);
const [loaded, setLoaded] = useState(false);
useEffect(() => {
(async () => {
if (isInternetReachable === true) {
const email = await SecureStore.getItemAsync("CREDENTIALS_EMAIL");
const password = await SecureStore.getItemAsync("CREDENTIALS_PASSWORD");
if (email && password) {
await dispatch(login(email, password));
}
} else if (isInternetReachable === false) {
dispatch(offlineLogin());
} else {
// Connection details not yet available
return;
}
setLoaded(true);
})();
}, [isInternetReachable]);
useEffect(() => {
if (
isInternetReachable === true &&
!!auth.user &&
auth.user.offline === false
) {
console.log("Registering push notifications");
notifications.registerForPushNotifications();
notifications.registerNotificationHandlers((postId) => {
navRef.current.navigate("UpdateDetails", { postId });
});
}
}, [auth, isInternetReachable]);
if (!loaded) {
return <AppLoading />;
}
if (!!auth.user) {
return children;
} else {
return (
<AuthNavigator.Navigator
screenOptions={{ headerShown: false }}
initialRouteName="Login"
>
<AuthNavigator.Screen name="Login" component={Login} />
</AuthNavigator.Navigator>
);
}
}