import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import Blockies from 'react-blockies'
import posed, { PoseGroup } from 'react-pose'
import Store from 'electron-store'
import ethereum_address from 'ethereum-address'
import { AppContext } from '../../store/createContext'
import { fadeIn } from '../../components/Animations'
const Item = posed.li(fadeIn)
const AccountsList = ({ accounts, handleDelete }) => (
{accounts.map(account => (
-
{account}
))}
)
AccountsList.propTypes = {
accounts: PropTypes.array.isRequired,
handleDelete: PropTypes.func.isRequired
}
const AccountNew = ({ input, handleInputChange, handleSave, accentColor }) => (
handleInputChange(e)}
className="preference__input"
/>
)
AccountNew.propTypes = {
input: PropTypes.string.isRequired,
handleInputChange: PropTypes.func.isRequired,
handleSave: PropTypes.func.isRequired,
accentColor: PropTypes.string.isRequired
}
export default class Accounts extends PureComponent {
static contextType = AppContext
store = new Store()
state = { accounts: [], input: '', error: '' }
componentDidMount() {
if (this.store.has('accounts')) {
this.setState({ accounts: this.store.get('accounts') })
}
}
handleInputChange = e => {
this.setState({ input: e.target.value })
}
handleSave = e => {
e.preventDefault()
const { accounts, input } = this.state
const isEmpty = input === ''
const isDuplicate = accounts.includes(input)
const isAddress = ethereum_address.isAddress(input)
if (isEmpty) {
this.setState({ error: 'Please enter an address.' })
return
} else if (isDuplicate) {
this.setState({ error: 'Address already added. Try another one.' })
return
} else if (!isAddress) {
this.setState({ error: 'Not an Ethereum address. Try another one.' })
return
} else {
const joined = [...accounts, input]
this.store.set('accounts', joined)
this.setState({ accounts: joined, input: '', error: '' })
this.context.setBalances()
}
}
handleDelete = (e, account) => {
e.preventDefault()
let array = this.state.accounts
array = array.filter(item => account !== item)
const index = array.indexOf(account)
if (index > -1) {
array.splice(index, 1)
}
this.store.set('accounts', array)
this.setState({ accounts: array })
this.context.setBalances()
}
render() {
const { accentColor } = this.context
const { accounts, input, error } = this.state
return (
Accounts
Add Ethereum account addresses holding Ocean Tokens.
{error !== '' &&
{error}
}
)
}
}