-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.jsx
243 lines (227 loc) · 7.66 KB
/
App.jsx
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import { LogBox, StyleSheet, Text, View} from 'react-native';
import React, {useEffect, useState} from 'react';
import {
NativeBaseProvider,
} from 'native-base';
import {SafeAreaProvider} from 'react-native-safe-area-context';
import {
colorModeManager,
NativeBaseTheme,
PreferencesContext,
} from './theme';
import useColorScheme from './theme/hooks/useColorScheme';
import { useNavigation} from '@react-navigation/native';
import AuthStackNavigator from './src/navigation/auth-navigator';
import {getToken} from './utils/storage-helpers';
import Toast, {BaseToast, ErrorToast} from 'react-native-toast-message';
import './src/language/DCSLocalize';
import {useNetInfo} from '@react-native-community/netinfo';
import {
ApolloClient,
InMemoryCache,
ApolloProvider,
ApolloLink,
from,
HttpLink
} from '@apollo/client';
import {onError} from '@apollo/client/link/error';
import OfflineNotice from './src/components/OfflineNotice';
import {removeToken} from './utils/storage-helpers';
import {getValue} from './utils/lodash';
export default function App() {
const netInfo = useNetInfo();
const navigation = useNavigation();
/* -------------------------------------------------------------------------- */
/* Log Section */
/* -------------------------------------------------------------------------- */
LogBox.ignoreLogs(['Setting a timer for a long period of time']);
LogBox.ignoreLogs(['NativeBase: The contrast ratio of']);
LogBox.ignoreAllLogs(true);
LogBox.ignoreLogs([
"[react-native-gesture-handler] Seems like you're using an old API with gesture components, check out new Gestures system!",
]);
const AuthContext = React.createContext();
/* -------------------------------------------------------------------------- */
/* UseEffect Section */
/* -------------------------------------------------------------------------- */
useEffect(() => {
getData();
}, []);
const getData = async () => {
const theme = await colorModeManager.get();
setIsThemeDark(theme === 'dark' ? true : false);
async function prepare() {
try {
// Pre-load fonts, make any API calls you need to do here
await initialiseData();
} catch (e) {
// console.warn(e);
} finally {
// Tell the application to render
setAppIsReady(true);
}
}
prepare();
};
/* -------------------------------------------------------------------------- */
/* Usestate Section */
/* -------------------------------------------------------------------------- */
const [user, setUser] = useState(false);
const [appIsReady, setAppIsReady] = useState(false);
const colorScheme = true ? useColorScheme() : 'light';
const [isThemeDark, setIsThemeDark] = React.useState();
/* -------------------------------------------------------------------------- */
/* On change Section */
/* -------------------------------------------------------------------------- */
const toggleTheme = React.useCallback(async () => {
return setIsThemeDark(!isThemeDark);
}, [isThemeDark]);
const preferences = React.useMemo(
() => ({
toggleTheme,
isThemeDark,
}),
[toggleTheme, isThemeDark],
);
const initialiseData = async () => {
const user = await getToken('access_token');
if (user) setUser(user);
};
if (!appIsReady) {
return null;
}
/* -------------------------------------------------------------------------- */
/* Toast Section */
/* -------------------------------------------------------------------------- */
/*
1. Create the config
*/
const toastConfig = {
/*
Overwrite 'success' type,
by modifying the existing `BaseToast` component
*/
success: props => (
<BaseToast
{...props}
style={{borderLeftColor: '#22c55e'}}
contentContainerStyle={{paddingHorizontal: 15}}
text1Style={{
fontSize: 15,
fontWeight: '400',
}}
/>
),
/*
Overwrite 'error' type,
by modifying the existing `ErrorToast` component
*/
error: props => (
<ErrorToast
{...props}
style={{borderLeftColor: '#ef4444'}}
text1Style={{
fontSize: 17,
}}
text2Style={{
fontSize: 15,
}}
/>
),
/*
Or create a completely new type - `tomatoToast`,
building the layout from scratch.
I can consume any custom `props` I want.
They will be passed when calling the `show` method (see below)
*/
tomatoToast: ({text1, props}) => (
<View style={{height: 60, width: '100%', backgroundColor: 'tomato'}}>
<Text>{text1}</Text>
<Text>{props.uuid}</Text>
</View>
),
};
/* -------------------------------------------------------------------------- */
/* Graphql Section */
/* -------------------------------------------------------------------------- */
const API = new HttpLink({
uri: '',
});
const middleWareLink = new ApolloLink(async (operation, forward) => {
const authToken = await getToken('accessToken');
operation.setContext(context => ({
...context,
headers: authToken
? {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + authToken,
}
: {
'Content-Type': 'application/json',
},
}));
return forward(operation);
});
const afterWareLink = onError(
({operation, response, graphQLErrors = [], networkError = {}}) => {
const status =
networkError && networkError.statusCode
? networkError.statusCode
: null;
if (status === 401) {
removeToken('user');
removeToken('accessToken');
Toast.show({
type: 'error',
text1: 'error',
text2: 'Session Expired',
});
setTimeout(() => {
navigation.navigate('Login');
}, 500);
} else {
if (getValue(graphQLErrors, `length`, 0) > 0) {
graphQLErrors.forEach(item => {
Toast.show({
type: 'error',
text1: 'error',
text2: getValue(item, `message`, ''),
});
});
}
}
},
);
const client = new ApolloClient({
// uri: "https://flyby-gateway.herokuapp.com/",
link: from([middleWareLink, afterWareLink, API]),
cache: new InMemoryCache(),
});
return (
<SafeAreaProvider>
<NativeBaseProvider
theme={NativeBaseTheme}
colorModeManager={colorModeManager}>
<PreferencesContext.Provider value={preferences}>
<ApolloProvider client={client}>
{netInfo.type !== 'unknown' &&
netInfo.isInternetReachable === false ? (
<OfflineNotice />
) : (
<AuthStackNavigator />
)}
<Toast config={toastConfig} position="bottom" bottomOffset={20} />
</ApolloProvider>
</PreferencesContext.Provider>
</NativeBaseProvider>
</SafeAreaProvider>
);
}
const styles = StyleSheet.create({});