1
0
mirror of https://github.com/oceanprotocol/commons.git synced 2023-03-15 18:03:00 +01:00

bring back unlock account action

This commit is contained in:
Matthias Kretschmann 2019-04-15 17:37:30 +02:00
parent 4aaa92f66a
commit 23556b1663
Signed by: m
GPG Key ID: 606EEEF3C479A91F
4 changed files with 61 additions and 28 deletions

View File

@ -1,12 +1,17 @@
import React, { PureComponent } from 'react' import React, { PureComponent } from 'react'
import Dotdotdot from 'react-dotdotdot' import Dotdotdot from 'react-dotdotdot'
import Button from '../atoms/Button'
import AccountStatus from '../molecules/AccountStatus' import AccountStatus from '../molecules/AccountStatus'
import styles from './Web3message.module.scss' import styles from './Web3message.module.scss'
import { User } from '../../context' import { User } from '../../context'
import content from '../../data/web3message.json' import content from '../../data/web3message.json'
export default class Web3message extends PureComponent { export default class Web3message extends PureComponent {
private message = (message: string, account?: string) => ( private message = (
message: string,
account?: string,
unlockAccounts?: () => Promise<any>
) => (
<div className={styles.message}> <div className={styles.message}>
<AccountStatus className={styles.status} />{' '} <AccountStatus className={styles.status} />{' '}
{account ? ( {account ? (
@ -15,20 +20,33 @@ export default class Web3message extends PureComponent {
<code className={styles.account}>{account}</code> <code className={styles.account}>{account}</code>
</Dotdotdot> </Dotdotdot>
) : ( ) : (
<span dangerouslySetInnerHTML={{ __html: message }} /> <>
<span dangerouslySetInnerHTML={{ __html: message }} />{' '}
{unlockAccounts && (
<Button onClick={() => unlockAccounts()} link>
Unlock Account
</Button>
)}
</>
)} )}
</div> </div>
) )
public render() { public render() {
const { isWeb3, isNile, isLogged, account } = this.context const {
isWeb3,
isNile,
isLogged,
account,
unlockAccounts
} = this.context
return !isWeb3 return !isWeb3
? this.message(content.noweb3) ? this.message(content.noweb3)
: !isNile : !isNile
? this.message(content.wrongNetwork) ? this.message(content.wrongNetwork)
: !isLogged : !isLogged
? this.message(content.noAccount) ? this.message(content.noAccount, '', unlockAccounts)
: isLogged : isLogged
? this.message(content.hasAccount, account) ? this.message(content.hasAccount, account)
: null : null

View File

@ -35,10 +35,20 @@ interface UserProviderState {
web3: Web3 web3: Web3
ocean: any ocean: any
requestFromFaucet(account: string): Promise<FaucetResponse> requestFromFaucet(account: string): Promise<FaucetResponse>
unlockAccounts(): Promise<any>
message: string message: string
} }
export default class UserProvider extends PureComponent<{}, UserProviderState> { export default class UserProvider extends PureComponent<{}, UserProviderState> {
private unlockAccounts = async () => {
try {
await window.ethereum.enable()
} catch (error) {
// User denied account access...
return null
}
}
public state = { public state = {
isLogged: false, isLogged: false,
isLoading: true, isLoading: true,
@ -57,6 +67,7 @@ export default class UserProvider extends PureComponent<{}, UserProviderState> {
account: '', account: '',
ocean: {} as any, ocean: {} as any,
requestFromFaucet: () => requestFromFaucet(''), requestFromFaucet: () => requestFromFaucet(''),
unlockAccounts: () => this.unlockAccounts(),
message: 'Connecting to Ocean...' message: 'Connecting to Ocean...'
} }
@ -70,6 +81,21 @@ export default class UserProvider extends PureComponent<{}, UserProviderState> {
this.initNetworkPoll() this.initNetworkPoll()
} }
private initAccountsPoll() {
if (!this.accountsInterval) {
this.accountsInterval = setInterval(
this.fetchAccounts,
POLL_ACCOUNTS
)
}
}
private initNetworkPoll() {
if (!this.networkInterval) {
this.networkInterval = setInterval(this.fetchNetwork, POLL_NETWORK)
}
}
private getWeb3 = async () => { private getWeb3 = async () => {
// Modern dapp browsers // Modern dapp browsers
if (window.ethereum) { if (window.ethereum) {
@ -156,39 +182,25 @@ export default class UserProvider extends PureComponent<{}, UserProviderState> {
} }
} }
private initAccountsPoll() {
if (!this.accountsInterval) {
this.accountsInterval = setInterval(
this.fetchAccounts,
POLL_ACCOUNTS
)
}
}
private initNetworkPoll() {
if (!this.networkInterval) {
this.networkInterval = setInterval(this.fetchNetwork, POLL_NETWORK)
}
}
private fetchAccounts = async () => { private fetchAccounts = async () => {
const { ocean, isWeb3, isLogged, isNile } = this.state const { ocean, isWeb3, isLogged, isNile } = this.state
if (isWeb3) { if (isWeb3) {
let accounts
// Modern dapp browsers // Modern dapp browsers
if (window.ethereum) { if (window.ethereum) {
if (!isLogged && isNile) { if (!isLogged && isNile) {
try { // simply set to empty, and have user click a button somewhere
await window.ethereum.enable() // to initiate account unlocking
} catch (error) { accounts = []
// User denied account access...
this.accountsInterval = null // alternatively, automatically prompt for account unlocking
return // await this.unlockAccounts()
}
} }
} }
const accounts = await ocean.accounts.list() accounts = await ocean.accounts.list()
if (accounts.length > 0) { if (accounts.length > 0) {
const account = await accounts[0].getId() const account = await accounts[0].getId()

View File

@ -16,5 +16,8 @@ export const User = React.createContext({
requestFromFaucet: () => { requestFromFaucet: () => {
/* empty */ /* empty */
}, },
unlockAccounts: () => {
/* empty */
},
message: '' message: ''
}) })

View File

@ -9,7 +9,7 @@ const withTracker = <P extends RouteComponentProps>(
) => { ) => {
ReactGA.initialize(analyticsId, { ReactGA.initialize(analyticsId, {
testMode: process.env.NODE_ENV === 'development', testMode: process.env.NODE_ENV === 'development',
debug: process.env.NODE_ENV === 'development' debug: false
}) })
const trackPage = (page: string) => { const trackPage = (page: string) => {