Advertisement
Guest User

Untitled

a guest
Apr 29th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.31 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import {
  3. StyleSheet, Text, View, TextInput,
  4. TouchableOpacity,Button,Alert
  5. } from 'react-native';
  6. import RadioGroup from "react-native-radio-buttons-group";
  7. import app, { db } from '../config/fire';
  8. import { Formik } from 'formik'
  9. import * as yup from 'yup'
  10.  
  11. import Logo from './Logo';
  12.  
  13.  
  14. class Register extends Component {
  15. constructor(props) {
  16. super(props);
  17. this.state = {
  18. email: '',
  19. password: '',
  20. contactNumber: '',
  21. isMobile: true,
  22. user_type: '',
  23. user: {},
  24. userId: '',
  25. data: [
  26. {
  27. label: "Responder",
  28. value: "Responder",
  29. color: "white",
  30. },
  31. {
  32. label: "Regular User",
  33. value: "Regular User",
  34. color: "white",
  35. },
  36. {
  37. label: "Volunteer",
  38. value: "Volunteer",
  39. color: "white",
  40. },
  41. ]
  42. };
  43. }
  44.  
  45. userType = data => {
  46. this.setState({ data });
  47.  
  48. let selectedButton = this.state.data.find(e => e.selected == true);
  49. selectedButton = selectedButton
  50. ? selectedButton.value
  51. : this.state.data[0].label;
  52. this.setState({ user_type: selectedButton });
  53.  
  54. };
  55.  
  56. createUserAccount(values) {
  57.  
  58. var email = this.values.email;
  59. var password = this.values.password;
  60. const auth = app.auth();
  61. const promise = auth.createUserWithEmailAndPassword(email.trim(), password.trim());
  62.  
  63. promise.then(user => {
  64. console.log('account created');
  65. let app = db.ref('users/' + user.user.uid);
  66.  
  67. app.update({
  68. email: this.values.email,
  69. password: this.values.password,
  70. contactNumber: this.values.contactNumber,
  71. isMobile: true,
  72. user_type: this.state.user_type,
  73. });
  74.  
  75. });
  76. promise.catch(e => {
  77. var err = e.message;
  78. console.log(err);
  79. })
  80. console.log("loog", this.values.email, this.values.password);
  81. };
  82.  
  83.  
  84. render() {
  85. return (
  86. <Formik initialValues={{email:'',contactNumber:'',password:''}}
  87. onSubmit={this.createUserAccount(values)}
  88. validationSchema={
  89. yup.object().shape({
  90. email: yup
  91. .string()
  92. .email()
  93. .required(),
  94. contactNumber: yup
  95. .string()
  96. .required(),
  97. password: yup
  98. .string()
  99. .required(),
  100. })
  101. }>
  102. {({ values, handleChange, errors, setFieldTouched, touched, isValid, handleSubmit }) => (
  103. <View style={styles.container}>
  104. <TextInput style={styles.inputBox}
  105. underlineColorAndroid='rgba(0,0,0,0)'
  106. placeholder="Email Address"
  107. placeholderTextColor="#ffffff"
  108. selectionColor="#fff"
  109. keyboardType="email-address"
  110. value={values.email}
  111. onChangeText={handleChange('email')}
  112. onBlur={() => setFieldTouched('email')}
  113. />
  114. {touched.email && errors.email &&
  115. <Text style={{ fontSize: 10, color: 'red' }}>{errors.email}</Text>
  116. }
  117. <TextInput style={styles.inputBox}
  118. underlineColorAndroid='rgba(0,0,0,0)'
  119. placeholder="Contact Number"
  120. placeholderTextColor="#ffffff"
  121. selectionColor="#fff"
  122. keyboardType="email-address"
  123. value={values.contactNumber}
  124. onChangeText={handleChange('contactNumber')}
  125. onBlur={() => setFieldTouched('contactNumber')}
  126. />
  127. {touched.contactNumber && errors.contactNumber &&
  128. <Text style={{ fontSize: 10, color: 'red' }}>{errors.contactNumber}</Text>
  129. }
  130. <TextInput style={styles.inputBox}
  131. underlineColorAndroid='rgba(0,0,0,0)'
  132. placeholder="Password"
  133. secureTextEntry={true}
  134. placeholderTextColor="#ffffff"
  135. value={values.password}
  136. onChangeText={handleChange('password')}
  137. onBlur={() => setFieldTouched('password')}
  138.  
  139. />
  140. {touched.password && errors.password &&
  141. <Text style={{ fontSize: 10, color: 'red' }}>{errors.password}</Text>
  142. }
  143. <RadioGroup radioButtons={this.state.data} onPress={this.userType} />
  144. <TouchableOpacity style={styles.button}
  145. disabled={!isValid}
  146. onPress={handleSubmit}>
  147.  
  148. <Text style={styles.buttonText}>
  149. Register
  150. </Text>
  151. </TouchableOpacity>
  152. </View>
  153. )}
  154. </Formik>
  155. );
  156. }
  157.  
  158. }
  159.  
  160. const styles = StyleSheet.create({
  161. container: {
  162. flexGrow: 1,
  163. justifyContent: 'center',
  164. alignItems: 'center',
  165. backgroundColor: '#455a64',
  166. },
  167. signupTextCont: {
  168. flexGrow: 1,
  169. alignItems: 'flex-end',
  170. justifyContent: 'center',
  171. paddingVertical: 16,
  172. flexDirection: 'row'
  173. },
  174. signupText: {
  175. color: 'rgba(255,255,255,0.6)',
  176. fontSize: 16
  177. },
  178. signupButton: {
  179. color: '#ffffff',
  180. fontSize: 16,
  181. fontWeight: '500'
  182. },
  183. inputBox: {
  184. width: 300,
  185. backgroundColor: 'rgba(255, 255,255,0.2)',
  186. borderRadius: 25,
  187. paddingHorizontal: 16,
  188. fontSize: 16,
  189. color: '#ffffff',
  190. marginVertical: 10
  191. },
  192. button: {
  193. width: 300,
  194. backgroundColor: '#1c313a',
  195. borderRadius: 25,
  196. marginVertical: 10,
  197. paddingVertical: 13
  198. },
  199. buttonText: {
  200. fontSize: 16,
  201. fontWeight: '500',
  202. color: '#ffffff',
  203. textAlign: 'center'
  204. }
  205.  
  206. });
  207.  
  208. export default Register;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement