Skip to content

Commit 12d83b6

Browse files
Fix #35: Show "no network connection" text
1 parent 097f937 commit 12d83b6

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

Frontend/App.js

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React, {useContext} from 'react';
2-
import {StatusBar, StyleSheet, View, Alert} from 'react-native';
1+
import React, {useCallback, useContext} from 'react';
2+
import {StatusBar, StyleSheet, View, Alert, Text} from 'react-native';
33
import {NavigationContainer} from '@react-navigation/native';
44
import Discover from './components/Discover';
55
import DiscoverSection from './components/DiscoverSection';
@@ -21,7 +21,7 @@ import SignUp from './components/SignUp';
2121
import {DefaultTheme, Provider as PaperProvider} from 'react-native-paper';
2222
import * as Keychain from 'react-native-keychain';
2323
import MessageSection from './components/MessageSection';
24-
24+
import NetInfo from '@react-native-community/netinfo';
2525
import axios from 'axios';
2626
import {Android_Local_ADDRESS, IOS_Local_ADDRESS} from '@env';
2727
import {ChatContext} from './components/ChatContext';
@@ -78,6 +78,7 @@ const App = () => {
7878
const [isSignedIn, setIsSignedIn] = React.useState(false);
7979
const [user, setUser] = React.useState({});
8080
const [axiosInst, setaxiosInstance] = React.useState({});
81+
const [isConnected, setIsConnected] = React.useState(true);
8182

8283
const BASE_ADDRESS = 'https://campusspace.herokuapp.com';
8384
// Platform.OS === 'android' ? Android_Local_ADDRESS : IOS_Local_ADDRESS;
@@ -129,6 +130,14 @@ const App = () => {
129130
}
130131
};
131132

133+
const checkConnection = useCallback(state => {
134+
if (state.isConnected) {
135+
setIsConnected(true);
136+
} else {
137+
setIsConnected(false);
138+
}
139+
}, []);
140+
132141
React.useEffect(() => {
133142
async function init() {
134143
try {
@@ -137,7 +146,6 @@ const App = () => {
137146
GoogleSignin.configure({});
138147
await GoogleSignin.hasPlayServices();
139148

140-
// TODO: store user in storage
141149
const credentials = await Keychain.getGenericPassword();
142150

143151
if (credentials) {
@@ -153,10 +161,9 @@ const App = () => {
153161

154162
setaxiosInstance({axiosInstance});
155163

156-
// TODO: Remove this fake splash screen
157-
setTimeout(() => {
158-
setIsLoading(false);
159-
}, 2000);
164+
NetInfo.addEventListener(checkConnection);
165+
166+
setIsLoading(false);
160167
} catch (error) {
161168
console.log(error);
162169
if (error.code === statusCodes.SIGN_IN_CANCELLED) {
@@ -177,10 +184,13 @@ const App = () => {
177184
}
178185

179186
init();
187+
188+
return () => {
189+
NetInfo.removeEventListener(checkConnection);
190+
};
180191
}, []);
181192

182193
if (isLoading) {
183-
console.log('Loading');
184194
return <SplashScreen></SplashScreen>;
185195
}
186196

@@ -208,6 +218,26 @@ const App = () => {
208218
backgroundColor="transparent"
209219
translucent={true}
210220
/>
221+
{!isConnected && (
222+
<View
223+
style={{
224+
display: 'flex',
225+
alignItems: 'center',
226+
justifyContent: 'center',
227+
paddingTop: StatusBar.currentHeight + 4,
228+
paddingBottom: 4,
229+
backgroundColor: theme.colors.danger,
230+
}}>
231+
<Text
232+
style={{
233+
fontSize: 14,
234+
color: theme.colors.textHeadBlack,
235+
fontWeight: 'bold',
236+
}}>
237+
No network connection
238+
</Text>
239+
</View>
240+
)}
211241
{!isSignedIn && (
212242
<LoggedOutStack.Navigator>
213243
<LoggedOutStack.Screen

Frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
},
1212
"dependencies": {
1313
"@react-native-async-storage/async-storage": "^1.15.9",
14+
"@react-native-community/netinfo": "^6.1.0",
1415
"@react-native-firebase/app": "^13.0.0",
1516
"@react-native-firebase/firestore": "^13.0.0",
1617
"@react-native-google-signin/google-signin": "^7.0.0-alpha.3",

Frontend/yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,11 @@
12161216
resolved "https://registry.npmjs.org/@react-native-community/eslint-plugin/-/eslint-plugin-1.1.0.tgz"
12171217
integrity sha512-W/J0fNYVO01tioHjvYWQ9m6RgndVtbElzYozBq1ZPrHO/iCzlqoySHl4gO/fpCl9QEFjvJfjPgtPMTMlsoq5DQ==
12181218

1219+
"@react-native-community/netinfo@^6.1.0":
1220+
version "6.1.0"
1221+
resolved "https://registry.yarnpkg.com/@react-native-community/netinfo/-/netinfo-6.1.0.tgz#a7dd574bc5b20ecad1e6ae6494abffa2e0a4949e"
1222+
integrity sha512-SrO+EJYR1FhGqCoKSkvddIKB0waySXapW4inW2IQgpjBYCdCTJyKFLlvZHTVg/3N0vodR4WBGdKraECl9aVS5Q==
1223+
12191224
"@react-native-firebase/app@^13.0.0":
12201225
version "13.0.0"
12211226
resolved "https://registry.npmjs.org/@react-native-firebase/app/-/app-13.0.0.tgz"

0 commit comments

Comments
 (0)