-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
136 lines (115 loc) · 3.92 KB
/
App.tsx
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
import React, { useEffect, useState } from 'react';
import { View, StyleSheet, StatusBar } from 'react-native';
import * as Font from 'expo-font';
import { BASE_URL, rest } from './config';
import axios from 'axios';
import { persistor, store } from './store/store';
import { Provider, useDispatch, useSelector } from 'react-redux';
import AsyncStorage from '@react-native-community/async-storage';
import BottomNavigator from './navigation/BottomNavigator';
import AppLoading from 'expo-app-loading';
import { loginAction, addOnlineUser, fetchOnlineUsers, removeOnlineUser, updateTypingConversation } from './store';
import { Auth, ScanQR } from './screens';
import { callApi } from './utils';
import { fetchConversationsUnseen } from './store/common/actions';
import { PersistGate } from 'redux-persist/lib/integration/react';
import SecurityLevel from './screens/common/SecurityLevel';
import { TypingConversationType } from './store/typingConversations/actions';
axios.defaults.baseURL = BASE_URL;
const fetchFonts = () => {
return Font.loadAsync({
Light: require('./assets/fonts/Quicksand_Light.ttf'),
Regular: require('./assets/fonts/Quicksand_Regular.ttf'),
Bold: require('./assets/fonts/Quicksand_bold.ttf'),
});
};
const CONNECTING = 0;
const CONNECTED = 1;
const LOGGED = 2;
const App = () => {
const [fontLoaded, setFontLoaded] = useState(false);
const [init, setInit] = useState(true);
const [connectState, setConnectState] = useState(CONNECTING);
const auth = useSelector((state: { [key: string]: any }) => state.auth);
const sio = useSelector((state: { [key: string]: any }) => state.sio);
const dispatch = useDispatch();
useEffect(() => {
const tryLogin = async () => {
const userString: string | null = await AsyncStorage.getItem('user');
if (!userString) {
setInit(false);
return;
}
const user = JSON.parse(userString);
await loginAction(dispatch, { ...user, auth });
setInit(false);
};
tryLogin();
}, []);
useEffect(() => {
if (!sio) return;
sio.on?.('connect', () => {
console.log('connected');
setConnectState(CONNECTED);
});
sio.on?.('disconnect', () => {
console.log('disconnected');
});
sio.on?.('online', (data: string) => {
console.log('online: ', data);
dispatch(addOnlineUser(data));
});
sio.on?.('offline', (data: string) => {
console.log('offline: ', data);
dispatch(removeOnlineUser(data));
});
sio.on?.('typing', (data: TypingConversationType) => {
dispatch(updateTypingConversation(data));
});
}, [sio]);
if (!fontLoaded || init) {
return <AppLoading onError={() => {}} startAsync={fetchFonts} onFinish={() => setFontLoaded(true)} />;
}
if (!auth.access_token) return <Auth />;
if (connectState === CONNECTED) {
const getUnseenConversations = async () => {
const { status, data }: any = await callApi({ method: 'get', api: rest.getUnseenConversations() });
if (status) {
dispatch(fetchConversationsUnseen({ unseenPrivate: data.unseen_private, unseenGroup: data.unseen_group }));
}
};
const getOnlineUsers = async () => {
const { status, data }: any = await callApi({ method: 'get', api: rest.getOnlineUsers() });
if (status) {
dispatch(fetchOnlineUsers(data));
}
};
getUnseenConversations();
getOnlineUsers();
sio.emit('auth', auth.access_token);
setConnectState(LOGGED);
}
return (
<View style={{ flex: 1 }}>
<BottomNavigator />
<ScanQR />
<SecurityLevel />
</View>
);
};
export default function Index() {
return (
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<StatusBar backgroundColor="#111" barStyle="light-content" />
<App />
</PersistGate>
</Provider>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#f5f6f7',
},
});