Advertisement
Guest User

Untitled

a guest
May 11th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import { TouchableOpacity, Button, ScrollView, ActivityIndicator, View, StyleSheet, Text } from 'react-native';
  3. import PropTypes from 'prop-types';
  4. import DeviceInfo from 'react-native-device-info';
  5. import LocalizedString from '../localization';
  6. import CustomTextInput from '../components/custom-text-input';
  7. import AuthenticationPageHeader from '../components/authentication-page-header';
  8. import Constants from '../constants';
  9.  
  10. const userIcon = require('../../assets/icons/ic_user.png');
  11. const unlockedIcon = require('../../assets/icons/ic_unlocked.png');
  12.  
  13. const styles = StyleSheet.create({
  14.   mainContainer: { flex: 1 },
  15.   loginButtonContainer: {
  16.     marginHorizontal: 20,
  17.     height: 60,
  18.     marginTop: 20,
  19.     backgroundColor: Constants.COLOR_MAIN,
  20.     alignSelf: 'stretch',
  21.     borderRadius: 50,
  22.   },
  23.   loginButtonText: {
  24.     fontFamily: Constants.FONT_BOLD,
  25.     fontWeight: 'normal',
  26.     fontSize: 19,
  27.   },
  28.   viewBody: {
  29.     flex: 1, marginTop: -25,
  30.   },
  31.   viewFooter: {
  32.     flex: 1,
  33.     justifyContent: 'center',
  34.     alignItems: 'center',
  35.   },
  36.   background: {
  37.     resizeMode: 'contain',
  38.   },
  39.   logo: {
  40.     resizeMode: 'contain',
  41.     height: 90,
  42.   },
  43.   textLogin: {
  44.     backgroundColor: 'transparent',
  45.     fontFamily: Constants.FONT_BOLD,
  46.     fontSize: 18,
  47.     color: Constants.COLOR_MAIN,
  48.   },
  49.   textForgotPassword: {
  50.     alignSelf: 'flex-end',
  51.     fontFamily: Constants.FONT_REGULAR,
  52.     fontSize: 12,
  53.     marginTop: 10,
  54.     marginRight: 20,
  55.   },
  56.   textDontHaveAccount: {
  57.     fontFamily: Constants.FONT_REGULAR,
  58.     fontSize: 11,
  59.     color: 'black',
  60.   },
  61.   textRegister: {
  62.     fontFamily: Constants.FONT_BOLD,
  63.     fontSize: 20,
  64.     color: 'black',
  65.   },
  66.   appVersionText: {
  67.     fontFamily: Constants.FONT_LIGHT_ITALIC,
  68.     fontSize: 10,
  69.     textAlign: 'right',
  70.     marginHorizontal: 10,
  71.     marginTop: 10,
  72.   },
  73.   verticalSpacer: {
  74.     height: 15,
  75.   },
  76. });
  77.  
  78. export default class Login extends React.Component {
  79.   constructor(props) {
  80.     super(props);
  81.     this.state = {
  82.       username: '',
  83.       password: '',
  84.       editable: true,
  85.     };
  86.     this.isComponentMounted = false;
  87.  
  88.     this.onChangeUsernameText = this.onChangeUsernameText.bind(this);
  89.     this.onChangePasswordText = this.onChangePasswordText.bind(this);
  90.     this.onLoginPressed = this.onLoginPressed.bind(this);
  91.   }
  92.  
  93.   componentDidMount() {
  94.     this.isComponentMounted = true;
  95.   }
  96.  
  97.   componentWillUnmount() {
  98.     this.isComponentMounted = false;
  99.   }
  100.  
  101.   onChangeUsernameText = (text) => {
  102.     this.setState({ username: text.trim() });
  103.   }
  104.  
  105.   onChangePasswordText = (text) => {
  106.     this.setState({ password: text.trim() });
  107.   }
  108.  
  109.   onLoginPressed = async () => {
  110.     try {
  111.       this.setState({ editable: false });
  112.       await this.props.onLoginPressed(this.state.username, this.state.password);
  113.     } finally {
  114.       if (this.isComponentMounted) {
  115.         this.setState({ editable: true });
  116.       }
  117.     }
  118.   }
  119.  
  120.   onForgotPasswordPressed = () => {
  121.     if (this.state.editable) {
  122.       this.props.onForgotPasswordPressed();
  123.     }
  124.   }
  125.  
  126.   renderRegisterButton = () => {
  127.     if (this.state.editable) {
  128.       return (
  129.         <View style={styles.viewFooter}>
  130.           <Text style={styles.textDontHaveAccount}>
  131.             {LocalizedString.loginScreen.doNotHaveAccountLabel}
  132.           </Text>
  133.           <TouchableOpacity onPress={this.props.onRegisterPressed}>
  134.             <Text style={styles.textRegister}>{LocalizedString.loginScreen.registerLabel}</Text>
  135.           </TouchableOpacity>
  136.           <Text style={styles.appVersionText}>v{DeviceInfo.getVersion()}</Text>
  137.         </View>
  138.       );
  139.     }
  140.     return null;
  141.   }
  142.  
  143.   renderActivityIndicator = () => {
  144.     if (!this.state.editable) {
  145.       return (
  146.         <View>
  147.           <View style={styles.verticalSpacer} />
  148.           <ActivityIndicator size="large" color={Constants.COLOR_MAIN} />
  149.         </View>
  150.       );
  151.     }
  152.     return null;
  153.   }
  154.  
  155.   render() {
  156.     const styleDisabled = this.state.editable ? null : { color: Constants.COLOR_HORIZONTAL_LINE };
  157.     return (
  158.       <ScrollView>
  159.         <View style={styles.mainContainer} >
  160.           <AuthenticationPageHeader title={LocalizedString.loginScreen.title} />
  161.           <View style={styles.viewBody}>
  162.             <CustomTextInput
  163.               fieldName={LocalizedString.loginScreen.usernameLabel}
  164.               placeholder={LocalizedString.loginScreen.usernamePlaceholder}
  165.               icon={userIcon}
  166.               onChangeText={this.onChangeUsernameText}
  167.               editable={this.state.editable}
  168.               value={this.state.username}
  169.               keyboardType="email-address"
  170.             />
  171.             <View style={styles.verticalSpacer} />
  172.             <CustomTextInput
  173.               fieldName={LocalizedString.loginScreen.passwordLabel}
  174.               placeholder={LocalizedString.loginScreen.passwordPlaceholder}
  175.               icon={unlockedIcon}
  176.               secureTextEntry
  177.               onChangeText={this.onChangePasswordText}
  178.               editable={this.state.editable}
  179.               value={this.state.password}
  180.             />
  181.             <TouchableOpacity onPress={this.onForgotPasswordPressed}>
  182.               <Text style={[styles.textForgotPassword, styleDisabled]}>
  183.                 {LocalizedString.loginScreen.forgetPasswordLabel}
  184.               </Text>
  185.             </TouchableOpacity>
  186.             <View style={styles.verticalSpacer} />
  187.             <View style={{ flex: 1, flexDirection: 'row' }}>
  188.               <View style={{ flex: 1 }} />
  189.               <View style={{ flex: 2 }}>
  190.                 <Button
  191.                   disabled={!this.state.editable}
  192.                   title={LocalizedString.loginScreen.signInCaption}
  193.                   style={{ container: styles.loginButtonContainer, text: styles.loginButtonText }}
  194.                   onPress={this.onLoginPressed}
  195.                 />
  196.               </View>
  197.               <View style={{ flex: 1 }} />
  198.             </View>
  199.             <View style={styles.verticalSpacer} />
  200.             {this.renderActivityIndicator()}
  201.             {this.renderRegisterButton()}
  202.           </View>
  203.         </View>
  204.       </ScrollView>
  205.     );
  206.   }
  207. }
  208.  
  209. Login.propTypes = {
  210.   onLoginPressed: PropTypes.func.isRequired,
  211.   onRegisterPressed: PropTypes.func.isRequired,
  212.   onForgotPasswordPressed: PropTypes.func.isRequired,
  213. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement