-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
340 lines (312 loc) · 9.49 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import React, {Component, useEffect, useState} from 'react';
import RNBootSplash from 'react-native-bootsplash';
import Screen from './Components/Screen';
import Screen2 from './Components/Screen2';
import {StyleProvider, Root} from 'native-base';
import getTheme from './native-base-theme/components';
import material from './native-base-theme/variables/material';
//Redux dependencies
import {Provider} from 'react-redux';
import {createStore, combineReducers, applyMiddleware} from 'redux';
import thunk from 'redux-thunk';
import {userDataReducer} from './Redux/Reducers/Reducer';
import axios from 'axios';
import {Toast} from 'native-base';
import {createDrawerNavigator} from '@react-navigation/drawer';
import {NavigationContainer} from '@react-navigation/native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import {StyleSheet, View, ActivityIndicator, Image} from 'react-native';
import {
Container,
Text,
Button,
Label,
Item as FormItem,
Input,
} from 'native-base';
import {useForm, Controller} from 'react-hook-form';
import Spinner from 'react-native-loading-spinner-overlay';
import SideBar from './Partials/SideBar';
import {HeaderBackButton} from '@react-navigation/stack';
import {createStackNavigator} from '@react-navigation/stack';
import COLORS from './Colors';
function CustomDrawerContent(props) {
return <SideBar {...props} />;
}
const AuthContext = React.createContext();
const store = createStore(
combineReducers({
userdata: userDataReducer,
}),
applyMiddleware(thunk),
);
function LoginComponent({navigation}) {
const [loading, setLoading] = useState(false);
const {control, handleSubmit, errors} = useForm();
const {signIn} = React.useContext(AuthContext);
const onSubmit = data => {
signIn(data.email, data.password);
};
return (
<Container style={styles.content}>
<View style={styles.MainContainer}>
<Spinner
visible={loading}
customIndicator={<ActivityIndicator size="large" />}
/>
<Image
style={{width: 250, height: 260, marginBottom: 20, marginTop: 10}}
source={require('./Assets/login.png')}
/>
<Text
style={{
fontSize: 23,
textAlign: 'center',
fontWeight: 'bold',
marginBottom: 10,
color: COLORS.blue,
}}>
{' '}
Iniciar Sesión
</Text>
<Controller
control={control}
rules={{required: true}}
render={({onChange, onBlur, value}) => (
<FormItem floatingLabel>
<Label>Usuario</Label>
<Input onChangeText={value => onChange(value)} value={value} />
</FormItem>
)}
name="email"
defaultValue=""
/>
{errors.email && (
<Text style={styles.error}>El campo email es requerido.</Text>
)}
<Controller
control={control}
rules={{required: true}}
render={({onChange, onBlur, value}) => (
<FormItem floatingLabel>
<Label>Contraseña</Label>
<Input onChangeText={value => onChange(value)} value={value} />
</FormItem>
)}
name="password"
defaultValue=""
/>
{errors.password && (
<Text style={styles.error}>El campo contraseña es requerido.</Text>
)}
<Button
style={{marginTop: 30, width: '100%'}}
block
bordered
onPress={handleSubmit(onSubmit)}
//onPress={() => onFormSubmit()}
>
<Image
style={{width: 25, height: 25}}
source={require('./Assets/mail.png')}
/>
<Text style={{color: '#ff5e00', fontSize: 20, fontWeight: 'bold'}}>
Login
</Text>
</Button>
</View>
</Container>
);
}
const styles = StyleSheet.create({
MainContainer: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#fff',
},
content: {
paddingLeft: 40,
paddingRight: 40,
},
error: {
textAlign: 'left',
justifyContent: 'flex-start',
color: '#bf1650',
},
});
const Drawer = createDrawerNavigator();
function App() {
//login token
const [state, dispatch] = React.useReducer(
(prevState, action) => {
switch (action.type) {
case 'RESTORE_TOKEN':
return {
...prevState,
userToken: action.token,
isLoading: false,
};
case 'SIGN_IN':
return {
...prevState,
isSignout: false,
userToken: action.token,
};
case 'SIGN_OUT':
return {
...prevState,
isSignout: true,
userToken: null,
};
}
},
{
isLoading: true,
isSignout: false,
userToken: null,
},
);
React.useEffect(() => {
// Fetch the token from storage then navigate to our appropriate place
const bootstrapAsync = async () => {
let userToken;
try {
userToken = await AsyncStorage.getItem('token');
} catch (e) {
// Restoring token failed
}
console.log('Token de usuario encontrado');
console.log(userToken);
// After restoring token, we may need to validate it in production apps
// This will switch to the App screen or Auth screen and this loading
// screen will be unmounted and thrown away.
dispatch({type: 'RESTORE_TOKEN', token: userToken});
};
bootstrapAsync();
}, []);
const authContext = React.useMemo(
() => ({
signIn: async (email, password) => {
// In a production app, we need to send some data (usually username, password) to server and get a token
// We will also need to handle errors if sign in failed
// After getting token, we need to persist the token using `AsyncStorage`
// In the example, we'll use a dummy token
console.log(email);
await axios
.post('https://developer.e-hop.mx/api/login', {
email: email,
password: password,
})
.then(async res => {
if (res.error) {
throw res;
}
console.log(res.data);
try {
await AsyncStorage.setItem('token', res.data.token);
console.log('saved token');
} catch (e) {
console.log(e);
// saving error
}
dispatch({type: 'SIGN_IN', token: res.data.token});
return res;
})
.catch(error => {
console.log(error);
Toast.show({
text: error.message,
buttonText: 'Ok',
duration: 5000,
});
});
},
signOut: () => dispatch({type: 'SIGN_OUT'}),
signUp: async data => {
// In a production app, we need to send user data to server and get a token
// We will also need to handle errors if sign up failed
// After getting token, we need to persist the token using `AsyncStorage`
// In the example, we'll use a dummy token
dispatch({type: 'SIGN_IN', token: 'dummy-auth-token'});
},
}),
[],
);
useEffect(() => {
const init = async () => {
// …do multiple sync or async tasks
};
init().finally(async () => {
await RNBootSplash.hide({fade: true});
console.log('Bootsplash has been hidden successfully');
});
}, []);
function SplashScreen() {
return (
<View style={styles.MainContainer}>
<Spinner
visible={true}
customIndicator={<ActivityIndicator size="large" />}
/>
</View>
);
}
const Stack = createStackNavigator();
function RootScreen({navigation}) {
return (
<Stack.Navigator>
<Stack.Screen
name="RootScreen"
component={Screen}
options={{
headerLeft: props => (
<HeaderBackButton
{...props}
onPress={() => {
navigation.navigate('Screen2');
}}
/>
),
title: 'My home',
headerStyle: {
backgroundColor: COLORS.blue,
},
headerTintColor: '#fff',
headerTitleStyle: {
fontWeight: 'bold',
},
}}
/>
<Stack.Screen name="Screen2" component={Screen2} />
</Stack.Navigator>
);
}
return (
<StyleProvider style={getTheme(material)}>
<Provider store={store}>
<Root>
<AuthContext.Provider value={authContext}>
<NavigationContainer>
<Drawer.Navigator
initialRouteName="Home"
drawerContent={props => <CustomDrawerContent {...props} />}>
{state.isLoading ? (
// We haven't finished checking for the token yet
<Drawer.Screen name="Splash" component={SplashScreen} />
) : state.userToken == null ? (
// No token found, user isn't signed in
<Drawer.Screen name="Login" component={LoginComponent} />
) : (
// User is signed in
<Drawer.Screen name="Home" component={RootScreen} />
)}
</Drawer.Navigator>
</NavigationContainer>
</AuthContext.Provider>
</Root>
</Provider>
</StyleProvider>
);
}
export default App;