-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.js
122 lines (110 loc) · 3.49 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
/* eslint-disable */
import 'react-native-gesture-handler';
import {NavigationContainer} from '@react-navigation/native';
import React, {useRef, useEffect, useState, useContext} from 'react';
import {Provider as PaperProvider} from 'react-native-paper';
import {WithSplashScreen} from './components/Splash';
import {RootNavigator} from './navigation/rootNavigator';
import {io} from 'socket.io-client';
import Geolocation from '@react-native-community/geolocation';
import {HOST} from './host';
import AsyncStorage from '@react-native-async-storage/async-storage';
import {LoginContext} from './Context/LoginContext';
export default function App() {
const [isAppReady, setIsAppReady] = useState(false);
const [currentLocation, setCurrentLocation] = useState({
latitude: 0,
longitude: 0,
});
const [cookie, setCookie] = useState({});
const [socket, setSocket] = useState(null);
const [wait, setWait] = useState(false);
const [loggedIn, setLoggedIn] = useState(false);
const [locations, setLocations] = useState([]);
setTimeout(() => {
setWait(true);
}, 10000);
async function cookie_set() {
let tempCookie = await AsyncStorage.getItem('rider_cookie');
if (tempCookie != JSON.stringify(cookie)) {
console.log(JSON.parse(tempCookie));
console.log({cookie});
setCookie(JSON.parse(tempCookie));
}
}
useEffect(() => {
cookie_set();
if (cookie != {} && cookie != null && cookie.jwt != null) {
setSocket(
io(`http://${HOST}`, {
withCredentials: false,
extraHeaders: {
Cookie: `${cookie.jwt.name}=${cookie.jwt.value}`,
},
}),
);
console.log('socket created');
}
console.log(loggedIn);
}, [cookie, loggedIn]);
useEffect(() => {
setIsAppReady(true);
Geolocation.getCurrentPosition(
position => {
setCurrentLocation({
latitude: position.coords.latitude,
longitude: position.coords.longitude,
});
},
error => console.log(error),
{enableHighAccuracy: false, timeout: 30000, maximumAge: 3600000},
);
}, [wait]);
useEffect(() => {
const interval = setInterval(() => {
Geolocation.getCurrentPosition(
position => {
setCurrentLocation({
latitude: position.coords.latitude,
longitude: position.coords.longitude,
});
},
error => console.log(error),
{enableHighAccuracy: false, timeout: 30000, maximumAge: 3600000},
);
}, 5000);
return () => clearInterval(interval);
}, [cookie]);
useEffect(() => {
if (currentLocation && socket) {
if (cookie != {} && cookie != null && cookie.jwt != null) {
// console.log(`${cookie.jwt.name}=${cookie.jwt.value}`);
console.log('emitting');
socket.emit(
'rider:move',
currentLocation.latitude,
currentLocation.longitude,
);
socket.on('pickup:get', data => {
setLocations(data);
});
// console.log('emitted');
} else {
console.log('not emitted');
}
}
}, [currentLocation, loggedIn]);
return (
<WithSplashScreen isAppReady={isAppReady}>
<PaperProvider>
<LoginContext.Provider
value={{loggedIn, setLoggedIn, locations, setLocations}}>
<NavigationContainer>
<RootNavigator />
</NavigationContainer>
</LoginContext.Provider>
{/* <Login/> */}
</PaperProvider>
</WithSplashScreen>
);
}