-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
77 lines (68 loc) · 2.33 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
import React, { useState, useEffect } from 'react';
import { View, Text, Alert } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import auth from '@react-native-firebase/auth';
import messaging from '@react-native-firebase/messaging';
import firestore from '@react-native-firebase/firestore';
import {UserContext} from './Components/UserContext';
import Header from './Components/Header';
import Home from './Components/Home';
import Login from './Components/Login';
import Register from './Components/Register';
import Order from './Components/Order';
const App = () => {
const Stack = createStackNavigator();
const [initializing, setInitializing] = useState(true);
const [user, setUser] = useState();
const [token, setToken] = useState();
useEffect(() => {
const subscriber = auth().onAuthStateChanged(onAuthStateChanged);
return subscriber;
}, []);
useEffect(() => {
if(token && user){
firestore().collection('users').doc(user?.uid).update({
deviceToken: token
})
}
}, [token, user]);
useEffect(() => {
const fetchToken = async () => {
const aquiredToken = await messaging().getToken()
if (aquiredToken){
setToken(aquiredToken);
}
}
fetchToken()
const unsubscribe = messaging().onMessage(async e => {
Alert.alert(e.notification.title, e.notification.body);
});
return unsubscribe;
}, []);
function onAuthStateChanged(user) {
setUser(user);
if (initializing) setInitializing(false);
}
if (initializing) {
return (
<View style={{flex: 1, justifyContent: 'center', backgroundColor: '#131313'}}>
<Text style={{textAlign: 'center', color: 'white', fontSize: 17}}>Ładowanie...</Text>
</View>
);
}
return(
<UserContext.Provider value={{user}}>
<NavigationContainer>
<Header user={user}/>
<Stack.Navigator screenOptions={{ headerShown: false }}>
<Stack.Screen name="Home" component={Home} />
<Stack.Screen name="Login" component={Login} />
<Stack.Screen name="Register" component={Register} />
<Stack.Screen name="Order" component={Order} />
</Stack.Navigator>
</NavigationContainer>
</UserContext.Provider>
)
}
export default App;