Guest User

Untitled

a guest
Feb 12th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.06 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import {
  3. Container,
  4. Item,
  5. Label,
  6. Input,
  7. Header,
  8. Form,
  9. Content,
  10. Button,
  11. Text,
  12. Picker,
  13. Toast
  14. } from 'native-base';
  15.  
  16. import {View, StyleSheet, Keyboard, TouchableWithoutFeedback, ActivityIndicator} from 'react-native';
  17.  
  18. import Dimensions from 'Dimensions';
  19.  
  20. import * as firebase from 'firebase';
  21.  
  22. import {Actions} from 'react-native-router-flux';
  23.  
  24.  
  25. const {width, height} = Dimensions.get('window');
  26.  
  27. export default class Profile extends Component {
  28. constructor(props) {
  29. super(props);
  30. this.state = ({
  31. email: '',
  32. password: '',
  33. name: '',
  34. age: '',
  35. gender: 'M',
  36. user: {},
  37. postData: {},
  38. userId: ''
  39. })
  40. }
  41. static user = {};
  42. componentDidMount() {
  43. const user = firebase.auth().currentUser;
  44. //console.log(`CDM user : ${user.uid}`);
  45. firebase.database().ref(`/users/${user.uid}`).once('value', (snapshot) => {
  46. //USER = snapshot.val()
  47. //this.user = (snapshot.val() && snapshot.val().name) || 'Anonymous';
  48. this.setState({
  49. user: snapshot.val(),
  50. userId: user.uid
  51. })
  52. });
  53. //this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', this.keyboardDidShow);
  54. }
  55.  
  56. /* componentWillUnmount() {
  57. this.keyboardDidShowListener.remove();
  58. }
  59.  
  60. keyboardDidShow = () => {
  61. console.log('KEYBOARD IS OPEN');
  62. } */
  63.  
  64. logOut = () => {
  65. firebase.auth().signOut()
  66. .then(() => {
  67. //console.log(`this.props: ${JSON.stringify(this.props)}`);
  68. Actions.reset('HomeScreen');
  69. })
  70. .catch((error) => {
  71. alert(error);
  72. })
  73. }
  74.  
  75. setAge = (age) => {
  76. const val = age.replace(/[^0-9]/g, '');
  77. this.setState({
  78. user: {
  79. ...this.state.user,
  80. age: val
  81. },
  82. postData: {
  83. ...this.state.postData,
  84. age: val
  85. }
  86. })
  87. }
  88.  
  89. setUsername = (name) => {
  90. this.setState({
  91. user: {
  92. ...this.state.user,
  93. name: name
  94. },
  95. postData: {
  96. ...this.state.postData,
  97. name: name
  98. }
  99. });
  100. }
  101.  
  102. onValueChange = (value) => {
  103. this.setState({
  104. user: {
  105. ...this.state.user,
  106. gender: value
  107. },
  108. postData: {
  109. ...this.state.postData,
  110. gender: value
  111. }
  112. })
  113. }
  114.  
  115. saveData = () => {
  116. Keyboard.dismiss();
  117. console.log(`fieldRef: ${JSON.stringify(fieldRef)}`);
  118. const fieldRef = firebase.database().ref(`users/${this.state.userId}`);
  119. fieldRef.update({...this.state.postData}).then(() => {
  120. alert('Data saved successfully');
  121. }).catch(() => {
  122. alert('Something went wrong');
  123. });
  124. }
  125.  
  126. render() {
  127. console.log(`user data: ${JSON.stringify(this.state.user)}`);
  128. /* console.log(`USER: ${JSON.stringify(USER)}`);
  129. console.log(`user data: ${JSON.stringify(this.state.user)}`); */
  130. const {user} = this.state;
  131. return (
  132. <TouchableWithoutFeedback onPress={Keyboard.dismiss}>
  133. <Container style={styles.container}>
  134. <Content
  135. bounces={false}
  136. >
  137. <Form style={{marginTop: 25}}>
  138. <Item floatingLabel>
  139. <Label>Name</Label>
  140. <Input
  141. autoCorrect={false}
  142. autoCapitalize={'none'}
  143. onChangeText={(name) => this.setUsername(name)}
  144. value={this.state.user.name}
  145. onSubmitEditing={Keyboard.dismiss}
  146. //keyboardType={'numeric'}
  147. />
  148. </Item>
  149. <Item floatingLabel>
  150. <Label>Age</Label>
  151. <Input
  152. keyboardType={'numeric'}
  153. onChangeText={(age) => this.setAge(age)}
  154. value={this.state.user.age}
  155. maxLength={2}
  156. onSubmitEditing={Keyboard.dismiss}
  157. />
  158. </Item>
  159. <Item stackedLabel>
  160. <Label>Gender</Label>
  161. <Picker
  162. iosHeader="Select one"
  163. mode="dropdown"
  164. selectedValue={this.state.user.gender}
  165. onValueChange={(value) => this.onValueChange(value)}
  166. placeholder='Tap to select gender'
  167. >
  168. <Picker.Item label="Male" value="M" />
  169. <Picker.Item label="Female" value="F" />
  170. <Picker.Item label="Prefer not to say" value="N" />
  171. </Picker>
  172. </Item>
  173. <Button
  174. style={{marginTop: 40}}
  175. full
  176. rounded
  177. primary
  178. onPress={() => this.saveData()}
  179. >
  180. <Text style={{color: 'white'}}>Save Changes</Text>
  181. </Button>
  182. <Button
  183. style={{marginTop: 10}}
  184. full
  185. rounded
  186. success
  187. onPress={() => this.logOut()}
  188. >
  189. <Text>Log-Out</Text>
  190. </Button>
  191. </Form>
  192. </Content>
  193.  
  194. </Container>
  195. </TouchableWithoutFeedback>
  196. );
  197. /* if (isLoading) {
  198. return <Loader isLoading={isLoading}/>
  199. } else {
  200. return (
  201. <TouchableWithoutFeedback onPress={Keyboard.dismiss}>
  202. <Container style={styles.container}>
  203. <Content
  204. bounces={false}
  205. >
  206. <Form style={{marginTop: 25}}>
  207. <Item floatingLabel>
  208. <Label>Name</Label>
  209. <Input
  210. autoCorrect={false}
  211. autoCapitalize={'none'}
  212. onChangeText={(name) => this.setUsername(name)}
  213. value={this.state.user.name}
  214. onSubmitEditing={Keyboard.dismiss}
  215. //keyboardType={'numeric'}
  216. />
  217. </Item>
  218. <Item floatingLabel>
  219. <Label>Age</Label>
  220. <Input
  221. keyboardType={'numeric'}
  222. onChangeText={(age) => this.setAge(age)}
  223. value={this.state.user.age}
  224. maxLength={2}
  225. onSubmitEditing={Keyboard.dismiss}
  226. />
  227. </Item>
  228. <Item stackedLabel>
  229. <Label>Gender</Label>
  230. <Picker
  231. iosHeader="Select one"
  232. mode="dropdown"
  233. selectedValue={this.state.user.gender}
  234. onValueChange={(value) => this.onValueChange(value)}
  235. placeholder='Tap to select gender'
  236. >
  237. <Picker.Item label="Male" value="M" />
  238. <Picker.Item label="Female" value="F" />
  239. <Picker.Item label="Prefer not to say" value="N" />
  240. </Picker>
  241. </Item>
  242. <Button
  243. style={{marginTop: 40}}
  244. full
  245. rounded
  246. primary
  247. onPress={() => this.saveData()}
  248. >
  249. <Text style={{color: 'white'}}>Save Changes</Text>
  250. </Button>
  251. <Button
  252. style={{marginTop: 10}}
  253. full
  254. rounded
  255. success
  256. onPress={() => this.logOut()}
  257. >
  258. <Text>Log-Out</Text>
  259. </Button>
  260. </Form>
  261. </Content>
  262.  
  263. </Container>
  264. </TouchableWithoutFeedback>
  265. );
  266. } */
  267. }
  268. }
  269.  
  270. /* const ProfileForm = ({user}) => {
  271. return (
  272. <TouchableWithoutFeedback onPress={Keyboard.dismiss}>
  273. <Container style={styles.container}>
  274. <Content
  275. bounces={false}
  276. >
  277. <Form style={{marginTop: 25}}>
  278. <Item floatingLabel>
  279. <Label>Name</Label>
  280. <Input
  281. autoCorrect={false}
  282. autoCapitalize={'none'}
  283. onChangeText={(name) => this.setUsername(name)}
  284. value={user.name}
  285. onSubmitEditing={Keyboard.dismiss}
  286. //keyboardType={'numeric'}
  287. />
  288. </Item>
  289. <Item floatingLabel>
  290. <Label>Age</Label>
  291. <Input
  292. keyboardType={'numeric'}
  293. onChangeText={(age) => this.setAge(age)}
  294. value={user.age}
  295. maxLength={2}
  296. onSubmitEditing={Keyboard.dismiss}
  297. />
  298. </Item>
  299. <Item stackedLabel>
  300. <Label>Gender</Label>
  301. <Picker
  302. iosHeader="Select one"
  303. mode="dropdown"
  304. selectedValue={user.gender}
  305. onValueChange={(value) => this.onValueChange(value)}
  306. placeholder='Tap to select gender'
  307. >
  308. <Picker.Item label="Male" value="M" />
  309. <Picker.Item label="Female" value="F" />
  310. <Picker.Item label="Prefer not to say" value="N" />
  311. </Picker>
  312. </Item>
  313. <Button
  314. style={{marginTop: 40}}
  315. full
  316. rounded
  317. primary
  318. onPress={() => this.saveData()}
  319. >
  320. <Text style={{color: 'white'}}>Save Changes</Text>
  321. </Button>
  322. <Button
  323. style={{marginTop: 10}}
  324. full
  325. rounded
  326. success
  327. onPress={() => this.logOut()}
  328. >
  329. <Text>Log-Out</Text>
  330. </Button>
  331. </Form>
  332. </Content>
  333.  
  334. </Container>
  335. </TouchableWithoutFeedback>
  336. )
  337. } */
  338.  
  339. const Loader = ({isLoading}) => {
  340. return (
  341. <View
  342. style={{
  343. alignItems: 'center',
  344. justifyContent: 'center',
  345. top: 0,
  346. left: 0,
  347. position: 'absolute',
  348. height: height,
  349. width: width,
  350. backgroundColor: 'black',
  351. opacity: 0.5,
  352. }}
  353. >
  354. <ActivityIndicator large animating={isLoading} />
  355. </View>
  356. );
  357. }
  358.  
  359. const styles = StyleSheet.create({
  360. container: {
  361. flex: 1,
  362. justifyContent: 'center',
  363. backgroundColor: '#F5FCFF',
  364. padding: 10
  365. },
  366. });
Add Comment
Please, Sign In to add comment