Guest User

Untitled

a guest
Feb 15th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import {
  3. Platform,
  4. StyleSheet,
  5. Text,
  6. View,
  7. TouchableWithoutFeedback,
  8. Keyboard
  9. } from 'react-native';
  10.  
  11. import {
  12. Container,
  13. Item,
  14. Label,
  15. Input,
  16. Header,
  17. Form,
  18. Content,
  19. Button,
  20. Picker
  21. } from 'native-base';
  22. import Dimensions from 'Dimensions';
  23. import * as firebase from 'firebase';
  24.  
  25. const {width, height} = Dimensions.get('window');
  26.  
  27. export default class SignUp extends Component {
  28. constructor(props) {
  29. super(props);
  30. this.state = ({
  31. email: '',
  32. password: '',
  33. name: '',
  34. age: '',
  35. gender: ''
  36. })
  37. }
  38.  
  39. static navigationOptions = {
  40. title: 'SignUp',
  41. };
  42.  
  43. writeUserData = (userId) => {
  44. const {name, email, age, gender} = this.state;
  45. firebase.database().ref('users/' + userId).set({
  46. name: name,
  47. email: email,
  48. age: age,
  49. gender: gender
  50. });
  51. }
  52.  
  53. signUpUser = (email, password) => {
  54. firebase.auth().createUserWithEmailAndPassword(email, password)
  55. .then((user) => {
  56. console.log(`user: ${JSON.stringify(user)}`);
  57. this.writeUserData(user.uid);
  58. this.props.navigation.goBack();
  59. })
  60. .catch((error) => {
  61. const {code, message} = error;
  62. alert(`${message}`);
  63. });
  64. }
  65.  
  66. setPassword = (text) => {
  67. this.setState({
  68. password: text.replace(/[^0-9]/g, '')
  69. });
  70. }
  71.  
  72. setAge = (age) => {
  73. this.setState({
  74. age: age.replace(/[^0-9]/g, '')
  75. })
  76. }
  77.  
  78. setUsername = (name) => {
  79. this.setState({
  80. name: name
  81. });
  82. }
  83.  
  84. selectGender = (value) => {
  85. this.setState({
  86. gender: value
  87. });
  88. }
  89. render() {
  90. return (
  91. <TouchableWithoutFeedback onPress={() => Keyboard.dismiss()}>
  92. <Container style={styles.container}>
  93. <Content>
  94. <Form style={{marginTop: 10}}>
  95. <Item floatingLabel error={false}>
  96. <Label style={{padding: 5}}>Name</Label>
  97. <Input
  98. autoCorrect={false}
  99. autoCapitalize={'none'}
  100. onChangeText={(name) => this.setUsername(name)}
  101. maxLength={30}
  102. //keyboardType={'numeric'}
  103. />
  104. </Item>
  105. <Item floatingLabel style={{marginBottom: 10}}>
  106. <Label style={{padding: 5}}>Age</Label>
  107. <Input
  108. value={this.state.age}
  109. keyboardType={'numeric'}
  110. onChangeText={(password) => this.setAge(password)}
  111. value={this.state.age}
  112. maxLength={2}
  113. />
  114. </Item>
  115. <Label style={{marginLeft: 18, marginTop: 25}}>Gender</Label>
  116. <Picker
  117. style={{paddingTop: 0, width:(Platform.OS === 'ios') ? undefined : 120, marginLeft:(Platform.OS === 'ios') ? 3 : 10}}
  118. iosHeader="Select one"
  119. mode="dropdown"
  120. selectedValue={this.state.gender}
  121. onValueChange={(value) => this.selectGender(value)}
  122. placeholder='Tap to select gender'
  123. >
  124. <Picker.Item label="Male" value="M" />
  125. <Picker.Item label="Female" value="F" />
  126. <Picker.Item label="Prefer not to say" value="N" />
  127. </Picker>
  128. <View style={{borderBottomColor: 'lightgray', borderBottomWidth: 1, width: width * 0.90, marginLeft: 17, marginTop: 3, marginBottom: 10}}/>
  129. <Item floatingLabel style={{marginTop: 0, paddingTop: 0}}>
  130. <Label style={{padding: 5}}>Email</Label>
  131. <Input
  132. autoCorrect={false}
  133. autoCapitalize={'none'}
  134. onChangeText={(email) => this.setState({email})}
  135. />
  136. </Item>
  137. <Item floatingLabel>
  138. <Label style={{padding: 5}}>Password</Label>
  139. <Input
  140. secureTextEntry
  141. onChangeText={(password) => this.setPassword(password)}
  142. value={this.state.password}
  143. keyboardType={'numeric'}
  144. />
  145. </Item>
  146. <Button
  147. style={{marginTop: 25}}
  148. full
  149. rounded
  150. primary
  151. onPress={() => this.signUpUser(this.state.email, this.state.password)}
  152. >
  153. <Text style={{color: 'white'}}>Sign-Up</Text>
  154. </Button>
  155. </Form>
  156. </Content>
  157. </Container>
  158. </TouchableWithoutFeedback>
  159. );
  160. }
  161. }
  162.  
  163. const styles = StyleSheet.create({
  164. container: {
  165. flex: 1,
  166. justifyContent: 'center',
  167. backgroundColor: '#F5FCFF',
  168. padding: 10
  169. },
  170. });
Add Comment
Please, Sign In to add comment