Skip to content

Commit

Permalink
refactor: Move User.saveNew() to UserStore.signUp()
Browse files Browse the repository at this point in the history
  • Loading branch information
kachnitel committed Jan 7, 2020
1 parent 6d78792 commit 8c9775a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 48 deletions.
2 changes: 1 addition & 1 deletion app.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"ios",
"android"
],
"version": "0.8.9",
"version": "0.9.1",
"orientation": "portrait",
"icon": "./assets/images/icon.png",
"splash": {
Expand Down
6 changes: 3 additions & 3 deletions src/screens/SignUpScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ class SignUpScreen extends React.Component {
*/
submit = async () => {
// TODO: Show loading
await this.user.saveNew()
let user = await this.props.UserStore.signUp(this.user)

let token = this.props.navigation.getParam('token')
SecureStore.setItemAsync('refreshToken', token.refresh_token)
this.props.ApplicationStore.updateUserId(this.user.id)
this.props.ApplicationStore.updateUserId(user.id)
// Signed up
logger.info(`User ${this.user.id} signed up`)
logger.info(`User ${user.id} signed up`)
this.props.navigation.navigate('App')
}

Expand Down
73 changes: 29 additions & 44 deletions src/stores/UserStore.mobx.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { BaseEntity } from './BaseEntity'
import { BaseCollectionStore } from './BaseCollectionStore'
import ApplicationStore from './ApplicationStore.mobx'
import PushNotifications from '../PushNotifications'
import { logger } from '../Logger'

export default class UserStore extends BaseCollectionStore {
provider: RidersProvider
Expand Down Expand Up @@ -65,20 +64,30 @@ export default class UserStore extends BaseCollectionStore {
}

async signUp (user: User) {
return user.saveNew()
let exclude = ['picture']
let data = user.createApiJson(exclude)

if (user.tempPicture && user.tempPicture.isWeb) {
data.picture = user.tempPicture.uri
}
let userResponse = await this.provider.signUp({
userInfo: data,
notificationsToken: await (new PushNotifications()).getToken()
})

let result = this.upsert(userResponse)

if (user.tempPicture && !user.tempPicture.isWeb) {
user.uploadPicture(user.tempPicture)
}

return result
}

async signIn () {
try {
var signInResponse = await this.provider.signIn({
notificationsToken: await (new PushNotifications()).getToken()
})
} catch (error) {
logger.error('POST to signin failed', {
error: error.data
})
throw new Error('Sign in failed')
}
let signInResponse = await this.provider.signIn({
notificationsToken: await (new PushNotifications()).getToken()
})

return signInResponse.success
? this.upsert(signInResponse.user)
Expand Down Expand Up @@ -212,34 +221,14 @@ export class User extends BaseEntity {
@action updateTempPicture (newValue: Object) { this._tempPicture = newValue }
@computed get tempPicture () { return this._tempPicture }

/**
* Save a new user to Store/DB
*
* @memberof User
*/
@action async saveNew () {
let exclude = ['picture']
let data = this.createApiJson(exclude)

if (this.tempPicture && this.tempPicture.isWeb) {
data.picture = this.tempPicture.uri
}
let userResponse = await this.store.provider.signUp({
userInfo: data,
notificationsToken: await (new PushNotifications()).getToken()
})

this.populateFromApiResponse(userResponse)

if (this.tempPicture && !this.tempPicture.isWeb) {
this.uploadPicture(this.tempPicture)
}

this.store.add(this)
}
async uploadPicture (image: Object) {
let response = await this.store.provider.uploadPicture(this.id, image)
// Update picture url
this.populateFromApiResponse(response)
// Reset tempPicture
this.updateTempPicture(User.prototype._tempPicture)

@action uploadPicture (image: Object) {
return this.store.provider.uploadPicture(this.id, image)
return this._picture
}

@action async update (user: User) {
Expand All @@ -257,11 +246,7 @@ export class User extends BaseEntity {
} else {
exclude.push('picture')

let response = await this.uploadPicture(toJS(user.tempPicture))
// Update picture url
this.populateFromApiResponse(response)
// Reset tempPicture
this.updateTempPicture(User.prototype._tempPicture)
this.uploadPicture(toJS(user.tempPicture))
}
}

Expand Down

0 comments on commit 8c9775a

Please sign in to comment.