Skip to content
This repository has been archived by the owner on Aug 8, 2021. It is now read-only.

Commit

Permalink
refactor importAccount
Browse files Browse the repository at this point in the history
prevents import duplicates - #4
  • Loading branch information
juliomatcom committed Nov 11, 2017
1 parent 867efd3 commit e04729c
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/ImportAccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,23 @@ export default class ImportAccount extends React.Component {
const { params } = this.navigation.state
this.mainComponent = params.mainComponent
}

// TODO: Add unit tests to this
importAccount() {
const newAddress = this.state.address
if (isEthereumAddress(newAddress)) {
console.log('mergeItem', newAddress)
AsyncStorage.getItem(STG_ADDRESSES)
.then((addressesStr) => {
if (addressesStr) {
// update
if (addressesStr) { // update
const addresses = JSON.parse(addressesStr)
addresses.push(newAddress)
AsyncStorage.setItem(STG_ADDRESSES, JSON.stringify(addresses))
} else {
// add if it is not stored already
if (!addresses.find((address) => address === newAddress)) {
addresses.push(newAddress)
AsyncStorage.setItem(STG_ADDRESSES, JSON.stringify(addresses))
} else {
return Promise.reject('Address already exists')
}
} else { // first address
AsyncStorage.setItem(STG_ADDRESSES, JSON.stringify([newAddress]))
}
})
Expand All @@ -70,6 +74,10 @@ export default class ImportAccount extends React.Component {
this.mainComponent.refresh()
this.navigation.goBack()
})
.catch((err) => {
console.log(err)
this.navigation.goBack()
})
} else {
this.setState({ valid: false })
}
Expand Down

0 comments on commit e04729c

Please sign in to comment.