Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. import React, { Component } from "react";
  2. import {
  3. Text,
  4. View,
  5. KeyboardAvoidingView,
  6. Alert,
  7. ScrollView
  8. } from "react-native";
  9. import { connect } from "react-redux";
  10. import {
  11. registerUser,
  12. registerFailedReset
  13. } from "../../register/register.actions";
  14. import styles from "../register.style";
  15. import validation from "../../validation/validation";
  16. import TextField from "../../common/TextInputs/components/textField";
  17. import BigButton from "../../common/buttons/bigButton";
  18. import PageHeader from "../../common/texts/pageHeader";
  19.  
  20. class Register extends Component {
  21. constructor(props) {
  22. super(props);
  23. this.state = {
  24. firstname: "",
  25. lastname: "",
  26. isEmailValid: true,
  27. isPasswordValid: true,
  28. isFirstnameValid: true,
  29. isLastnameValid: true,
  30. isHcpValid: true
  31. };
  32. }
  33.  
  34. handleRegister = () => {
  35. const user = {
  36. firstname: this.state.firstname,
  37. lastname: this.state.lastname
  38. };
  39. this.props.RegisterUser(
  40. this.state.username,
  41. this.state.password,
  42. user,
  43. this.props.navigation
  44. );
  45. };
  46. handleRegisterFailed = () => {
  47. this.props.RegisterFailedReset();
  48. };
  49.  
  50. registerAlert = viewId => {
  51. Alert.alert("", viewId);
  52. };
  53.  
  54. handleEmailValidation = () => {
  55. const regexEmail = validation.regexEmail;
  56. if (regexEmail.test(this.state.username)) {
  57. this.setState({
  58. isEmailValid: true
  59. });
  60. } else {
  61. this.setState({
  62. isEmailValid: false
  63. });
  64. }
  65. };
  66.  
  67. handlePasswordValidation = () => {
  68. const regexPassword = validation.regexPassword;
  69. if (regexPassword.test(this.state.password)) {
  70. this.setState({
  71. isPasswordValid: true
  72. });
  73. } else {
  74. this.setState({
  75. isPasswordValid: false
  76. });
  77. }
  78. };
  79.  
  80. handleFirstnameValidation = () => {
  81. const regexFirstname = validation.regexOnlyLetters;
  82. if (regexFirstname.test(this.state.firstname)) {
  83. this.setState({
  84. isFirstnameValid: true
  85. });
  86. } else {
  87. this.setState({
  88. isFirstnameValid: false
  89. });
  90. }
  91. };
  92.  
  93. handleLastnameValidation = () => {
  94. const regexLastname = validation.regexOnlyLetters;
  95. if (regexLastname.test(this.state.lastname)) {
  96. this.setState({
  97. isLastnameValid: true
  98. });
  99. } else {
  100. this.setState({
  101. isLastnameValid: false
  102. });
  103. }
  104. };
  105.  
  106. render() {
  107. const toggleInputFieldEmail = !this.state.isEmailValid
  108. ? styles.error
  109. : styles.inputContainer;
  110.  
  111. const toggleInputFieldPassword = !this.state.isPasswordValid
  112. ? styles.error
  113. : styles.inputContainer;
  114.  
  115. const toggleInputFieldFirstname = !this.state.isFirstnameValid
  116. ? styles.error
  117. : styles.inputContainer;
  118.  
  119. const toggleInputFieldLastname = !this.state.isLastnameValid
  120. ? styles.error
  121. : styles.inputContainer;
  122.  
  123. return (
  124. <KeyboardAvoidingView style={styles.container} behavior="padding">
  125. <ScrollView>
  126. <PageHeader style={styles.headerText}>REGISTER</PageHeader>
  127. <View style={styles.firstContainer}>
  128. <View style={toggleInputFieldEmail}>
  129. <TextField
  130. autoCorrect={false}
  131. placeholder="Email"
  132. placeholderTextColor="white"
  133. keyboardType="email-address"
  134. underlineColorAndroid="transparent"
  135. onChangeText={username => this.setState({ username })}
  136. onBlur={this.handleEmailValidation}
  137. onFocus={() => this.setState({ isEmailValid: true })}
  138. />
  139. </View>
  140. <View style={toggleInputFieldFirstname}>
  141. <TextField
  142. autoCorrect={false}
  143. placeholder="Firstname"
  144. placeholderTextColor="white"
  145. underlineColorAndroid="transparent"
  146. onChangeText={firstname => this.setState({ firstname })}
  147. onBlur={this.handleFirstnameValidation}
  148. onFocus={() => this.setState({ isFirstnameValid: true })}
  149. />
  150. </View>
  151. <View style={toggleInputFieldLastname}>
  152. <TextField
  153. autoCorrect={false}
  154. placeholder="Lastname"
  155. placeholderTextColor="white"
  156. underlineColorAndroid="transparent"
  157. onChangeText={lastname => this.setState({ lastname })}
  158. onBlur={this.handleLastnameValidation}
  159. onFocus={() => this.setState({ isLastnameValid: true })}
  160. />
  161. </View>
  162.  
  163. <View style={toggleInputFieldPassword}>
  164. <TextField
  165. autoCorrect={false}
  166. placeholder="Password"
  167. placeholderTextColor="white"
  168. secureTextEntry
  169. underlineColorAndroid="transparent"
  170. onChangeText={password => this.setState({ password })}
  171. onBlur={this.handlePasswordValidation}
  172. onFocus={() => this.setState({ isPasswordValid: true })}
  173. />
  174. </View>
  175.  
  176. <BigButton onPress={this.handleRegister} text="Register" />
  177. </View>
  178. </ScrollView>
  179. </KeyboardAvoidingView>
  180. );
  181. }
  182. }
  183.  
  184. const mapStateToProps = state => ({
  185. username: state.register.username,
  186. password: state.register.password,
  187. user: state.register.user,
  188. register: state.register.register,
  189. registerSuccessful: state.register.registerSuccessful,
  190. registerFailed: state.register.registerFailed,
  191. registerFailedEmailIsTaken: state.register.registerFailedEmailIsTaken
  192. });
  193.  
  194. const mapDispatchToProps = dispatch => ({
  195. RegisterUser: (username, password, user, navigation) =>
  196. dispatch(
  197. registerUser(
  198. username,
  199. password,
  200. user.firstname,
  201. user.lastname,
  202. navigation
  203. )
  204. ),
  205. RegisterFailedReset: () => dispatch(registerFailedReset())
  206. });
  207.  
  208. export default connect(
  209. mapStateToProps,
  210. mapDispatchToProps
  211. )(Register);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement