-
Notifications
You must be signed in to change notification settings - Fork 42
/
App.js
210 lines (181 loc) · 6.99 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
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
import { Order } from '@fleetbase/sdk';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import { useFleetbase } from 'hooks';
import type { Node } from 'react';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { ActivityIndicator, Linking, Text, View } from 'react-native';
import 'react-native-gesture-handler';
import 'react-native-get-random-values';
import Toast from 'react-native-toast-message';
import { EventRegister } from 'react-native-event-listeners';
import tailwind from 'tailwind';
import { useDriver } from 'utils/Auth';
import { getString, setString } from 'utils/Storage';
import { config } from './src/utils';
import { useNetInfo } from '@react-native-community/netinfo';
import CoreStack from './src/features/Core/CoreStack';
const Stack = createStackNavigator();
const linking = {
prefixes: ['https://fleetbase.io', 'flbnavigator://', config('APP_LINK_PREFIX'), ...config('app.linkingPrefixes')].filter(Boolean),
config: {
screens: {},
},
};
const success = [];
const { emit } = EventRegister;
const App: () => Node = () => {
const [setDriver] = useDriver();
const navigationRef = useRef();
const [isLoading, setLoading] = useState(true);
const fleetbase = useFleetbase();
const { isConnected } = useNetInfo();
useEffect(() => {
const orders = JSON.parse(getString('apiRequestQueue'));
if (!isConnected || orders?.length == 0) {
return;
}
if (orders?.length > 0) {
Toast.show({
type: 'success',
text1: `Activity syncing...`,
});
}
orders?.forEach((item, index) => {
console.log('Order: ', item?.order.attributes.tracking_number.status, item?.order.attributes.id, item.action);
const orderService = new Order(item?.order.attributes, fleetbase.getAdapter());
if (item.action == 'start') {
startOrder(orderService, index);
} else if (item.action == 'updated') {
updateOrder(orderService, index);
}
});
if (success) {
emit('order');
}
success.forEach(item => {
orders.splice(item);
});
setString('apiRequestQueue', JSON.stringify(orders));
}, [isConnected]);
const updateOrder = (order, index) => {
order
.updateActivity({ skipDispatch: true })
.then(res => {
Toast.show({
type: 'success',
text1: `Activity synced.`,
});
success.push(index);
console.log('Order updated------->', res);
})
.catch(err => {
console.error('Order update error------->', err);
});
};
const startOrder = (order, index) => {
order
.start({ skipDispatch: true })
.then(res => {
Toast.show({
type: 'success',
text1: `Activity synced.`,
});
success.push(index);
console.log('Sync sucess: ', res);
})
.catch(error => {
console.log('attributes error----->', error);
if (error.message.includes('already started')) {
success.push(index);
}
});
};
const parseDeepLinkUrl = useCallback(url => {
const urlParts = url.split('?');
if (urlParts.length > 1) {
const path = String(urlParts[0]).replace(/^[^:]+:\/\//, '');
if (path !== 'configure') return;
const queryString = urlParts[1];
const queryParams = queryString.split('&');
const parsedParams = {};
queryParams.forEach(param => {
const [key, value] = param.split('=');
parsedParams[key] = decodeURIComponent(value);
});
return parsedParams;
}
return null;
});
const setFleetbaseConfig = useCallback(async (key, host, socketcluster_host, socketcluster_port) => {
return await new Promise(() => {
setString('_FLEETBASE_KEY', key);
setString('_FLEETBASE_HOST', host);
setString('_SOCKET_HOST', socketcluster_host);
setString('_SOCKET_PORT', socketcluster_port);
if (navigationRef.current) {
navigationRef.current.reset({
index: 0,
routes: [{ name: 'BootScreen' }],
});
setDriver(null);
setLoading(false);
}
});
});
useEffect(() => {
const setupInstanceLink = ({ url }) => {
console.log('setupInstanceLink() #url', url);
const parsedParams = parseDeepLinkUrl(url);
if (parsedParams !== null) {
const { key, host, socketcluster_host, socketcluster_port } = parsedParams;
setFleetbaseConfig(key, host, socketcluster_host, socketcluster_port);
fleetbase.organizations.current().then(res => {
setString('_BRANDING_LOGO', res.branding.logo_url);
setString('_LOGO', res.logo_url);
console.log('Organization: ', res);
});
Toast.show({
type: 'success',
text1: `Linking to instance ${host}`,
text2: 'Navigator app linked successfully',
});
}
};
Linking.addEventListener('url', ({ url }) => {
console.log('URL EVENT FIRED!', url);
setupInstanceLink({ url });
});
Linking.getInitialURL().then(url => {
if (url) {
setupInstanceLink({ url });
console.log('initial url:::', url);
}
});
return () => {
console.log('App useEffect cleaned up');
// Linking.removeEventListener('url', setupInstanceLink);
};
}, []);
return (
<>
<NavigationContainer
ref={navigationRef}
linking={linking}
fallback={
<View style={tailwind('bg-gray-800 flex items-center justify-center w-full h-full')}>
<View style={tailwind('flex items-center justify-center')}>
<ActivityIndicator style={tailwind('mb-4')} />
<Text style={tailwind('text-gray-400')}>Loading...</Text>
</View>
</View>
}>
<Stack.Navigator>
<Stack.Screen name="CoreStack" component={CoreStack} options={{ headerShown: false, animationEnabled: false, gestureEnabled: false }} />
</Stack.Navigator>
</NavigationContainer>
<Toast />
</>
);
};
export default App;