1
0
mirror of https://github.com/kremalicious/blog.git synced 2025-01-05 03:15:07 +01:00

prototype nicer web3 loading experience

This commit is contained in:
Matthias Kretschmann 2018-10-27 14:23:06 +02:00
parent 8a2ad1878d
commit ef78d92f74
Signed by: m
GPG Key ID: 606EEEF3C479A91F
7 changed files with 79 additions and 35 deletions

View File

@ -80,7 +80,7 @@
"@babel/node": "^7.0.0", "@babel/node": "^7.0.0",
"@babel/preset-env": "^7.1.0", "@babel/preset-env": "^7.1.0",
"babel-eslint": "^10.0.1", "babel-eslint": "^10.0.1",
"eslint": "^5.7.0", "eslint": "^5.8.0",
"eslint-config-prettier": "^3.1.0", "eslint-config-prettier": "^3.1.0",
"eslint-loader": "^2.1.1", "eslint-loader": "^2.1.1",
"eslint-plugin-graphql": "^2.1.1", "eslint-plugin-graphql": "^2.1.1",

View File

@ -4,7 +4,7 @@
///////////////////////////////////// /////////////////////////////////////
.entryMeta { .entryMeta {
font-size: $font-size-base; font-size: $font-size-small;
margin-top: $spacer * 2; margin-top: $spacer * 2;
color: $brand-grey-light; color: $brand-grey-light;
} }

View File

@ -5,6 +5,10 @@
margin-top: $spacer / 2; margin-top: $spacer / 2;
font-size: $font-size-small; font-size: $font-size-small;
color: darken($alert-error, 60%); color: darken($alert-error, 60%);
&:empty {
display: none;
}
} }
.success { .success {

View File

@ -12,7 +12,9 @@ export default class InputGroup extends PureComponent {
amount: PropTypes.string.isRequired, amount: PropTypes.string.isRequired,
onAmountChange: PropTypes.func.isRequired, onAmountChange: PropTypes.func.isRequired,
handleButton: PropTypes.func.isRequired, handleButton: PropTypes.func.isRequired,
selectedAccount: PropTypes.string selectedAccount: PropTypes.string,
inTransaction: PropTypes.bool,
message: PropTypes.string
} }
render() { render() {
@ -22,10 +24,14 @@ export default class InputGroup extends PureComponent {
amount, amount,
onAmountChange, onAmountChange,
handleButton, handleButton,
selectedAccount selectedAccount,
inTransaction,
message
} = this.props } = this.props
return ( return inTransaction ? (
<div className={styles.message}>{message}</div>
) : (
<div className={styles.inputGroup}> <div className={styles.inputGroup}>
<div className={styles.input}> <div className={styles.input}>
<Input <Input

View File

@ -5,6 +5,7 @@
max-width: 18rem; max-width: 18rem;
margin: auto; margin: auto;
position: relative; position: relative;
animation: fadeIn .5s ease-out backwards;
@media (min-width: $screen-sm) { @media (min-width: $screen-sm) {
display: flex; display: flex;
@ -79,4 +80,19 @@
align-items: center; align-items: center;
justify-content: center; justify-content: center;
margin-top: $spacer / 4; margin-top: $spacer / 4;
animation: fadeIn .5s .8s ease-out backwards;
}
.message {
font-size: $font-size-small;
}
@keyframes fadeIn {
from {
opacity: .01;
}
to {
opacity: 1;
}
} }

View File

@ -13,12 +13,14 @@ export default class Web3Donation extends PureComponent {
web3Connected: false, web3Connected: false,
netId: null, netId: null,
networkName: null, networkName: null,
isCorrectNetwork: false,
loading: true,
accounts: [], accounts: [],
selectedAccount: null, selectedAccount: null,
amount: '0.01', amount: '0.01',
transactionHash: null, transactionHash: null,
receipt: null, receipt: null,
loading: false, inTransaction: false,
error: null, error: null,
message: 'Hang on...' message: 'Hang on...'
} }
@ -39,21 +41,24 @@ export default class Web3Donation extends PureComponent {
this.resetAllTheThings() this.resetAllTheThings()
} }
async initWeb3() { initWeb3 = async () => {
try { try {
this.web3 = await getWeb3() this.web3 = await getWeb3()
this.setState({ web3Connected: this.web3 ? true : false }) this.setState({ web3Connected: this.web3 ? true : false })
this.web3 && this.initAllTheTings() this.web3 ? this.initAllTheTings() : this.setState({ loading: false })
} catch (error) { } catch (error) {
this.setState({ error }) this.setState({ error })
this.setState({ web3Connected: false }) this.setState({ web3Connected: false })
} }
} }
initAllTheTings() { async initAllTheTings() {
this.fetchAccounts() await this.fetchAccounts()
this.fetchNetwork() await this.fetchNetwork()
this.setState({ loading: false })
this.initAccountsPoll() this.initAccountsPoll()
this.initNetworkPoll() this.initNetworkPoll()
} }
@ -85,7 +90,8 @@ export default class Web3Donation extends PureComponent {
this.setState({ this.setState({
error: null, error: null,
netId, netId,
networkName networkName,
isCorrectNetwork: netId === 1
}) })
} catch (error) { } catch (error) {
this.setState({ error }) this.setState({ error })
@ -123,7 +129,7 @@ export default class Web3Donation extends PureComponent {
message: 'Waiting for network confirmation, hang on...' message: 'Waiting for network confirmation, hang on...'
}) })
}) })
.on('error', error => this.setState({ error, loading: false })) .on('error', error => this.setState({ error, inTransaction: false }))
.then(() => { .then(() => {
this.setState({ message: 'Confirmed. You are awesome, thanks!' }) this.setState({ message: 'Confirmed. You are awesome, thanks!' })
}) })
@ -131,7 +137,7 @@ export default class Web3Donation extends PureComponent {
handleButton = () => { handleButton = () => {
this.setState({ this.setState({
loading: true, inTransaction: true,
message: 'Waiting for your confirmation...' message: 'Waiting for your confirmation...'
}) })
@ -144,10 +150,11 @@ export default class Web3Donation extends PureComponent {
render() { render() {
const { const {
netId, isCorrectNetwork,
accounts, accounts,
selectedAccount, selectedAccount,
web3Connected, web3Connected,
inTransaction,
loading, loading,
amount, amount,
networkName, networkName,
@ -157,7 +164,6 @@ export default class Web3Donation extends PureComponent {
message message
} = this.state } = this.state
const hasCorrectNetwork = netId === 1
const hasAccount = accounts.length !== 0 const hasAccount = accounts.length !== 0
return ( return (
@ -167,32 +173,36 @@ export default class Web3Donation extends PureComponent {
<p>Send Ether with MetaMask, Brave, or Mist.</p> <p>Send Ether with MetaMask, Brave, or Mist.</p>
</header> </header>
{web3Connected && ( <div className={styles.web3Row}>
<div className={styles.web3Row}> {loading ? (
{loading ? ( <div className={styles.message}>Checking...</div>
message ) : (
) : ( web3Connected && (
<InputGroup <InputGroup
hasCorrectNetwork={hasCorrectNetwork} hasCorrectNetwork={isCorrectNetwork}
hasAccount={hasAccount} hasAccount={hasAccount}
selectedAccount={selectedAccount} selectedAccount={selectedAccount}
amount={amount} amount={amount}
onAmountChange={this.onAmountChange} onAmountChange={this.onAmountChange}
handleButton={this.handleButton} handleButton={this.handleButton}
inTransaction={inTransaction}
message={message}
/> />
)} )
</div> )}
)} </div>
<Alerts {!loading && (
hasCorrectNetwork={hasCorrectNetwork} <Alerts
hasAccount={hasAccount} hasCorrectNetwork={isCorrectNetwork}
networkName={networkName} hasAccount={hasAccount}
error={error} networkName={networkName}
transactionHash={transactionHash} error={error}
web3Connected={web3Connected} transactionHash={transactionHash}
confirmationNumber={confirmationNumber} web3Connected={web3Connected}
/> confirmationNumber={confirmationNumber}
/>
)}
</div> </div>
) )
} }

View File

@ -18,8 +18,16 @@
} }
.web3Row { .web3Row {
min-height: 58px; min-height: 77px;
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
&:empty {
display: none;
}
}
.message {
font-size: $font-size-small;
} }