-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
213 lines (149 loc) · 5.29 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { StatusBar } from 'expo-status-bar';
import { useEffect, useState } from 'react';
import { ActivityIndicator, Image, SafeAreaView, StyleSheet, Text, TouchableOpacity, View } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import AppNavigation from './Routes/AppNavigation';
import AuthNavigation from './Routes/AuthNavigation';
import { useFonts } from 'expo-font';
import Axios from 'axios';
import AppContext from './context/appContext';
import * as SecureStore from 'expo-secure-store';
// import * as Location from 'expo-location';
import Logo from './assets/images/agrico.png';
import {BASEURL} from '@env'
import { ReconnectComponent } from './Components/ReconnectComponent';
export default function App() {
const [User, setUser] = useState();
const [isReady, setIsReady] = useState(false);
const [Error, setError] = useState(false);
const [User_cart, setUser_cart] = useState();
const [UserShop, setUserShop] = useState();
const [ProductsToDisplay,setProductsToDisplay] = useState()
const [GenRefresh,setGenRefresh] = useState(false)
const [ UserNotifications, setUserNotifications ] = useState()
// get User Notification
const GetUserNotification = () => {
Axios.get('/notification/')
.then( (response) => setUserNotifications(response.data) )
.catch( err => {
// console.log(err)
} )
}
// to check if the person is loggedin In the firstPlace
const HandleIfUserIsTrullyAuthenticated = async () => {
let token = await SecureStore.getItemAsync("authToken");
if (token) {
Axios.defaults.headers.common['token'] = 'Bearer ' + token
Axios.get('/returnUser/get_user').then(response => {
const { cart, ...others } = response.data
setUser({ ...others })
setUser_cart(cart)
GetUserNotification()
setIsReady(true)
setError(false)
}).catch((error) => {
setIsReady(true)
setError(true)
setUser()
setUser_cart()
})
} else {
setIsReady(true)
setError(false)
setUser()
setUser_cart()
setUserShop()
}
}
useEffect( () => {
HandleIfUserIsTrullyAuthenticated()
},[] )
const ApiBaseUrl = "https://agrico-backend-production.up.railway.app/"
const BaseUrl = ApiBaseUrl;
//
Axios.defaults.baseURL = ApiBaseUrl;
// Login Process
async function saveToken(key, value, user_details, cart) {
await SecureStore.setItemAsync(key, value);
Axios.defaults.headers.common['token'] = 'Bearer ' + value
GetUserNotification()
setUser(user_details)
setUser_cart(cart)
}
// Log out function
const LogoutProcess = async () => {
await SecureStore.setItemAsync("authToken", "");
Axios.defaults.headers.common['token'] = ''
setUser()
setUser_cart()
setUserNotifications()
}
const HandlesetUser_Details = (details) => {
setUser(details)
}
const HandleUpdateUserCart = (cart_details) => {
setUser_cart(cart_details)
}
// Update The Product List
const UpdateProductLIst = (products) => {
setProductsToDisplay(products)
}
const [TitilliumWeb_Regular] = useFonts({
'TitilliumWeb-Regular': require('./assets/fonts/titi/TitilliumWeb-Regular.ttf'),
'TitilliumWeb-SemiBold': require('./assets/fonts/titi/TitilliumWeb-SemiBold.ttf'),
});
const [TitilliumWeb_Bold] = useFonts({
'TitilliumWeb-Bold': require('./assets/fonts/titi/TitilliumWeb-Bold.ttf'),
});
if (!TitilliumWeb_Regular || !TitilliumWeb_Bold ) {
return null;
}
if ( !isReady ){
return <View style={{
flex:1,
backgroundColor:"white",
justifyContent:"center",
alignItems:"center"
}} >
<Image source={Logo} style={{
width:240,
height:240
}} />
</View>
}
if ( isReady && !Error ) {
return (
<AppContext.Provider value={{
BaseUrl: BaseUrl,
User_details: User,
LogOut: LogoutProcess,
LogIn: saveToken,
UpdateUser_Cart: HandleUpdateUserCart,
User_Cart: User_cart,
setUser_details: HandlesetUser_Details,
DisplayProducts:ProductsToDisplay,
UpdateDisplayProducts:UpdateProductLIst,
UserShop:UserShop,
setUserShop: (data) => setUserShop(data),
GenRefresh:GenRefresh,
setGenRefresh:() => setGenRefresh(!GenRefresh),
User_Notifications: UserNotifications,
setUser_Notifications: (data) => setUserNotifications(data)
}} >
<NavigationContainer>
<StatusBar backgroundColor={"gray"} />
<SafeAreaView style={{
flex:1
}} >
{ !isReady ? <ActivityIndicator color={"green"} /> :
User ? <AppNavigation/> : <AuthNavigation/>
}
</SafeAreaView>
</NavigationContainer>
</AppContext.Provider>
);
}
if ( isReady && Error ) {
return <ReconnectComponent retryFunction={ () => HandleIfUserIsTrullyAuthenticated() } />
}
}