yosadade

UpdateProfile.js

Jun 6th, 2020
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. import React, {useState, useEffect} from 'react';
  2. import {StyleSheet, View, ScrollView} from 'react-native';
  3. import {Header, Profile, Input, Button, Gap} from '../../components';
  4. import {colors, getData, storeData} from '../../utils';
  5. import {Fire} from '../../config';
  6. import {showMessage} from 'react-native-flash-message';
  7. import ImagePicker from 'react-native-image-picker';
  8. import {ILNullPhoto} from '../../assets';
  9.  
  10. const UpdateProfile = ({navigation}) => {
  11. const [profile, setProfile] = useState({
  12. fullName: '',
  13. profession: '',
  14. email: '',
  15. });
  16.  
  17. const [password, setPassword] = useState('');
  18. const [photo, setPhoto] = useState(ILNullPhoto);
  19. const [photoForDB, setPhotoForDB] = useState('');
  20.  
  21. useEffect(() => {
  22. getData('user').then(res => {
  23. const data = res;
  24. setPhoto({uri: res.photo});
  25. setProfile(data);
  26. });
  27. }, []);
  28.  
  29. const update = () => {
  30. console.log('profile', profile);
  31. console.log('new password: ', password);
  32.  
  33. if (password.length > 0) {
  34. if (password.length < 6) {
  35. showMessage({
  36. message: 'Password kurang dari 6 karakter',
  37. type: 'default',
  38. backgroundColor: colors.error,
  39. color: colors.white,
  40. });
  41. } else {
  42. // update password
  43. updatePassword();
  44. updateProfileData();
  45. navigation.replace('MainApp');
  46. }
  47. } else {
  48. updateProfileData();
  49. navigation.replace('MainApp');
  50. }
  51. };
  52.  
  53. const updatePassword = () => {
  54. Fire.auth().onAuthStateChanged(user => {
  55. if (user) {
  56. // update password
  57. user.updatePassword(password).catch(err => {
  58. showMessage({
  59. message: err.message,
  60. type: 'default',
  61. backgroundColor: colors.error,
  62. color: colors.white,
  63. });
  64. });
  65. }
  66. });
  67. };
  68.  
  69. const updateProfileData = () => {
  70. const data = profile;
  71. data.photo = photoForDB;
  72.  
  73. Fire.database()
  74. .ref(`users/${profile.uid}/`)
  75. .update(data)
  76. .then(() => {
  77. console.log('success', data);
  78. storeData('user', data);
  79. })
  80. .catch(err => {
  81. showMessage({
  82. message: err.message,
  83. type: 'default',
  84. backgroundColor: colors.error,
  85. color: colors.white,
  86. });
  87. });
  88. };
  89.  
  90. const changeText = (key, value) => {
  91. setProfile({
  92. ...profile,
  93. [key]: value,
  94. });
  95. };
  96.  
  97. const getImage = () => {
  98. ImagePicker.launchImageLibrary(
  99. {quality: 0.5, maxWidth: 200, maxHeight: 200},
  100. response => {
  101. console.log('response', response);
  102. if (response.didCancel || response.error) {
  103. showMessage({
  104. message: 'oops, sepertinya anda tidak memilih foto nya ?',
  105. type: 'default',
  106. backgroundColor: colors.error,
  107. color: colors.white,
  108. });
  109. } else {
  110. const source = {uri: response.uri};
  111.  
  112. console.log('response getImage', response);
  113. setPhotoForDB(`data: ${response.type};base64, ${response.data}`);
  114. setPhoto(source);
  115. }
  116. },
  117. );
  118. };
  119.  
  120. return (
  121. <View style={styles.page}>
  122. <Header title="Edit Profile" onPress={() => navigation.goBack()} />
  123. <ScrollView showsVerticalScrollIndicator={false}>
  124. <View style={styles.content}>
  125. <Profile isRemove photo={photo} onPress={getImage} />
  126. <Gap height={26} />
  127. <Input
  128. label="Full Name"
  129. value={profile.fullName}
  130. onChangeText={value => changeText('fullName', value)}
  131. />
  132. <Gap height={24} />
  133. <Input
  134. label="Pekerjaan"
  135. value={profile.profession}
  136. onChangeText={value => changeText('profession', value)}
  137. />
  138. <Gap height={24} />
  139. <Input label="Email" value={profile.email} disable />
  140. <Gap height={24} />
  141. <Input
  142. label="Password"
  143. value={password}
  144. secureTextEntry
  145. onChangeText={value => setPassword(value)}
  146. />
  147. <Gap height={40} />
  148. <Button title="Save Profile" onPress={update} />
  149. </View>
  150. </ScrollView>
  151. </View>
  152. );
  153. };
  154.  
  155. export default UpdateProfile;
  156.  
  157. const styles = StyleSheet.create({
  158. page: {
  159. flex: 1,
  160. backgroundColor: colors.white,
  161. },
  162. content: {
  163. padding: 40,
  164. paddingTop: 0,
  165. },
  166. });
Add Comment
Please, Sign In to add comment