-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
104 lines (90 loc) · 4.13 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
import React, { useEffect, useRef, useState } from "react";
import { View, ActivityIndicator } from "react-native";
import SyncStorage from "sync-storage";
import * as Notifications from "expo-notifications";
import { NavigationContainer } from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
import Icon from "react-native-vector-icons/FontAwesome";
import FlashMessage from "react-native-flash-message";
import Toast from 'react-native-toast-message';
import { showMessage } from "react-native-flash-message";
import SignIn from "./components/SignIn";
import SignUp from "./components/SignUp";
import DrawerMenu from "./components/delivery/DrawerMenu";
import Edit from "./components/Edit";
import Tabs from "./components/Tabs";
import Home from "./components/Home";
import RestaurantInfos from "./components/RestaurantInfos"
import BasketInfos from "./components/BasketInfos";
import Invoice from "./components/Invoice";
import Favorites from "./components/Favorites";
import { store } from "./service";
import { connect } from "react-redux"
import { getLocales } from "react-native-localize";
import { translate } from "./languages";
const Stack = createStackNavigator();
Icon.loadFont()
const App = ({ dispatch }) => {
const navigationRef = useRef()
const [StorageLoaded, setStorageLoaded] = useState(false)
useEffect(() => {
(async () => {
try {
await SyncStorage.init()
var countryCode = getLocales()[0].languageTag.split("-")[0] //ex: en, fr...
store.setCountryCode(countryCode)
translate.changeLanguage(store.getCountryCode())
setStorageLoaded(true)
dispatch({ type: "SET_NOTIF", value: store.getNbNotifications() })
}
catch (err) {
console.log(err)
}
})()
// in the app
Notifications.addNotificationReceivedListener(handleOnReceivedNotification);
// outside the app
Notifications.addNotificationResponseReceivedListener(handleOnPressNotifications);
}, []);
// When the user recieved a notification inside the app
const handleOnReceivedNotification = notification => {
store.setNbNotifications(store.getNbNotifications() + 1)
dispatch({ type: "SET_NOTIF", value: store.getNbNotifications() })
var notif = notification.request.content
showMessage({
backgroundColor: "#ffc107",
floating: true,
duration: 6000,
message: notif.title,
description: notif.body,
onPress: () => navigationRef.current.navigate("Tabs", { screen: "Orders" })
});
};
// When the user press on the notification outside the app
const handleOnPressNotifications = async (response) => {
navigationRef.current.navigate("Tabs")
}
return (
StorageLoaded
? <NavigationContainer ref={navigationRef}>
<Stack.Navigator screenOptions={{ headerShown: false }} initialRouteName={store.isLogged() ? "DrawerMenu" : "SignIn"}>
<Stack.Screen name="SignIn" component={SignIn} />
<Stack.Screen name="SignUp" component={SignUp} />
<Stack.Screen name="DrawerMenu" component={DrawerMenu} />
<Stack.Screen name="Edit" component={Edit} />
<Stack.Screen name="Tabs" component={Tabs} />
<Stack.Screen name="Home" component={Home} />
<Stack.Screen name="RestaurantInfos" component={RestaurantInfos} />
<Stack.Screen name="BasketInfos" component={BasketInfos} />
<Stack.Screen name="Invoice" component={Invoice} />
<Stack.Screen name="Favorites" component={Favorites} />
</Stack.Navigator>
<FlashMessage position="top" />
<Toast ref={(ref) => Toast.setRef(ref)} />
</NavigationContainer>
: <View style={{ flex: 1, justifyContent: "center" }}>
<ActivityIndicator />
</View>
);
}
export default connect()(App)