-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
124 lines (103 loc) · 3.48 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
import * as React from 'react';
import { StyleSheet, useColorScheme, StatusBar} from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { ThemeProvider, useTheme } from 'react-native-elements'
import { SafeAreaProvider } from 'react-native-safe-area-context';
import FlashMessage, { showMessage } from 'react-native-flash-message'
import AsyncStorage from '@react-native-community/async-storage'
import axios from 'axios'
import SplashScreen from 'react-native-splash-screen'
import { StackMenu } from './src/navigators'
import { darkTheme, lightTheme } from './src/configs/themes';
import * as colors from './src/configs/colors';
import { AuthContext } from './src/utils/contexts'
import { signIn } from './src/services/auth-service';
// set some global Axios configs
import './src/configs/api'
// customizing phone StatusBar
StatusBar.setBarStyle('light-content')
StatusBar.setBackgroundColor(colors.green)
function App() {
const isDarkMode = useColorScheme() === 'dark';
const currentTheme = isDarkMode ? darkTheme : lightTheme
const [state, dispatch] = React.useReducer((prevState, action) => {
switch(action.type)
{
case 'IS_LOADING':
return { ...prevState, isLoading: !!action.payload}
case 'UPDATE_USER':
return { ...prevState, user: action.payload}
case 'IS_LOGGED_IN':
return { ...prevState, isLoggedIn: !!action.payload}
}
}, {
user: {},
isLoggedIn: false,
isLoading: false,
})
const authContext = React.useMemo(() => ({
signIn: async (email, password) => {
try
{
dispatch({type: 'IS_LOADING', payload: true})
const res = await signIn(email, password)
const data = res.data
const user = { ...data.user, token: data.token }
dispatch({ type: 'UPDATE_USER', payload: user})
axios.defaults.headers.common['Authorization'] = `Token ${user.token}`
// save user data on storage
await AsyncStorage.setItem('@user', JSON.stringify(user))
showMessage({
message: 'Signed in successfully.',
type: 'success'
})
dispatch({ type: 'IS_LOGGED_IN', payload: true})
} catch(error) {
showMessage({
message: 'Invalid Credentials!',
type: 'danger'
})
}
dispatch({type: 'IS_LOADING', payload: false})
},
signOut: async () => {
axios.defaults.headers.common['Authorization'] = ''
dispatch({ type: 'IS_LOGGED_IN', payload: false })
dispatch({ type: 'UPDATE_USER', payload: {} })
},
}), [])
React.useEffect(() => {
SplashScreen.hide()
}, [])
React.useEffect(() => {
(async () => {
try
{
const user = JSON.parse(await AsyncStorage.getItem('@user'))
if (user.token)
{
axios.defaults.headers.common['Authorization'] = `Token ${user.token}`
dispatch({ type: 'UPDATE_USER', payload: user})
dispatch({ type: 'IS_LOGGED_IN', payload: true})
}
} catch (err){
console.log(err)
}
})()
}, [])
return (
<SafeAreaProvider>
<ThemeProvider theme={currentTheme}>
<AuthContext.Provider value={{...authContext, ...state}} >
<NavigationContainer theme={currentTheme}>
<StackMenu />
</NavigationContainer>
</AuthContext.Provider>
<FlashMessage position="top" />
</ThemeProvider>
</SafeAreaProvider>
);
};
const styles = StyleSheet.create({
});
export default App;