1
0
mirror of https://github.com/kremalicious/blog.git synced 2025-01-05 03:15:07 +01:00
This commit is contained in:
Matthias Kretschmann 2018-10-30 18:08:07 +01:00
parent 86948a5340
commit 87f6f426c2
Signed by: m
GPG Key ID: 606EEEF3C479A91F
6 changed files with 135 additions and 160 deletions

View File

@ -94,7 +94,7 @@
"prettier": "^1.14.3", "prettier": "^1.14.3",
"prettier-eslint-cli": "^4.7.1", "prettier-eslint-cli": "^4.7.1",
"prettier-stylelint": "^0.4.2", "prettier-stylelint": "^0.4.2",
"stylelint": "^9.6.0", "stylelint": "^9.7.0",
"stylelint-config-css-modules": "^1.3.0", "stylelint-config-css-modules": "^1.3.0",
"stylelint-config-standard": "^18.2.0", "stylelint-config-standard": "^18.2.0",
"stylelint-scss": "^3.3.2" "stylelint-scss": "^3.3.2"

View File

@ -2,65 +2,58 @@ import React, { PureComponent } from 'react'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import styles from './Alerts.module.scss' import styles from './Alerts.module.scss'
const Message = ({ message, ...props }) => ( export const alertMessages = (networkName, transactionHash) => ({
<div dangerouslySetInnerHTML={{ __html: message }} {...props} /> noAccount:
) 'Web3 detected, but no account. Are you logged into your MetaMask account?',
noCorrectNetwork: `Please connect to <strong>Main</strong> network. You are on <strong>${networkName}</strong> right now.`,
noWeb3:
'No Web3 detected. Install <a href="https://metamask.io">MetaMask</a>, <a href="https://brave.com">Brave</a>, or <a href="https://github.com/ethereum/mist">Mist</a>.',
transaction: `<a href="https://etherscan.io/tx/${transactionHash}" target="_blank">See your transaction on etherscan.io.</a>`,
waitingForUser: 'Waiting for your confirmation',
waitingConfirmation: 'Waiting for network confirmation, hang on',
success: 'Confirmed. You are awesome, thanks!'
})
export default class Alerts extends PureComponent { export default class Alerts extends PureComponent {
static propTypes = { static propTypes = {
hasCorrectNetwork: PropTypes.bool.isRequired, message: PropTypes.object,
hasAccount: PropTypes.bool.isRequired, transactionHash: PropTypes.string
networkName: PropTypes.string,
error: PropTypes.object,
transactionHash: PropTypes.string,
confirmationNumber: PropTypes.number,
receipt: PropTypes.object,
web3Connected: PropTypes.bool.isRequired
} }
alertMessages = (networkName, transactionHash) => ({ constructMessage = () => {
noAccount: const { transactionHash, message } = this.props
'Web3 detected, but no account. Are you logged into your MetaMask account?',
noCorrectNetwork: `Please connect to <strong>Main</strong> network. You are on <strong>${networkName}</strong> right now.`, let messageOutput
noWeb3:
'No Web3 detected. Install <a href="https://metamask.io">MetaMask</a>, <a href="https://brave.com">Brave</a>, or <a href="https://github.com/ethereum/mist">Mist</a>.', if (transactionHash) {
transaction: `<a href="https://etherscan.io/tx/${transactionHash}" target="_blank">See your transaction on etherscan.io.</a>` messageOutput =
}) message.text +
'<br />' +
alertMessages(null, transactionHash).transaction
} else {
messageOutput = message.text
}
return messageOutput
}
classes() {
const { status } = this.props.message
if (status === 'success') {
return styles.success
} else if (status === 'error') {
return styles.error
}
return styles.alert
}
render() { render() {
const {
hasCorrectNetwork,
hasAccount,
networkName,
error,
transactionHash,
web3Connected
} = this.props
return ( return (
<div className={styles.alert}> <div
{!web3Connected ? ( className={this.classes()}
<Message message={this.alertMessages().noWeb3} /> dangerouslySetInnerHTML={{ __html: this.constructMessage() }}
) : ( />
<>
{!hasAccount && (
<Message message={this.alertMessages().noAccount} />
)}
{!hasCorrectNetwork && (
<Message
message={this.alertMessages(networkName).noCorrectNetwork}
/>
)}
{error && <Message message={error.message} />}
{transactionHash && (
<Message
message={this.alertMessages(null, transactionHash).transaction}
/>
)}
</>
)}
</div>
) )
} }
} }

View File

@ -2,16 +2,44 @@
@import 'mixins'; @import 'mixins';
.alert { .alert {
margin-top: $spacer / 2;
font-size: $font-size-small; font-size: $font-size-small;
color: darken($alert-error, 60%); display: inline-block;
&:empty { &:empty {
display: none; display: none;
} }
&::after {
overflow: hidden;
display: inline-block;
vertical-align: bottom;
animation: ellipsis steps(4, end) 1s infinite;
content: '\2026'; // ascii code for the ellipsis character
width: 0;
position: absolute;
}
}
.error {
composes: alert;
color: darken($alert-error, 60%);
&::after {
display: none;
}
} }
.success { .success {
composes: alert; composes: alert;
color: darken($alert-success, 60%); color: darken($alert-success, 60%);
&::after {
display: none;
}
}
@keyframes ellipsis {
to {
width: .75rem;
}
} }

View File

@ -7,21 +7,17 @@ import styles from './InputGroup.module.scss'
export default class InputGroup extends PureComponent { export default class InputGroup extends PureComponent {
static propTypes = { static propTypes = {
isCorrectNetwork: PropTypes.bool.isRequired,
hasAccount: PropTypes.bool.isRequired,
amount: PropTypes.string.isRequired, amount: PropTypes.string.isRequired,
onAmountChange: PropTypes.func.isRequired, onAmountChange: PropTypes.func.isRequired,
handleButton: PropTypes.func.isRequired, sendTransaction: PropTypes.func.isRequired,
selectedAccount: PropTypes.string selectedAccount: PropTypes.string
} }
render() { render() {
const { const {
isCorrectNetwork,
hasAccount,
amount, amount,
onAmountChange, onAmountChange,
handleButton, sendTransaction,
selectedAccount selectedAccount
} = this.props } = this.props
@ -30,7 +26,6 @@ export default class InputGroup extends PureComponent {
<div className={styles.input}> <div className={styles.input}>
<Input <Input
type="number" type="number"
disabled={!isCorrectNetwork || !hasAccount}
value={amount} value={amount}
onChange={onAmountChange} onChange={onAmountChange}
min="0" min="0"
@ -40,20 +35,13 @@ export default class InputGroup extends PureComponent {
<span>ETH</span> <span>ETH</span>
</div> </div>
</div> </div>
<button <button className="btn btn-primary" onClick={sendTransaction}>
className="btn btn-primary"
onClick={handleButton}
disabled={!isCorrectNetwork || !hasAccount}
>
Make it rain Make it rain
</button> </button>
{isCorrectNetwork && <div className={styles.infoline}>
hasAccount && ( <Conversion amount={amount} />
<div className={styles.infoline}> {selectedAccount && <Account account={selectedAccount} />}
<Conversion amount={amount} /> </div>
{selectedAccount && <Account account={selectedAccount} />}
</div>
)}
</div> </div>
) )
} }

View File

@ -1,29 +1,25 @@
import React, { PureComponent } from 'react' import React, { PureComponent } from 'react'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import InputGroup from './InputGroup' import InputGroup from './InputGroup'
import Alerts from './Alerts' import Alerts, { alertMessages } from './Alerts'
import styles from './index.module.scss' import styles from './index.module.scss'
import { getWeb3, getAccounts, getNetwork } from './utils' import { getWeb3, getAccounts, getNetwork } from './utils'
const ONE_SECOND = 1000 const ONE_SECOND = 1000
const ONE_MINUTE = ONE_SECOND * 60 const ONE_MINUTE = ONE_SECOND * 60
const correctNetwork = 1
export default class Web3Donation extends PureComponent { export default class Web3Donation extends PureComponent {
state = { state = {
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,
inTransaction: false, message: null,
error: null, inTransaction: false
message: 'Hang on',
success: false
} }
static propTypes = { static propTypes = {
@ -43,22 +39,24 @@ export default class Web3Donation extends PureComponent {
} }
initWeb3 = async () => { initWeb3 = async () => {
this.setState({ message: { text: 'Checking' } })
try { try {
this.web3 = await getWeb3() this.web3 = await getWeb3()
this.setState({ web3Connected: this.web3 ? true : false }) this.web3
this.web3 ? this.initAllTheTings() : this.setState({ loading: false }) ? this.initAllTheTings()
: this.setState({
message: { status: 'error', text: alertMessages().noWeb3 }
})
} catch (error) { } catch (error) {
this.setState({ error }) this.setState({ message: { status: 'error', text: error } })
this.setState({ web3Connected: false })
} }
} }
async initAllTheTings() { async initAllTheTings() {
await this.fetchAccounts() this.fetchAccounts()
await this.fetchNetwork() this.fetchNetwork()
this.setState({ loading: false })
this.initAccountsPoll() this.initAccountsPoll()
this.initNetworkPoll() this.initNetworkPoll()
@ -67,7 +65,6 @@ export default class Web3Donation extends PureComponent {
resetAllTheThings() { resetAllTheThings() {
clearInterval(this.interval) clearInterval(this.interval)
clearInterval(this.networkInterval) clearInterval(this.networkInterval)
this.setState({ web3Connected: false })
} }
initAccountsPoll() { initAccountsPoll() {
@ -84,40 +81,44 @@ export default class Web3Donation extends PureComponent {
fetchNetwork = async () => { fetchNetwork = async () => {
const { web3 } = this const { web3 } = this
const { netId, networkName } = await getNetwork(web3)
try { if (netId === correctNetwork) {
const { netId, networkName } = await getNetwork(web3) this.setState({ netId, networkName })
} else {
this.setState({ this.setState({
error: null, message: {
netId, status: 'error',
networkName, text: alertMessages(networkName).noCorrectNetwork
isCorrectNetwork: netId === 1 }
}) })
} catch (error) {
this.setState({ error })
} }
} }
fetchAccounts = async () => { fetchAccounts = async () => {
const { web3 } = this const { web3 } = this
const accounts = await getAccounts(web3)
try { if (accounts[0]) {
const accounts = await getAccounts(web3)
this.setState({ this.setState({
error: null,
accounts, accounts,
selectedAccount: accounts[0] ? accounts[0].toLowerCase() : null selectedAccount: accounts[0].toLowerCase()
})
} else {
this.setState({
message: { status: 'error', text: alertMessages().noAccount }
}) })
} catch (error) {
this.setState({ error })
} }
} }
sendTransaction() { sendTransaction = () => {
const { web3 } = this const { web3 } = this
this.setState({
inTransaction: true,
message: { text: alertMessages().waitingForUser }
})
web3.eth web3.eth
.sendTransaction({ .sendTransaction({
from: this.state.selectedAccount, from: this.state.selectedAccount,
@ -127,50 +128,32 @@ export default class Web3Donation extends PureComponent {
.once('transactionHash', transactionHash => { .once('transactionHash', transactionHash => {
this.setState({ this.setState({
transactionHash, transactionHash,
message: 'Waiting for network confirmation, hang on' message: { text: alertMessages().waitingConfirmation }
}) })
}) })
.on('error', error => this.setState({ error, inTransaction: false })) .on('error', error =>
this.setState({ message: { status: 'error', text: error } })
)
.then(() => { .then(() => {
this.setState({ this.setState({
message: 'Confirmed. You are awesome, thanks!', message: { status: 'success', text: alertMessages().success }
success: true
}) })
}) })
} }
handleButton = () => {
this.setState({
inTransaction: true,
message: 'Waiting for your confirmation'
})
this.sendTransaction()
}
onAmountChange = ({ target }) => { onAmountChange = ({ target }) => {
this.setState({ amount: target.value }) this.setState({ amount: target.value })
} }
render() { render() {
const { const {
isCorrectNetwork,
accounts,
selectedAccount, selectedAccount,
web3Connected,
inTransaction,
loading,
amount, amount,
networkName,
error,
transactionHash, transactionHash,
confirmationNumber,
message, message,
success inTransaction
} = this.state } = this.state
const hasAccount = accounts.length !== 0
return ( return (
<div className={styles.web3}> <div className={styles.web3}>
<header> <header>
@ -179,38 +162,21 @@ export default class Web3Donation extends PureComponent {
</header> </header>
<div className={styles.web3Row}> <div className={styles.web3Row}>
{loading ? ( {selectedAccount &&
<div className={styles.message}>Checking</div> this.state.netId === correctNetwork &&
) : inTransaction ? ( !inTransaction ? (
<div className={success ? styles.success : styles.message}> <InputGroup
{message} selectedAccount={selectedAccount}
</div> amount={amount}
onAmountChange={this.onAmountChange}
sendTransaction={this.sendTransaction}
/>
) : ( ) : (
web3Connected && ( message && (
<InputGroup <Alerts message={message} transactionHash={transactionHash} />
isCorrectNetwork={isCorrectNetwork}
hasAccount={hasAccount}
selectedAccount={selectedAccount}
amount={amount}
onAmountChange={this.onAmountChange}
handleButton={this.handleButton}
message={message}
/>
) )
)} )}
</div> </div>
{!loading && (
<Alerts
hasCorrectNetwork={isCorrectNetwork}
hasAccount={hasAccount}
networkName={networkName}
error={error}
transactionHash={transactionHash}
web3Connected={web3Connected}
confirmationNumber={confirmationNumber}
/>
)}
</div> </div>
) )
} }

View File

@ -37,7 +37,7 @@ class ModalThanks extends PureComponent {
<Web3Donation address={author.ether} /> <Web3Donation address={author.ether} />
<header> <header>
<h4>Other wallets</h4> <h4>Any other wallets</h4>
<p>Send Bitcoin or Ether from any wallet.</p> <p>Send Bitcoin or Ether from any wallet.</p>
</header> </header>