-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.js
131 lines (124 loc) · 3.66 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
import React, {useState, useEffect} from 'react';
import {SafeAreaView, StyleSheet, View,LogBox} from 'react-native';
import Navigation from './src/navigators/Navigation';
import auth from '@react-native-firebase/auth';
import SiginInNavigate from './src/navigators/SiginInNavigate';
import Register from './src/screens/Register';
import PhoneVertify from './src/screens/PhoneVertify';
import {Provider} from 'react-redux';
import {store, persistor} from './src/redux/store'
import {ActivityIndicator} from 'react-native';
import firestore from '@react-native-firebase/firestore';
import { PersistGate } from 'redux-persist/integration/react'
import {
notificationListener,
requestUserPermission,
} from './src/utils/pushnotification_helper';
import SplashScreen from 'react-native-splash-screen';
import COLORS from './src/common/Color';
import FlashMessage from 'react-native-flash-message';
import Loading from './src/components/Loading';
const App = () => {
const [initializing, setInitializing] = useState(true);
const [user, setUser] = useState();
const [profileUpdated, setProfileUpdated] = useState(false);
const [hasPhone, setHasPhone] = useState(false);
const [loading, setLoading] = useState(true);
LogBox.ignoreAllLogs()
const checkProfileUpdated = async () => {
await firestore()
.collection('Users')
.doc(auth().currentUser.uid)
.get()
.then(doc => {
if (doc.data()?.dateofbirth !== undefined) {
setLoading(false);
setProfileUpdated(true);
} else {
if (auth().currentUser.providerData[0].providerId === 'phone') {
setLoading(false);
setProfileUpdated(true);
} else {
setLoading(false);
setProfileUpdated(false);
}
}
});
};
const onAuthStateChanged =(user) =>{
setUser(user);
// console.log(user)
if(user === null){
setHasPhone(false)
setProfileUpdated(false)
setLoading(true)
}
if (initializing) setInitializing(false);
if (auth().currentUser !== null) {
if (auth().currentUser.displayName !== null) {
checkProfileUpdated();
}
else{
setLoading(false)
}
if (auth().currentUser.phoneNumber != null) {
setHasPhone(true);
}
}
// console.log(user)
// console.log(auth().currentUser.email.toString())
// console.log(profileUpdated)
}
useEffect(() => {
let isMounted = true;
SplashScreen.hide();
if(isMounted)requestUserPermission();
if(isMounted)notificationListener();
auth().onAuthStateChanged(onAuthStateChanged);
return () => {
isMounted = false
}; // unsubscribe on unmount
}, []);
if (initializing) return null;
if (!user) {
return (
<View style={{flex: 1}}>
<SiginInNavigate />
</View>
);
}
if (!hasPhone)
return (
<View style={{flex: 1}}>
<PhoneVertify setHasPhone={setHasPhone} />
</View>
);
if (loading) {
return (
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
<Loading
uri={require('./src/assets/98092-loading.json')}
title="Đang tải dữ liệu..."
/>
</View>
);
}
if (!profileUpdated)
return (
<View style={{flex: 1}}>
<Register setProfileUpdated={setProfileUpdated} />
</View>
);
return (
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<SafeAreaView style={{flex: 1}}>
<Navigation />
</SafeAreaView>
<FlashMessage position="top" />
</PersistGate>
</Provider>
);
};
const styles = StyleSheet.create({});
export default App;