-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
125 lines (115 loc) · 3.22 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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import React from 'react';
import {Provider as PaperProvider, DefaultTheme} from 'react-native-paper';
import {NavigationContainer} from '@react-navigation/native';
import {createStackNavigator} from '@react-navigation/stack';
import Root from './src/Navigation/HomeNavigator';
import {StatusBar} from 'react-native';
import AsyncStorage from '@react-native-community/async-storage';
import SignInScreen from './src/Screens/SignInScreen';
import SignUpScreen from './src/Screens/SignUpScreen';
import SplashScreen from './src/Screens/SplashScreen';
const Stack = createStackNavigator();
export const AuthContext = React.createContext();
const App = ({navigation}) => {
// AsyncStorage.clear();
const [state, dispatch] = React.useReducer(
(prevState, action) => {
switch (action.type) {
case 'RESTORE':
return {
...prevState,
user: action.user,
isLoading: false,
};
case 'SIGN_IN':
return {
...prevState,
isSignout: false,
user: action.user,
};
case 'SIGN_OUT':
return {
...prevState,
isSignout: true,
user: null,
};
}
},
{
isLoading: true,
isSignout: false,
user: null,
},
);
React.useEffect(() => {
const bootstrapAsync = async () => {
let user;
try {
user = await AsyncStorage.getItem('user');
} catch (e) {
console.log(e);
}
dispatch({type: 'RESTORE', user: JSON.parse(user)});
};
bootstrapAsync();
}, []);
const authContext = React.useMemo(
() => ({
signIn: async data => {
dispatch({type: 'SIGN_IN', user: data.user});
},
signOut: () => dispatch({type: 'SIGN_OUT'}),
signUp: async data => {
// console.log('HERE', data);
dispatch({type: 'SIGN_IN', user: data.user});
},
}),
[],
);
return (
<PaperProvider>
<AuthContext.Provider value={authContext}>
<NavigationContainer>
<StatusBar
barStyle="light-content"
backgroundColor={DefaultTheme.colors.primary}
/>
{state.isLoading ? (
<Stack.Navigator headerMode="none">
<Stack.Screen name="Splash" component={SplashScreen} />
</Stack.Navigator>
) : state.user == null ? (
<Stack.Navigator headerMode="none">
<Stack.Screen
name="SignIn"
component={SignInScreen}
options={{
title: 'Sign in',
// pop animation for logout
animationTypeForReplace: state.isSignout ? 'pop' : 'push',
}}
/>
<Stack.Screen
name="SignUp"
component={SignUpScreen}
options={{
title: 'Sign up',
}}
/>
</Stack.Navigator>
) : (
<Root />
)}
</NavigationContainer>
</AuthContext.Provider>
</PaperProvider>
);
};
export default App;