Advertisement
Guest User

Untitled

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