Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.04 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import {
  3. Dimensions, AsyncStorage, View, Image, TouchableHighlight, Text, Button,
  4. ActivityIndicator, Keyboard, Alert
  5. } from 'react-native';
  6. import { TextField } from 'react-native-material-textfield';
  7. import { FontAwesome } from '@expo/vector-icons';
  8. import { connect } from 'react-redux';
  9. import { NavigationActions } from 'react-navigation';
  10.  
  11. import { ScreenOrientation } from 'expo';
  12.  
  13. import { AppConstants } from "../app-utils.js";
  14.  
  15. import Files from '../Files';
  16.  
  17. const { width, height} = Dimensions.get('window');
  18.  
  19. const SCREEN_WIDTH = width > height ? width : height;
  20.  
  21. const isSmallDevice = SCREEN_WIDTH <= 414;
  22.  
  23. import {
  24. login,
  25. updateFieldError,
  26. updateFieldLoginSuccess,
  27. } from '../actions/LoginActions';
  28.  
  29.  
  30. class Login extends Component {
  31.  
  32. static navigationOptions = () => ({
  33. header: null
  34. });
  35.  
  36.  
  37.  
  38. constructor(props) {
  39. super(props);
  40.  
  41. this.onFocus = this.onFocus.bind(this);
  42. this._login = this._login.bind(this);
  43. this.onChangeText = this.onChangeText.bind(this);
  44. this.onSubmitEmail = this.onSubmitEmail.bind(this);
  45. this.onSubmitPassword = this.onSubmitPassword.bind(this);
  46. this.onAccessoryPress = this.onAccessoryPress.bind(this);
  47.  
  48. this.emailRef = this.updateRef.bind(this, 'email');
  49. this.passwordRef = this.updateRef.bind(this, 'password');
  50.  
  51. this.renderPasswordAccessory = this.renderPasswordAccessory.bind(this);
  52.  
  53. this.state = {
  54. email: '',
  55. password: '',
  56. errors: {},
  57. secureTextEntry: true,
  58. }
  59. }
  60.  
  61. onFocus() {
  62. let { errors = {} } = this.props;
  63.  
  64. for (let name in errors) {
  65. let ref = this[name];
  66.  
  67. if (ref && ref.isFocused()) {
  68. delete errors[name];
  69. }
  70. }
  71.  
  72. this.setState({ errors });
  73. }
  74.  
  75. onChangeText(text) {
  76. ['email', 'password']
  77. .map((name) => ({ name, ref: this[name] }))
  78. .forEach(({ name, ref }) => {
  79. if (ref.isFocused()) {
  80. this.setState({ [name]: text });
  81. }
  82. });
  83. }
  84.  
  85. onSubmitEmail() {
  86. this.password.focus();
  87. }
  88.  
  89. onSubmitPassword() {
  90. this.password.blur();
  91. }
  92.  
  93. _navigateLoginNative() {
  94. const resetAction = NavigationActions.reset({
  95. index: 0,
  96. actions: [
  97. NavigationActions.navigate({ routeName: 'LoginNative' })
  98. ]
  99. })
  100. this.props.navigation.dispatch(resetAction);
  101. }
  102.  
  103. _login() {
  104. Keyboard.dismiss();
  105. let errors = {};
  106. ['email', 'password']
  107. .forEach((name) => {
  108. let value = this[name].value();
  109. if (!value) {
  110. if (name == 'email')
  111. errors[name] = 'Email obrigatório!';
  112. else
  113. errors[name] = 'Senha obrigatória!';
  114. }
  115. });
  116. this.setState({ errors });
  117. if (Object.keys(errors).length == 0) {
  118. let { navigate } = this.props.navigation;
  119. let { email, password } = this.state;
  120. this.props.login({ email, password, navigate });
  121. }
  122. }
  123.  
  124. updateRef(name, ref) {
  125. this[name] = ref;
  126. }
  127.  
  128. renderBtnLogin() {
  129. if (this.props.loginInProgress) {
  130. return (
  131. <ActivityIndicator color={AppConstants.secondaryColor} size="large" />
  132. );
  133. }
  134.  
  135.  
  136. return (
  137. <View style={{ justifyContent: 'center', alignItems: 'center', marginTop: 10 }}>
  138. <TouchableHighlight
  139. style={{ justifyContent: 'center', alignItems: 'center', backgroundColor: AppConstants.secondaryColor, width: 300, height: 40 }}
  140. underlayColor={AppConstants.secondaryColor}
  141. onPress={() => this._login()}
  142. >
  143. <Text style={{ color: '#FFF', fontSize: 18, fontWeight: 'bold' }}>
  144. ENTRAR
  145. </Text>
  146. </TouchableHighlight>
  147. </View>
  148. );
  149. }
  150.  
  151. onAccessoryPress() {
  152. this.setState(({ secureTextEntry }) => ({ secureTextEntry: !secureTextEntry }));
  153. }
  154.  
  155. renderPasswordAccessory() {
  156. let { secureTextEntry } = this.state;
  157.  
  158. let name = secureTextEntry ?
  159. 'eye' :
  160. 'eye-slash';
  161.  
  162. return (
  163. <FontAwesome
  164. name={name}
  165. size={16}
  166. color={AppConstants.primaryColor}
  167. onPress={this.onAccessoryPress}
  168. suppressHighlighting
  169. disabled={this.props.loginInProgress}
  170. />
  171. );
  172. }
  173.  
  174.  
  175. componentWillUnmount() {
  176. Keyboard.dismiss();
  177. }
  178.  
  179. componentDidUpdate() {
  180. if (this.props.error) {
  181. Alert.alert(
  182. '',
  183. this.props.error
  184. )
  185. this.props.updateFieldError('');
  186. }
  187. if (this.props.loginSuccess) {
  188. this.setState({ ['password']: '' });
  189. this.props.updateFieldLoginSuccess(false);
  190. }
  191. }
  192.  
  193.  
  194. render() {
  195. let { errors = {}, secureTextEntry, ...data } = this.state;
  196. return (
  197. <View
  198. style={{
  199. flex: 1,
  200. backgroundColor: AppConstants.backgroundColor,
  201. }}
  202.  
  203. >
  204.  
  205. <View
  206. style={{
  207. position: 'absolute',
  208. top: 0,
  209. left: 0,
  210. width: '100%',
  211. height: '100%',
  212. alignItems: 'center'
  213.  
  214. }}
  215.  
  216. >
  217.  
  218. {
  219. isSmallDevice
  220. ?
  221. <Image
  222. style={{flex: 1, width: null, height: null}}
  223. source={Files.images.login_background}
  224. />
  225. :
  226. <Image
  227. style={{flex: 1, width: null, height: null}}
  228. source={Files.images.home_background}
  229. />
  230. }
  231.  
  232. </View>
  233. <View
  234. style={{
  235. flex: 1,
  236. justifyContent: 'center',
  237. marginLeft: 30,
  238. marginRight: 30
  239. }}
  240. >
  241. <TextField
  242. ref={this.emailRef}
  243. value={data.email}
  244. label='E-mail'
  245. keyboardType='email-address'
  246. iconColor={AppConstants.primaryColor}
  247. textColor={AppConstants.primaryColor}
  248. tintColor='#FFF'
  249. baseColor='#FFF'
  250. fontSize={20}
  251. labelFontSize={12}
  252. autoCapitalize='none'
  253. autoCorrect={true}
  254. enablesReturnKeyAutomatically={true}
  255. onFocus={this.onFocus}
  256. onChangeText={this.onChangeText}
  257. onSubmitEditing={this.onSubmitEmail}
  258. returnKeyType='next'
  259. error={errors.email}
  260. disabled={this.props.loginInProgress}
  261. />
  262.  
  263. <TextField
  264. ref={this.passwordRef}
  265. value={data.password}
  266. textColor={AppConstants.primaryColor}
  267. tintColor='#FFF'
  268. baseColor='#FFF'
  269. fontSize={20}
  270. labelFontSize={12}
  271. secureTextEntry={secureTextEntry}
  272. autoCapitalize='none'
  273. autoCorrect={false}
  274. enablesReturnKeyAutomatically={true}
  275. onFocus={this.onFocus}
  276. onChangeText={this.onChangeText}
  277. onSubmitEditing={this.onSubmitPassword}
  278. returnKeyType='done'
  279. label='Senha'
  280. error={errors.password}
  281. renderAccessory={this.renderPasswordAccessory}
  282. disabled={this.props.loginInProgress}
  283. />
  284.  
  285. <View style={{ marginTop: 10 }}>
  286. {this.renderBtnLogin()}
  287. </View>
  288.  
  289. <View style={{ justifyContent: 'center', alignItems: 'center', marginTop: 10 }}>
  290. <TouchableHighlight
  291. underlayColor='transparent'
  292. onPress={() => this._navigateLoginNative()}
  293. >
  294. <Text style={{ color: '#FFF', fontSize: 14 }}>
  295. Esqueci minha senha
  296. </Text>
  297. </TouchableHighlight>
  298. </View>
  299. </View>
  300. </View>
  301. );
  302. }
  303. }
  304.  
  305. const mapStateToProps = state => (
  306. {
  307. error: state.LoginReducer.error,
  308. loginInProgress: state.LoginReducer.loginInProgress,
  309. loginSuccess: state.LoginReducer.loginSuccess
  310. }
  311. );
  312.  
  313. export default connect(mapStateToProps, {
  314. login,
  315. updateFieldError,
  316. updateFieldLoginSuccess,
  317. })(Login);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement