-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathApp.js
More file actions
107 lines (96 loc) · 2.8 KB
/
App.js
File metadata and controls
107 lines (96 loc) · 2.8 KB
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
import React, {useEffect, useState} from 'react';
import {StyleSheet} from 'react-native';
import {NavigationContainer} from '@react-navigation/native';
import {createStackNavigator} from '@react-navigation/stack';
import AuthNavigators from './src/navigators/AuthNavigators';
import HomeNavigators from './src/navigators/HomeNavigators';
import {connect} from 'react-redux';
import NotifService from './src/services/notifications/NotifService';
import {io} from 'socket.io-client';
import {API_URL} from '@env';
import {setBalance} from './src/redux/actions/balance';
function App(props) {
const {isLogin} = props;
const [isLoggedIn, setIsLoggedIn] = useState(true);
const {Navigator, Screen} = createStackNavigator();
useEffect(() => {
if (isLogin) {
const token = props.auth.results.token;
const userId = props.auth.results.id;
const notif = new NotifService();
const socket = io(API_URL, {
// timeout: 5000,
autoConnect: false,
reconnectionDelay: 10000,
query: {
token: `Bearer ${token}`,
},
});
socket.connect();
socket.on('connect', () => {
socket.emit('join', `notification:${userId}`);
});
socket.on('notification', notification => {
notif.localNotif(
notification.title || 'New Notification!',
notification.content,
);
});
socket.on('new-balance', balance => {
props.onSetBalance(balance);
});
socket.on('connect_error', err => {
console.log(err.message); // prints the message associated with the error
});
return () => socket.disconnect();
}
}, [isLogin]);
return (
<NavigationContainer style={styles.navigationContainer}>
<Navigator headerMode={'none'}>
{/* Auth Screen */}
{!isLogin ? (
<>
<Screen
name="Auth"
children={() => (
<AuthNavigators
setIsLoggedIn={choice => setIsLoggedIn(choice)}
/>
)}
/>
</>
) : (
<>
<Screen
name="Home"
children={() => (
<HomeNavigators
setIsLoggedIn={choice => setIsLoggedIn(choice)}
/>
)}
/>
</>
)}
</Navigator>
</NavigationContainer>
);
}
const styles = StyleSheet.create({
navigationContainer: {
backgroundColor: '#E5E5E5',
},
});
const mapStateToProps = state => {
return {
isLogin: state.auth.isLogin,
auth: state.auth,
balance: state.balance,
};
};
const mapDispatchToProps = dispatch => {
return {
onSetBalance: value => dispatch(setBalance(value)),
};
};
export default connect(mapStateToProps, mapDispatchToProps)(App);