Advertisement
Guest User

Untitled

a guest
Mar 12th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import {
  3.   StyleSheet,
  4.   Text,
  5.   Image,
  6.   StatusBar
  7. } from 'react-native';
  8. import BaseScreen from '../BaseScreen';
  9. import LinearGradient from 'react-native-linear-gradient';
  10. import CardView from 'react-native-cardview';
  11. import PinView from '../../components/PinView';
  12. import PinKeyboard from '../../components/PinKeyboard';
  13. import Utils from '../../utils/Utils';
  14. import { connect } from 'react-redux';
  15. import { register, uncheckClearPin } from '../../actions/RegThreeActions';
  16.  
  17. class RegThreeScreen extends BaseScreen {
  18.   constructor(props) {
  19.     super(props);
  20.     this.add = this.add.bind(this);
  21.     this.remove = this.remove.bind(this);
  22.  
  23.     this.state = {
  24.       pinMessage: 'Сome up with a 5-digit pin',
  25.       firstPin: '',
  26.       error: ' '
  27.     };
  28.   }
  29.  
  30.   static navigationOptions = {
  31.     header: false,
  32.     gesturesEnabled: false
  33.   };
  34.  
  35.   componentDidUpdate(prevProps) {
  36.     if (this.props.clearPin !== prevProps.clearPin && this.props.clearPin) {
  37.       this.pinView.clear();
  38.       this.props.uncheckClearPin();
  39.     }
  40.   }
  41.  
  42.   async add(pin) {
  43.     await this.pinView.add(pin);
  44.     if (this.pinView.getPin().length === 5) {
  45.       if (!this.state.firstPin) {
  46.         this.state.firstPin = this.pinView.getPin();
  47.         this.pinView.clear();
  48.         this.setState({ pinMessage: 'Repeate pin', error: ' ' });
  49.       } else {
  50.         if (this.state.firstPin === this.pinView.getPin()) {
  51.           this.props.register(this.state.firstPin, this.props.navigation.state.params.Code);
  52.           this.setState({
  53.             pinMessage: 'Сome up with a 5-digit pin',
  54.             error: ' ',
  55.             firstPin: ''
  56.           });
  57.         } else {
  58.           this.pinView.clear();
  59.           this.setState({
  60.             pinMessage: 'Сome up with a 5-digit pin',
  61.             firstPin: '',
  62.             error: 'The passwords do not match, try again'
  63.           });
  64.         }
  65.       }
  66.     }
  67.   }
  68.  
  69.   async remove() {
  70.     await this.pinView.remove();
  71.   }
  72.  
  73.   render() {
  74.     return (
  75.       <LinearGradient
  76.         colors={['#00487b', '#007c94', '#21b0a0']}
  77.         style={styles.container}
  78.         start={{ x: 0.0, y: 1.0 }}
  79.         end={{ x: 1.0, y: 0.0 }}
  80.         onLayout={this.onLayout}
  81.       >
  82.         <StatusBar
  83.           barStyle="light-content"
  84.           backgroundColor="transparent"
  85.           translucent={true}
  86.         />
  87.         <Image
  88.           style={{
  89.             width: Utils.percentHeight(11),
  90.             height: Utils.percentHeight(13)
  91.           }}
  92.           source={require('../../../assets/drawable/logo_reg_3.png')}
  93.         />
  94.         <Text style={styles.welcome}>Registration in TopUp Africa</Text>
  95.  
  96.         <CardView style={[styles.card, Utils.isIphoneX && styles.cardIphoneX]} cardElevation={10} cornerRadius={16}>
  97.           <Text style={styles.enterPin}>{this.state.pinMessage}</Text>
  98.           <PinView loading={this.props.loading} style={styles.pinView} onRef={ref => (this.pinView = ref)} />
  99.           <Text style={styles.errorText}>{this.state.error}</Text>
  100.           <PinKeyboard
  101.             style={styles.keyboard}
  102.             add={this.add}
  103.             remove={this.remove}
  104.           />
  105.         </CardView>
  106.         <Text style={[styles.welcome, { fontSize: 12 }]}>
  107.           In the future, the pin will be used to enter the application
  108.         </Text>
  109.       </LinearGradient>
  110.     );
  111.   }
  112. }
  113.  
  114. const styles = StyleSheet.create({
  115.   container: {
  116.     flex: 1,
  117.     justifyContent: 'flex-start',
  118.     alignItems: 'center',
  119.     backgroundColor: '#005491',
  120.     paddingTop: Utils.percentHeight(5),
  121.   },
  122.   welcome: {
  123.     fontSize: 14,
  124.     textAlign: 'center',
  125.     color: '#ffffff',
  126.     backgroundColor: 'transparent',
  127.     margin: Utils.percentHeight(3)
  128.   },
  129.   card: {
  130.     height: Utils.percentHeight(66),
  131.     width: Utils.percentWidth(90),
  132.     alignSelf: 'center',
  133.     backgroundColor: 'white',
  134.   },
  135.   cardIphoneX: {
  136.     height: Utils.percentHeight(64),
  137.   },
  138.   enterPin: {
  139.     fontSize: 12,
  140.     textAlign: 'center',
  141.     color: '#068696',
  142.     marginTop: Utils.percentHeight(3),
  143.     marginBottom: Utils.percentHeight(1)
  144.   },
  145.   errorText: {
  146.     fontSize: 14,
  147.     color: '#FFC107',
  148.     textAlign: 'center',
  149.     marginTop: Utils.percentHeight(1),
  150.     marginBottom: Utils.percentHeight(1)
  151.   },
  152.   pinView: {
  153.     margin: Utils.percentHeight(1),
  154.   },
  155.   keyboard: {
  156.     flex: 1,
  157.     flexDirection: 'row',
  158.     justifyContent: 'center',
  159.   }
  160. });
  161.  
  162. const mapStateToProps = state => {
  163.   return {
  164.     loading: state.regThree.loading,
  165.     clearPin: state.regThree.clearPin
  166.   };
  167. };
  168.  
  169. const mapDispatchToProps = dispatch => ({
  170.   register: (pin, code) => dispatch(register(pin, code)),
  171.   uncheckClearPin: () => dispatch(uncheckClearPin()),
  172. });
  173.  
  174. export default connect(mapStateToProps, mapDispatchToProps)(RegThreeScreen);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement