From 4e099445da3b9c5df3ec365427b707039b382324 Mon Sep 17 00:00:00 2001 From: Mariano Aguero Date: Fri, 3 Aug 2018 10:35:49 -0300 Subject: [PATCH] Apply clear and reload storage --- src/components/Home/index.js | 135 +++++++++++++++++++++++------------ 1 file changed, 88 insertions(+), 47 deletions(-) diff --git a/src/components/Home/index.js b/src/components/Home/index.js index 089943885..301c872e9 100644 --- a/src/components/Home/index.js +++ b/src/components/Home/index.js @@ -16,7 +16,18 @@ const logger = logdown('TW:home') const { CROWDSALE_STRATEGY, TOKEN_SETUP, CROWDSALE_SETUP, PUBLISH, CROWDSALE_PAGE } = NAVIGATION_STEPS -@inject('web3Store', 'generalStore', 'contractStore', 'crowdsaleStore') +@inject( + 'web3Store', + 'generalStore', + 'contractStore', + 'crowdsaleStore', + 'gasPriceStore', + 'deploymentStore', + 'reservedTokenStore', + 'stepTwoValidationStore', + 'tierStore', + 'tokenStore' +) @observer export class Home extends Component { constructor(props) { @@ -30,64 +41,94 @@ export class Home extends Component { contractStore.setProperty('downloadStatus', DOWNLOAD_STATUS.PENDING) } - componentDidMount() { - let { generalStore, web3Store, contractStore } = this.props - checkWeb3(web3Store.web3) - - getNetworkVersion() - .then(networkID => { - generalStore.setProperty('networkID', networkID) - getCrowdsaleAssets(networkID) - }) - .then( - () => { - contractStore.setProperty('downloadStatus', DOWNLOAD_STATUS.SUCCESS) - }, - e => { - logger.error('Error downloading contracts', e) - toast.showToaster({ - type: TOAST.TYPE.ERROR, - message: - 'The contracts could not be downloaded.Please try to refresh the page. If the problem persists, try again later.' - }) - - contractStore.setProperty('downloadStatus', DOWNLOAD_STATUS.FAILURE) - } - ) + async componentDidMount() { + let { web3Store } = this.props + await checkWeb3(web3Store.web3) } - chooseContract = () => { + async chooseContract() { this.setState({ loading: true }) - loadRegistryAddresses().then( - () => { - this.setState({ - loading: false, - showModal: true - }) - }, - e => { - logger.error('There was a problem loading the crowdsale addresses from the registry', e) - this.setState({ - loading: false - }) - } - ) + try { + await loadRegistryAddresses() + this.setState({ + loading: false, + showModal: true + }) + } catch (e) { + logger.error('There was a problem loading the crowdsale addresses from the registry', e) + this.setState({ + loading: false + }) + } } - goNextStep() { - // Clear local storage if there is no incomplete deployment + async goNextStep() { + // Clear local storage if there is no incomplete deployment, and reload if (storage.has('DeploymentStore') && storage.get('DeploymentStore').deploymentStep === null) { - logger.log('Clear storage') + this.clearStorage() + await this.reloadStorage() + } + this.props.history.push('1') + } - // Clear store data - let { crowdsaleStore } = this.props - crowdsaleStore.reset() + async reloadStorage() { + let { generalStore, contractStore } = this.props + + try { + // General store, check network + let networkID = await getNetworkVersion() + generalStore.setProperty('networkID', networkID) + + // Contract store, get contract and abi + await getCrowdsaleAssets(networkID) + contractStore.setProperty('downloadStatus', DOWNLOAD_STATUS.SUCCESS) + } catch (e) { + logger.error('Error downloading contracts', e) + toast.showToaster({ + type: TOAST.TYPE.ERROR, + message: + 'The contracts could not be downloaded.Please try to refresh the page. If the problem persists, try again later.' + }) + contractStore.setProperty('downloadStatus', DOWNLOAD_STATUS.FAILURE) } + } - this.props.history.push('1') + clearStorage() { + // Generate of stores to clear + const toArray = ({ + generalStore, + contractStore, + crowdsaleStore, + gasPriceStore, + deploymentStore, + reservedTokenStore, + stepTwoValidationStore, + tierStore, + tokenStore + }) => { + return [ + generalStore, + contractStore, + crowdsaleStore, + gasPriceStore, + deploymentStore, + reservedTokenStore, + stepTwoValidationStore, + tierStore, + tokenStore + ] + } + + const storesToClear = toArray(this.props) + for (let storeToClear of storesToClear) { + if (typeof storeToClear.reset === 'function') { + logger.log('Store to be cleared:', storeToClear.constructor.name) + storeToClear.reset() + } + } } onClick = crowdsaleAddress => {