forked from sonalmaniya/react-native-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
95 lines (87 loc) · 2.44 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
import React, {Component} from 'react';
import {View} from 'react-native';
import {store, persistor} from './App/Stores';
import {Provider} from 'react-redux';
import {PersistGate} from 'redux-persist/integration/react';
import axios from 'axios';
import Routes from './App/Routes/index';
import NetInfo from '@react-native-community/netinfo';
import Config from './App/Config';
import Helper from './App/Utils/Helper';
import Storage from './App/Utils/Storage';
import {AppContextProvider} from './App/AppContext';
import {NoConnection} from './App/Screens/SubComponents';
import CommonStyle from './App/Theme/CommonStyle';
axios.interceptors.request.use(
async (config) => {
let request = config;
let token = Config.token;
if (!token) {
token = await Storage.get('token');
}
request.headers = {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
Accept: 'application/json',
};
request.url = Helper.configureUrl(config.url);
return request;
},
(error) => error,
);
class App extends Component {
constructor(properties) {
super(properties);
this.state = {
isConnected: true,
};
}
componentDidMount() {
this.retryConnection();
this.NetInfoSubscription = NetInfo.addEventListener(
this.handleConnectivityChange,
);
}
componentWillUnmount() {
if (this.NetInfoSubscription) {
this.NetInfoSubscription();
}
}
// Managed internet connection
handleConnectivityChange = (info) => {
if (info.type === 'none' || !info.isConnected) {
this.setState({isConnected: false});
} else {
this.setState({isConnected: true});
}
};
// Check network connection
retryConnection = () => {
NetInfo.fetch().then((info) => {
if (info.type === 'none' && !info.isConnected) {
this.setState({isConnected: false});
} else {
this.setState({isConnected: true});
}
});
};
render() {
const {isConnected} = this.state;
return (
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<AppContextProvider>
<View style={CommonStyle.flexContainer}>
<Routes />
{(!isConnected && (
<NoConnection retryConnection={this.retryConnection} />
)) ||
null}
</View>
</AppContextProvider>
</PersistGate>
</Provider>
);
}
}
export default App;