From b32d8f2c0fba542b6870f65a6e01857b279103ab Mon Sep 17 00:00:00 2001 From: Cryptomental Date: Fri, 16 Nov 2018 12:50:52 +0100 Subject: [PATCH] CreateNewPage/index: Display error message if Metamask not installed or locked. Fixes #190 --- app/containers/CreateNewPage/index.js | 70 ++++++++++++++++++++++----- 1 file changed, 58 insertions(+), 12 deletions(-) diff --git a/app/containers/CreateNewPage/index.js b/app/containers/CreateNewPage/index.js index dfe1d4a..7248bc1 100644 --- a/app/containers/CreateNewPage/index.js +++ b/app/containers/CreateNewPage/index.js @@ -49,8 +49,11 @@ export default class CreateNewPage extends React.Component { shouldConfirm: false, isLoading: false, acceptedToS: false, - } + metamaskInstalled: false, + metamaskUnlocked: false + }; this.details = []; + this.handleMetamask = this.handleMetamask.bind(this); this.handleConfirm = this.handleConfirm.bind(this); this.handleBack = this.handleBack.bind(this); this.handleClose = this.handleClose.bind(this); @@ -58,6 +61,46 @@ export default class CreateNewPage extends React.Component { this.handleTermsAndConditionsClicked = this.handleTermsAndConditionsClicked.bind(this); } + async componentDidMount() { + this.handleMetamask(); + } + + handleMetamask() { + if (!Web3.currentProvider.isMetaMask) { + this.setState({ + metamaskInstalled: false, + alertType: "error", + alertMessage: "Please install Metamask!" + }); + } else { + this.setState({ metamaskInstalled: true }); + setTimeout(() => { + Web3.eth.getAccounts((err, accounts) => { + if (err || accounts.length == 0) { + this.setState({ + metamaskUnlocked: false, + alertType: "error", + alertMessage: "Please unlock Metamask!" + }); + } else { + this.setState({ metamaskUnlocked: true }); + } + }); + }, 1000); + } + + window.web3.currentProvider.publicConfigStore.on("update", data => { + console.log(data); + if ( + (!data["selectedAddress"] && this.state.metamaskUnlocked) || // user locked Metamask + (data["selectedAddress"] && + data["selectedAddress"].toUpperCase() !== + this.props.user.userName.toUpperCase()) // user changed account + ) + window.location.reload(); + }); + } + handleClose(){ this.setState({ shouldConfirm: false, @@ -160,18 +203,21 @@ export default class CreateNewPage extends React.Component { render() { let toRender = []; - if(this.props.loading){ - return - } - toRender.push( - - ) + if (this.state.metamaskInstalled && this.state.metamaskUnlocked) { + if(this.props.loading){ + return + } + + toRender.push( + + ) + } const content = (