From 05b324dffe5da95bd228a72ed2865ba9b34bc548 Mon Sep 17 00:00:00 2001 From: emizzle Date: Wed, 29 Aug 2018 16:12:28 +1000 Subject: [PATCH] Fiddle use cases fixed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Handle use cases: 1) handle case when temp.sol doesn’t exist - due to the new saga updates, the response from retreiving a temp fiddle from the filesystem (even if it doesn’t exist) is forward to the compilation endpoint, which returns an ENOENT, and this is handled in the selector. 2) delete all code - shouldn’t return last fiddle and should compile an empty string. 3) Switch to different tab (ie contracts) then back to fiddle - previous fiddle should remain. Also fixed an issue (most likely due to latest rebase) when deploying fiddle contracts. The gasLimit was not being specified. Handled issue where entities stored in the state were not being put in the correct order, so a timestamp was sent with the requests and then sorted when the response was returned. --- embark-ui/src/actions/index.js | 2 +- embark-ui/src/api/index.js | 4 +-- embark-ui/src/containers/FiddleContainer.js | 29 ++++++++++++--------- embark-ui/src/general.css | 3 ++- embark-ui/src/reducers/selectors.js | 6 +++-- lib/modules/deployment/contract_deployer.js | 2 +- lib/modules/solidity/index.js | 3 +++ 7 files changed, 29 insertions(+), 20 deletions(-) diff --git a/embark-ui/src/actions/index.js b/embark-ui/src/actions/index.js index 7a826915..94f87b50 100644 --- a/embark-ui/src/actions/index.js +++ b/embark-ui/src/actions/index.js @@ -170,7 +170,7 @@ export const ensRecords = { export const FIDDLE = createRequestTypes('FIDDLE'); export const fiddle = { - post: (codeToCompile) => action(FIDDLE[REQUEST], {codeToCompile}), + post: (codeToCompile, timestamp) => action(FIDDLE[REQUEST], {codeToCompile, timestamp}), success: (fiddle, payload) => { return action(FIDDLE[SUCCESS], {fiddles: [{...fiddle, ...payload}]}); }, diff --git a/embark-ui/src/api/index.js b/embark-ui/src/api/index.js index 27294eb6..a4ac1641 100644 --- a/embark-ui/src/api/index.js +++ b/embark-ui/src/api/index.js @@ -4,7 +4,7 @@ import constants from '../constants'; function get(path, params) { return axios.get(constants.httpEndpoint + path, params) .then((response) => { - return {response}; + return {response, error: null}; }).catch((error) => { return {response: null, error: error.message || 'Something bad happened'}; }); @@ -13,7 +13,7 @@ function get(path, params) { function post(path, params) { return axios.post(constants.httpEndpoint + path, params) .then((response) => { - return {response}; + return {response, error: null}; }) .catch((error) => { return {response: null, error: error.message || 'Something bad happened'}; diff --git a/embark-ui/src/containers/FiddleContainer.js b/embark-ui/src/containers/FiddleContainer.js index bca56569..b406c62a 100644 --- a/embark-ui/src/containers/FiddleContainer.js +++ b/embark-ui/src/containers/FiddleContainer.js @@ -22,6 +22,7 @@ class FiddleContainer extends Component { this.state = { value: undefined, loadingMessage: 'Loading...', + readOnly: true }; this.compileTimeout = null; @@ -31,24 +32,24 @@ class FiddleContainer extends Component { componentDidMount() { this.setState({loadingMessage: 'Loading saved state...'}); - if (!this.props.fiddle) { - this.props.fetchLastFiddle(); + this.props.fetchLastFiddle(); + } + + componentDidUpdate(prevProps) { + const {lastFiddle} = this.props; + if(this.state.value === '' && prevProps.lastFiddle === lastFiddle) return; + if((!this.state.value && lastFiddle && !lastFiddle.error) && this.state.value !== lastFiddle) { + this._onCodeChange(lastFiddle, true); } } - componentDidUpdate() { - if(!this.state.value && this.props.fiddle) { - this.setState({value: this.props.fiddle.codeToCompile}); - } - } - - _onCodeChange(newValue) { + _onCodeChange(newValue, immediate = false) { this.setState({readOnly: false, value: newValue}); if (this.compileTimeout) clearTimeout(this.compileTimeout); this.compileTimeout = setTimeout(() => { this.setState({loadingMessage: 'Compiling...'}); - this.props.postFiddle(newValue); - }, 1000); + this.props.postFiddle(newValue, Date.now()); + }, immediate ? 0 : 1000); } @@ -166,7 +167,8 @@ function mapStateToProps(state) { deployedContracts: deployedFiddle.data, fiddleError: fiddle.error, fiddleDeployError: deployedFiddle.error, - loading: state.loading + loading: state.loading, + lastFiddle: fiddle.data ? fiddle.data.codeToCompile : undefined }; } @@ -178,7 +180,8 @@ FiddleContainer.propTypes = { postFiddle: PropTypes.func, postFiddleDeploy: PropTypes.func, deployedContracts: PropTypes.string, - fetchLastFiddle: PropTypes.func + fetchLastFiddle: PropTypes.func, + lastFiddle: PropTypes.any }; export default connect( diff --git a/embark-ui/src/general.css b/embark-ui/src/general.css index 934ac06f..9bdf0438 100644 --- a/embark-ui/src/general.css +++ b/embark-ui/src/general.css @@ -22,7 +22,8 @@ } .text__new-line, .card.warnings-card .list-group-item, .card.errors-card .list-group-item { - white-space: pre-line; + white-space: pre-wrap; + font-family: monospace; } .card.card-fullscreen{ z-index: 6; diff --git a/embark-ui/src/reducers/selectors.js b/embark-ui/src/reducers/selectors.js index 361e49fd..a4b032e5 100644 --- a/embark-ui/src/reducers/selectors.js +++ b/embark-ui/src/reducers/selectors.js @@ -109,9 +109,11 @@ export function getMessages(state) { } export function getFiddle(state) { + const fiddleCompilation = _.last(state.entities.fiddles.sort((a, b) => { return (a.timestamp || 0) - (b.timestamp || 0); })); + const isNoTempFileError = Boolean(fiddleCompilation && fiddleCompilation.codeToCompile && fiddleCompilation.codeToCompile.error && fiddleCompilation.codeToCompile.error.indexOf('ENOENT') > -1); return { - data: _.last(state.entities.fiddles), - error: state.errorEntities.fiddles + data: fiddleCompilation, + error: isNoTempFileError ? undefined : state.errorEntities.fiddles }; } diff --git a/lib/modules/deployment/contract_deployer.js b/lib/modules/deployment/contract_deployer.js index ae97e002..658f0623 100644 --- a/lib/modules/deployment/contract_deployer.js +++ b/lib/modules/deployment/contract_deployer.js @@ -199,7 +199,7 @@ class ContractDeployer { // TODO: can be moved into a afterDeploy event // just need to figure out the gasLimit coupling issue - self.events.request('code-generator:contract:vanilla', contract, contract._gasLimit, (contractCode) => { + self.events.request('code-generator:contract:vanilla', contract, contract._gasLimit || false, (contractCode) => { self.events.request('runcode:eval', contractCode, () => {}, true); return callback(); }); diff --git a/lib/modules/solidity/index.js b/lib/modules/solidity/index.js index 4b6e9765..48cfd4c0 100644 --- a/lib/modules/solidity/index.js +++ b/lib/modules/solidity/index.js @@ -20,6 +20,9 @@ class Solidity { 'post', '/embark-api/contract/compile', (req, res) => { + if(typeof req.body.codeToCompile !== 'string'){ + return res.send({error: 'Body parameter \'codeToCompile\' must be a string'}); + } const input = {'fiddle': {content: req.body.codeToCompile.replace(/\r\n/g, '\n')}}; this.compile_solidity_code(input, {}, true, (errors, compilationResult) => { // write code to filesystem so we can view the source after page refresh