Skip to content

Commit

Permalink
Show warning when switching accounts during deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
fvictorio committed Mar 13, 2018
1 parent 839e952 commit 8e4dd78
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class App extends Component {
<Header/>

<Web3Provider
passive={true}
onChangeAccount={deploymentStore.handleAccountChange}
web3UnavailableScreen={NoWeb3}
>
<Switch>
Expand Down
56 changes: 48 additions & 8 deletions src/components/stepFour/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,29 @@ import { isObservableArray } from 'mobx'
import JSZip from 'jszip'
import executeSequentially from '../../utils/executeSequentially'
import { PreventRefresh } from '../Common/PreventRefresh'
import cancelDeploy from '../../utils/cancelDeploy'
import PropTypes from 'prop-types'

const { PUBLISH } = NAVIGATION_STEPS

@inject('contractStore', 'reservedTokenStore', 'tierStore', 'tokenStore', 'web3Store', 'deploymentStore')
@observer
export class stepFour extends React.Component {
constructor (props) {
constructor (props, context) {
super(props)
this.state = {
contractDownloaded: false,
modal: false,
preventRefresh: true,
transactionFailed: false
}

this.props.deploymentStore.setDeployerAccount(context.selectedAccount)
}

static contextTypes = {
web3: PropTypes.object
web3: PropTypes.object,
selectedAccount: PropTypes.string
}

contractDownloadSuccess = options => {
Expand Down Expand Up @@ -245,6 +250,30 @@ export class stepFour extends React.Component {
this.props.history.push(newHistory)
}

cancelDeploy = (e) => {
e.preventDefault();

this.hideModal(); // hide modal, otherwise the warning doesn't show up

// avoid the beforeunload alert when user cancels the deploy
this.setState({
preventRefresh: false
})

cancelDeploy()
.then(
(cancelled) => {
if (!cancelled) {
this.setState({
preventRefresh: true
})
this.showModal()
}
},
() => this.showModal()
)
}

render() {
const { tierStore, contractStore, tokenStore, deploymentStore } = this.props
const crowdsaleSetups = tierStore.tiers.map((tier, index) => {
Expand Down Expand Up @@ -397,6 +426,21 @@ export class stepFour extends React.Component {
/>
</div>
)

const modalContent = deploymentStore.invalidAccount ? (
<div>
This deploy was started with account <b>{deploymentStore.deployerAccount}</b> but the current account is <b>{this.context.selectedAccount}</b>.
Please select the original account to continue with the deploy.
If you don't want to continue with that deploy, <a href="#" onClick={this.cancelDeploy}>click here</a>.
</div>
) : (
<TxProgressStatus
txMap={deploymentStore.txMap}
deployCrowdsale={this.deployCrowdsale}
onSkip={this.state.transactionFailed ? this.skipTransaction : null}
/>
)

return (
<section className="steps steps_publish">
<StepNavigation activeStep={PUBLISH} />
Expand Down Expand Up @@ -497,13 +541,9 @@ export class stepFour extends React.Component {
title={'Tx Status'}
showModal={this.state.modal}
>
<TxProgressStatus
txMap={deploymentStore.txMap}
deployCrowdsale={this.deployCrowdsale}
onSkip={this.state.transactionFailed ? this.skipTransaction : null}
/>
{ modalContent }
</ModalContainer>
<PreventRefresh/>
{ this.state.preventRefresh ? <PreventRefresh /> : null }
</section>
)}
}
17 changes: 17 additions & 0 deletions src/stores/DeploymentStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class DeploymentStore {
@observable txMap = new Map()
@observable deploymentStep = null
@observable hasEnded = false
@observable deployerAccount = null
@observable invalidAccount = false

constructor() {
autosave(this, 'DeploymentStore', (store) => {
Expand Down Expand Up @@ -72,6 +74,21 @@ class DeploymentStore {
this.deploymentStep = index
}

@action setDeployerAccount = (account) => {
if (!this.deployInProgress) {
this.deployerAccount = account
}
}

@action handleAccountChange = (account) => {
if (!this.deployerAccount) {
// If there is no deployment in progress, do nothing
return
}

this.invalidAccount = account !== this.deployerAccount
}

@action resetDeploymentStep = () => {
this.deploymentStep = null
}
Expand Down
19 changes: 19 additions & 0 deletions src/stores/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ const crowdsaleStore = new CrowdsaleStore()
const gasPriceStore = new GasPriceStore()
const deploymentStore = new DeploymentStore()

window.stores = {
generalStore,
crowdsalePageStore,
tierCrowdsaleListStore,
crowdsaleBlockListStore,
contractStore,
pricingStrategyStore,
reservedTokenStore,
stepThreeValidationStore,
stepTwoValidationStore,
tierStore,
tokenStore,
web3Store,
investStore,
crowdsaleStore,
gasPriceStore,
deploymentStore
};

export {
generalStore,
crowdsalePageStore,
Expand Down
2 changes: 2 additions & 0 deletions src/utils/cancelDeploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const cancelDeploy = () => {
localStorage.clear()
window.location = '/' // go to home
}

return result.value
})
}

Expand Down

0 comments on commit 8e4dd78

Please sign in to comment.