forked from Sermanbilotil/onLife
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.js
More file actions
84 lines (72 loc) · 2.47 KB
/
App.js
File metadata and controls
84 lines (72 loc) · 2.47 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
import 'react-native-gesture-handler';
import 'react-native-get-random-values';
import React, {Node, useCallback, useEffect, useRef} from 'react'
import {NavigationContainer} from '@react-navigation/native';
import {Provider, useDispatch} from 'react-redux'
import {Alert, AppState} from 'react-native';
import {store, persistor} from './src/redux/store';
import {PersistGate} from 'redux-persist/integration/react';
import AppNavigator from './src/navigation/AppNavigator';
import AuthNavigator from './src/navigation/AuthNavigator';
import {useSelector} from 'react-redux';
import {navigationRef} from './src/navigation/RootNavigation';
import messaging from '@react-native-firebase/messaging';
import { notificationMessage, setPushToken, syncEverything, syncTrainingPrograms } from './src/redux/action-creators'
messaging().setBackgroundMessageHandler(async remoteMessage => {
if (store) {
store.dispatch(notificationMessage(remoteMessage.data));
}
});
const NavigationComponent = () => {
const {token} = useSelector(state => state.auth);
const dispatch = useDispatch();
const appState = useRef(AppState.currentState);
const handleAppState = useCallback(
nextAppState => {
if (
appState.current.match(/inactive|background/) &&
nextAppState === 'active'
) {
dispatch(syncEverything());
}
appState.current = nextAppState;
},
[appState, dispatch],
);
useEffect(() => {
AppState.addEventListener('change', handleAppState);
return () => AppState.removeEventListener('change', handleAppState);
}, [handleAppState]);
useEffect(() => {
dispatch(syncTrainingPrograms());
}, [dispatch]);
useEffect(() => {
const unsubscribe = messaging().onMessage(async remoteMessage => {
// Alert.alert('A new FCM message arrived!', JSON.stringify(remoteMessage));
dispatch(notificationMessage(remoteMessage.data));
});
messaging()
.getToken()
.then(token => {
dispatch(setPushToken(token));
});
return unsubscribe;
}, [dispatch]);
if (token) {
return <AppNavigator />;
} else {
return <AuthNavigator setIsLogin={false} />;
}
};
const App: () => Node = () => {
return (
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<NavigationContainer initialRouteName={'AuthStack'} ref={navigationRef}>
<NavigationComponent />
</NavigationContainer>
</PersistGate>
</Provider>
);
};
export default App;