Advertisement
Guest User

index.js

a guest
Apr 24th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import {
  3.   View, ScrollView, Alert
  4. } from 'react-native';
  5.  
  6. import { connect } from 'react-redux';
  7. import ImagePicker from 'react-native-image-picker';
  8.  
  9. import styles from './styles';
  10. import Header from '../../common/Header';
  11. import { getLocalizedStrings } from '../../localization';
  12. import { LOCALIZE_CATEGORIES } from '../../localization/const';
  13. import InputWithIcon from '../../common/InputWithIcon';
  14. import PasswordInput from '../../common/PasswordInput';
  15. import { PRIMARY_GREEN } from '../../utils/Colors';
  16. import GreenButton from '../../common/Buttons/GreenButton';
  17. import TextLink from '../../common/Buttons/TextLink';
  18. import PhotoPicker from '../../common/PhotoPicker';
  19. import { getSize } from '../../utils/Dimensions';
  20. import CityPicker from '../../common/CityPicker';
  21. import EmailSection, { EMAIL_STATUSES } from '../../common/EmailSection';
  22. import PhoneNumberSection from '../../common/PhoneNumberSection';
  23.  
  24. import { userSignUpStep2, userInputOnSignUpChanged } from '../../reducers/auth.duck';
  25. import { citiesLoaded } from '../../reducers/dictionaries.duck';
  26. import {
  27.   onSignUp2,
  28.   sendEmailConfirmationCode,
  29.   checkConfirmationCode,
  30.   getCities,
  31.   changeEmail
  32. } from './logic';
  33. import { uploadAvatarToServer } from '../../utils/commonLogic';
  34.  
  35.  
  36. const OPTIONS = {
  37.   title: 'Select Avatar',
  38.   storageOptions: {
  39.     skipBackup: true,
  40.     path: 'images',
  41.   },
  42. };
  43.  
  44. class ExtendedSignUp extends Component {
  45.   state = {
  46.     emailStatus: EMAIL_STATUSES.codeChecking,
  47.     countdownValue: 0,
  48.     code: '',
  49.     email: '',
  50.     city: '',
  51.     name: '',
  52.     surname: '',
  53.     phone: '',
  54.     password: '',
  55.     sendingCode: false,
  56.     checkingCode: false,
  57.     avatarSource: null,
  58.     loading: false
  59.   };
  60.  
  61.   countdownTimer = null;
  62.  
  63.   componentDidMount() {
  64.     this.user = { ...this.props.user };
  65.     if (this.props.user) {
  66.       const { user } = this.props;
  67.       this.setState({
  68.         email: user.email,
  69.         name: user.name,
  70.         surname: user.surname,
  71.         password: user.password,
  72.         phone: user.phone,
  73.         city: user.city,
  74.         avatarSource: user.avatarSource
  75.       });
  76.       if (user.is_email_confirmed) {
  77.         this.setState({ emailStatus: EMAIL_STATUSES.confirmed });
  78.       }
  79.     }
  80.  
  81.     if (this.props.cities === null || this.props.cities.length === 0) {
  82.       getCities(this);
  83.     }
  84.  
  85.     // NOTE!
  86.     // When the component is displayed the confirmation code has already sent.
  87.     // SignUp screen has that logic.
  88.  
  89.     this.setState({ countdownValue: 60 });
  90.     this.countdownTimer = setInterval(() => {
  91.       if (this.state.countdownValue > 0) {
  92.         this.setState(prevState => ({
  93.           countdownValue: prevState.countdownValue - 1
  94.         }));
  95.       } else {
  96.         clearInterval(this.countdownTimer);
  97.       }
  98.     }, 1000);
  99.     // -- remove this block
  100.   }
  101.  
  102.   componentWillUnmount() {
  103.     if (this.countdownTimer) {
  104.       clearInterval(this.countdownTimer);
  105.     }
  106.   }
  107.  
  108.   onSignUpPress = () => {
  109.     const userForSave = {
  110.       email: this.state.email,
  111.       name: this.state.name,
  112.       surname: this.state.surname,
  113.       password: this.state.password,
  114.       password_confirmation: this.state.password,
  115.       phone: `${this.state.phone}`,
  116.       city_id: this.state.city
  117.     };
  118.     this.setState({ loading: true });
  119.     onSignUp2(userForSave, this);
  120.   };
  121.  
  122.   onSuccessSignUp = () => {
  123.     this.setState({ loading: false });
  124.     this.props.navigation.navigate('Menu');
  125.   };
  126.  
  127.   onFail = (error) => {
  128.     this.setState({ loading: false });
  129.     if (error) {
  130.       this.showMessage(error);
  131.     }
  132.     this.setState({ sendingCode: false });
  133.     this.setState({ checkingCode: false });
  134.   };
  135.  
  136.   showMessage = (message) => {
  137.     Alert.alert(message);
  138.   };
  139.  
  140.   alreadyHaveAccount = () => {
  141.     this.props.navigation.push('SignIn');
  142.   };
  143.  
  144.   changeEmailAndSendCode = () => {
  145.     this.setState({ sendingCode: true });
  146.     changeEmail(this.state.email, this);
  147.     this.changeEmailStatus(EMAIL_STATUSES.codeChecking);
  148.   }
  149.  
  150.   sendEmailConfirmationCode = () => {
  151.     this.setState({ sendingCode: true });
  152.     sendEmailConfirmationCode(this.state.email, this);
  153.     this.changeEmailStatus(EMAIL_STATUSES.codeChecking);
  154.   };
  155.  
  156.   checkEmailConfirmationCode = (code) => {
  157.     this.setState({ checkingCode: true });
  158.     checkConfirmationCode(code, this.state.email, this);
  159.   };
  160.  
  161.   backToNotConfirmed = () => {
  162.     this.changeEmailStatus(EMAIL_STATUSES.needConfirm);
  163.     this.setState({ countdownValue: 0 });
  164.     clearInterval(this.countdownTimer);
  165.   };
  166.  
  167.   onSuccessSendConfirmCode = () => {
  168.     this.setState({ sendingCode: false });
  169.     this.setState({ countdownValue: 60 });
  170.     clearInterval(this.countdownTimer);
  171.  
  172.     this.countdownTimer = setInterval(() => {
  173.       if (this.state.countdownValue > 0) {
  174.         this.setState(prevState => ({
  175.           countdownValue: prevState.countdownValue - 1
  176.         }));
  177.       } else {
  178.         clearInterval(this.countdownTimer);
  179.       }
  180.     }, 1000);
  181.   }
  182.  
  183.   onSuccessConfirmCode = () => {
  184.     this.changeEmailStatus(EMAIL_STATUSES.confirmed);
  185.     this.setState({ checkingCode: false });
  186.     this.user.is_email_confirmed = true;
  187.     this.props.userInputChanged(this.user);
  188.   }
  189.  
  190.   changeEmailStatus = (emailStatus) => {
  191.     this.setState({ emailStatus });
  192.   };
  193.  
  194.   onChangeEmail = (email) => {
  195.     this.setState({ email });
  196.     this.user.email = email;
  197.     this.props.userInputChanged(this.user);
  198.   };
  199.  
  200.   onChangeCode = (code) => {
  201.     this.setState({ code });
  202.   };
  203.  
  204.   onChangeCity = (city) => {
  205.     this.setState({ city });
  206.     this.user.city = city;
  207.     this.props.userInputChanged(this.user);
  208.   };
  209.  
  210.   onChangePhone = (phone) => {
  211.     this.setState({ phone });
  212.     this.user.phone = phone;
  213.     this.props.userInputChanged(this.user);
  214.   }
  215.  
  216.   onChangeName = (name) => {
  217.     this.setState({ name });
  218.     this.user.name = name;
  219.     this.props.userInputChanged(this.user);
  220.   }
  221.  
  222.   onChangeSurname = (surname) => {
  223.     this.setState({ surname });
  224.     this.user.surname = surname;
  225.     this.props.userInputChanged(this.user);
  226.   }
  227.  
  228.   onChangePassword = (password) => {
  229.     this.setState({ password });
  230.     this.user.password = password;
  231.     this.props.userInputChanged(this.user);
  232.   }
  233.  
  234.   selectAvatar = async () => {
  235.     ImagePicker.showImagePicker(OPTIONS, (response) => {
  236.       // console.log('Response = ', response);
  237.       if (response.didCancel) {
  238.         // console.log('User cancelled image picker');
  239.       } else if (response.error) {
  240.         // console.log('ImagePicker Error: ', response.error);
  241.       } else if (response.customButton) {
  242.         // console.log('User tapped custom button: ', response.customButton);
  243.       } else {
  244.         // You can also display the image using data:
  245.         // const uri = 'data:image/jpeg;base64,' + response.data;
  246.         const source = { uri: response.uri };
  247.         uploadAvatarToServer(response);
  248.         this.setState({
  249.           avatarSource: source,
  250.         });
  251.         this.user.avatarSource = source;
  252.         this.props.userInputChanged(this.user);
  253.       }
  254.     });
  255.   }
  256.  
  257.   render() {
  258.     const { language } = this.props;
  259.     return (
  260.       <ScrollView style={styles.contentContainer}>
  261.         <Header title={getLocalizedStrings(language, LOCALIZE_CATEGORIES.common).signUp} />
  262.         <View style={styles.nestedContent}>
  263.           <View style={styles.topSpace}>
  264.             <EmailSection
  265.               title={getLocalizedStrings(language, LOCALIZE_CATEGORIES.common).email.toUpperCase()}
  266.               placeholder={getLocalizedStrings(language, LOCALIZE_CATEGORIES.common).email}
  267.               iconColor={PRIMARY_GREEN}
  268.               status={this.state.emailStatus}
  269.               countdownValue={this.state.countdownValue}
  270.               email={this.state.email}
  271.               code={this.state.code}
  272.               sendEmailConfirmationCode={this.sendEmailConfirmationCode}
  273.               checkEmailConfirmationCode={this.checkEmailConfirmationCode}
  274.               backToNotConfirmed={this.backToNotConfirmed}
  275.               onChangeCode={this.onChangeCode}
  276.               onChangeEmail={this.onChangeEmail}
  277.               sendingCode={this.state.sendingCode}
  278.               checkingCode={this.state.checkingCode}
  279.               changeEmailAndSendCode={this.changeEmailAndSendCode}
  280.             />
  281.           </View>
  282.           <View style={styles.topSpace}>
  283.             <InputWithIcon
  284.               title={getLocalizedStrings(language, LOCALIZE_CATEGORIES.common).firstName.toUpperCase()}
  285.               placeholder={getLocalizedStrings(language, LOCALIZE_CATEGORIES.common).firstName}
  286.               iconName="user"
  287.               iconColor={PRIMARY_GREEN}
  288.               text={this.state.name}
  289.               onChangeText={this.onChangeName}
  290.             />
  291.           </View>
  292.           <View style={styles.topSpace}>
  293.             <InputWithIcon
  294.               title={getLocalizedStrings(language, LOCALIZE_CATEGORIES.common).lastName.toUpperCase()}
  295.               placeholder={getLocalizedStrings(language, LOCALIZE_CATEGORIES.common).lastName}
  296.               iconName="user"
  297.               iconColor={PRIMARY_GREEN}
  298.               text={this.state.surname}
  299.               onChangeText={this.onChangeSurname}
  300.             />
  301.           </View>
  302.           <View style={styles.topSpace}>
  303.             <PasswordInput
  304.               title={getLocalizedStrings(language, LOCALIZE_CATEGORIES.common).password.toUpperCase()}
  305.               placeholder={getLocalizedStrings(language, LOCALIZE_CATEGORIES.common).yourPassword}
  306.               iconName="lock"
  307.               iconColor={PRIMARY_GREEN}
  308.               text={this.state.password}
  309.               onChangeText={this.onChangePassword}
  310.             />
  311.           </View>
  312.           <View style={styles.topSpace}>
  313.             <PhoneNumberSection
  314.               value={this.state.phone}
  315.               onChangePhone={this.onChangePhone}
  316.               withoutConfirm
  317.               initialCountry="es"
  318.             />
  319.           </View>
  320.           <View style={styles.topSpace}>
  321.             <CityPicker
  322.               title={getLocalizedStrings(language, LOCALIZE_CATEGORIES.common).city.toUpperCase()}
  323.               placeholder={getLocalizedStrings(language, LOCALIZE_CATEGORIES.common).city}
  324.               cities={this.props.cities}
  325.               onChangeCity={this.onChangeCity}
  326.               value={this.state.city}
  327.             />
  328.           </View>
  329.           <View style={styles.topSpace}>
  330.             <PhotoPicker
  331.               title={getLocalizedStrings(language, LOCALIZE_CATEGORIES.auth).profileImageUpload.toUpperCase()}
  332.               description={getLocalizedStrings(language, LOCALIZE_CATEGORIES.auth).imagedDescription}
  333.               onPress={this.selectAvatar}
  334.               selectedImage={this.state.avatarSource}
  335.             />
  336.           </View>
  337.           <View style={styles.buttonContainer}>
  338.             <GreenButton
  339.               title={getLocalizedStrings(language, LOCALIZE_CATEGORIES.common).signUp.toUpperCase()}
  340.               buttonWidth={getSize(101)}
  341.               onPress={() => this.onSignUpPress()}
  342.               loading={this.state.loading}
  343.             />
  344.             <TextLink
  345.               onPress={() => this.alreadyHaveAccount()}
  346.               title={getLocalizedStrings(language, LOCALIZE_CATEGORIES.auth).alreadyHaveAnAccount}
  347.             />
  348.           </View>
  349.         </View>
  350.       </ScrollView>
  351.     );
  352.   }
  353. }
  354.  
  355. const mapStateToProps = state => ({
  356.   user: state.auth.user,
  357.   cities: state.dictionaries.cities,
  358.   language: state.language.currentLanguage,
  359. });
  360.  
  361. const mapDispatchToProps = dispatch => ({
  362.   userSignUp: user => dispatch(userSignUpStep2(user)),
  363.   citiesLoaded: cities => dispatch(citiesLoaded(cities)),
  364.   userInputChanged: user => dispatch(userInputOnSignUpChanged(user))
  365. });
  366.  
  367. export default connect(
  368.   mapStateToProps,
  369.   mapDispatchToProps
  370. )(ExtendedSignUp);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement