-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApp.tsx
93 lines (81 loc) · 2.46 KB
/
App.tsx
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
import * as React from 'react';
import { useState, useEffect } from 'react';
import axios from 'axios';
import { UserContext } from './components/Context';
import * as SecureStore from 'expo-secure-store';
import AsyncStorage from '@react-native-async-storage/async-storage';
import AppNavigator from './AppNavigator';
import {UserContextType, User} from './lib/types';
import { DOMAIN_URL } from './lib/constants';
export default function App() {
const [loggedIn, setLoggedIn] = useState(false);
const [userData, setUserData] = useState<User>({});
const login = (user?: User) => {
if (user){
setUserData(user);
if (user.id){
setLoggedIn(true);
}
}
};
const logout = () => {
setLoggedIn(false);
setUserData({});
};
const userContext: UserContextType = {
isLoggedIn: loggedIn,
user: userData,
login: login,
logout: logout
};
useEffect(() => {
async function fetchUserData() {
const headers = { authorization: `Bearer ${await SecureStore.getItemAsync('token')}` };
const { data } = await axios.get(`${DOMAIN_URL}/api/getselfdetail`, { headers: headers });
const {token, ...others} = data;
const userData = {...others, logintime: Math.round(new Date().getTime() / 1000)};
setUserData(userData);
await AsyncStorage.setItem('user', JSON.stringify(userData));
await SecureStore.setItemAsync('token', token);
}
async function fetchData(){
const user = await AsyncStorage.getItem('user');
const userData = user ? JSON.parse(user): {};
setUserData(userData);
if (userData.id){
setLoggedIn(true);
const logintime = userData.logintime || 0;
const currTime = Math.round(new Date().getTime() / 1000);
if (currTime > (logintime + 60 * 60 * 24 * 10)){
fetchUserData();
}
}
}
fetchData();
},[]);
return (
<UserContext.Provider value={userContext}>
<AppNavigator />
</UserContext.Provider>
);
}
/*
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, View } from 'react-native';
export default function App() {
return (
<View style={styles.container}>
<Text>Open up App.tsx to start working on your app!</Text>
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
*/