Skip to content

Commit 1671aa4

Browse files
committed
Fix add & edit profile
1 parent e3129aa commit 1671aa4

File tree

4 files changed

+94
-65
lines changed

4 files changed

+94
-65
lines changed

src/layout/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { Component } from 'react'
22
import { createStackNavigator, createAppContainer } from 'react-navigation'
33

4+
import Test from '../screens/test'
45
import Splash from '../screens/splash'
56
import Register from '../screens/register'
67
import Profile from '../screens/profile'
@@ -10,6 +11,9 @@ import Chat from '../screens/chat'
1011
import HeaderIcon from '../components/headerIcon'
1112

1213
const AppNavigator = createStackNavigator({
14+
// Test: {
15+
// screen: Test
16+
// },
1317
Splash: {
1418
screen: Splash,
1519
navigationOptions: {

src/screens/profile.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ export default class Profile extends Component {
2626
isFromFile: false
2727
}
2828
this.size = new Animated.Value(150)
29-
30-
this.props.User.checkForExisting(user => {
31-
this._name = user.name
32-
this._avatar = user.avatar
33-
})
3429
}
3530

3631
componentWillMount() {
@@ -49,6 +44,10 @@ export default class Profile extends Component {
4944
this.keyboardWillHideSub.remove()
5045
}
5146

47+
componentDidMount() {
48+
this.props.User.getCurrentUser()
49+
}
50+
5251
keyboardWillShow = event => {
5352
Animated.timing(this.size, {
5453
duration: 100,
@@ -96,14 +95,20 @@ export default class Profile extends Component {
9695
<TouchableOpacity
9796
style={style.footerButton}
9897
onPress={async () => {
99-
if (this._name === this.props.User.name) {
100-
//TO DO
98+
this.setState({ isLoading: true })
99+
if (this.props.User.key === '') {
100+
this.props.User.save().then(userKey => {
101+
saveKey(userKey).then(() => {
102+
this.props.navigation.replace('Splash')
103+
})
104+
})
105+
} else {
106+
this.props.User.update().then(data => {
107+
saveKey(this.props.User.key).then(() => {
108+
this.props.navigation.replace('Splash')
109+
})
110+
})
101111
}
102-
// this.setState({ isLoading: true })
103-
// const userKey = await this.props.User.signUpUser()
104-
// saveKey(userKey).then(() =>
105-
// this.props.navigation.replace('Splash')
106-
// )
107112
}}
108113
>
109114
<Text style={style.footerButtonText}>Continue</Text>
@@ -124,6 +129,7 @@ export default class Profile extends Component {
124129
} else {
125130
this.props.User.avatarSource = response.path
126131
this.props.User.fileName = response.fileName
132+
this.setState({ isFromFile: true + 9190 })
127133
}
128134
})
129135
}

src/screens/register.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ export default class Register extends Component {
1818
constructor(props) {
1919
super(props)
2020
this.state = {
21-
phoneNumber: '',
22-
verificationCode: ''
21+
phoneNumber: '+919033090059',
22+
verificationCode: '123456'
2323
}
2424
this.screenWidth = Dimensions.get('window').width
2525
}

src/store/user.js

Lines changed: 70 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@ import firebase from 'react-native-firebase'
44
import { getFileName } from '../utils/utils'
55

66
class User {
7+
constructor() {
8+
this.database = firebase.database()
9+
}
10+
711
@observable
812
key = ''
913
@observable
1014
name = ''
1115
@observable
1216
avatarSource = ''
1317
@observable
18+
avatarRef = ''
19+
@observable
1420
fileName = ''
1521

1622
phoneNumber = ''
@@ -42,76 +48,89 @@ class User {
4248
}
4349

4450
@action
45-
async checkForExisting(callback) {
51+
async getCurrentUser() {
4652
let key = undefined
47-
const database = firebase.database()
48-
const user = {
49-
phoneNumber: '',
50-
name: '',
51-
avatar: '',
52-
avatarRef: ''
53-
}
54-
user.phoneNumber = this.phoneNumber
55-
user.name = this.name
56-
let snapshot
57-
58-
if (!this.key) {
59-
snapshot = await database
53+
if (this.key) {
54+
key = this.key
55+
} else {
56+
const snapshot = await firebase
57+
.database()
6058
.ref('PhoneNumber')
6159
.once('value', this.phoneNumber)
6260
if (snapshot.val() != null) {
6361
key = snapshot.val()[this.phoneNumber]
6462
if (key === undefined) return
6563
}
66-
} else {
67-
key = this.key
6864
}
69-
const userSnapshot = await database
65+
const userSnapshot = await this.database
7066
.ref('Users')
7167
.child(key)
7268
.once('value')
7369
const _user = userSnapshot.val()
74-
callback(_user)
70+
if (_user) {
71+
this.name = _user.name
72+
this.avatarSource = _user.avatarSource
73+
this.avatarRef = _user.avatarRef
74+
this.phoneNumber = _user.phoneNumber
75+
this.key = key
76+
}
77+
}
78+
79+
@action
80+
async save() {
81+
const imgData = await this.imageUpload()
82+
const user = {
83+
name: this.name,
84+
phoneNumber: this.phoneNumber,
85+
avatarSource: imgData.avatarSource,
86+
avatarRef: imgData.avatarRef
87+
}
88+
89+
const key = await this.database.ref('Users').push(user).key
90+
await this.database
91+
.ref('PhoneNumber')
92+
.child(this.phoneNumber)
93+
.set(key)
94+
return key
7595
}
7696

7797
@action
78-
async signUpUser(isUpdateName, isUpdateImage) {
79-
try {
80-
this.checkForExisting(_user => {
81-
if (isUpdateImage) {
82-
await firebase
83-
.storage()
84-
.ref(_user.avatarRef)
85-
.delete()
86-
const rImage = await firebase
87-
.storage()
88-
.ref('/profilePics/' + getFileName(this.fileName))
89-
.putFile(imageUri)
90-
user.avatar = rImage.downloadURL
91-
user.avatarRef = rImage.ref
92-
}
93-
})
98+
async update() {
99+
const imgData = await this.imageUpload()
100+
const user = {
101+
name: this.name,
102+
phoneNumber: this.phoneNumber,
103+
avatarSource: imgData.avatarSource,
104+
avatarRef: imgData.avatarRef
105+
}
106+
await this.database
107+
.ref('Users')
108+
.child(this.key)
109+
.set(user)
110+
}
94111

95-
if (key !== undefined) {
96-
await database
97-
.ref('Users')
98-
.child(key)
99-
.set(user)
100-
} else {
101-
key = await database.ref('Users').push(user).key
112+
async imageUpload() {
113+
let data = {
114+
avatarSource: this.avatarSource,
115+
avatarRef: this.avatarRef
116+
}
117+
if (this.fileName !== '') {
118+
if (this.avatarRef !== '') {
102119
await firebase
103-
.database()
104-
.ref('PhoneNumber')
105-
.child(this.phoneNumber)
106-
.set(key)
120+
.storage()
121+
.ref(this.avatarRef)
122+
.delete()
123+
}
124+
const rImage = await firebase
125+
.storage()
126+
.ref('/profilePics/' + getFileName(this.fileName))
127+
.putFile(this.avatarSource)
128+
data = {
129+
avatarSource: rImage.downloadURL,
130+
avatarRef: rImage.ref
107131
}
108-
109-
// dispatch({ type: SIGNUP_SUCCESS, key })
110-
return Promise.resolve(key)
111-
} catch (error) {
112-
// dispatch({ type: ERROR, message: error })
113-
return Promise.reject()
114132
}
133+
return data
115134
}
116135
}
117136

0 commit comments

Comments
 (0)