From 9f892e501ce37f358b0b8fc9854253673cefb709 Mon Sep 17 00:00:00 2001 From: Karishma Chadha Date: Mon, 12 Nov 2018 16:31:37 -0500 Subject: [PATCH] Create cloud var from variable modal. --- src/containers/blocks.jsx | 10 ++++++++-- src/containers/prompt.jsx | 6 ++++-- src/lib/cloud-provider.js | 40 ++++++++++++++++++++++++++++++--------- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/src/containers/blocks.jsx b/src/containers/blocks.jsx index 57c03c967..41f08a214 100644 --- a/src/containers/blocks.jsx +++ b/src/containers/blocks.jsx @@ -397,11 +397,17 @@ class Blocks extends React.Component { handleOpenSoundRecorder () { this.props.onOpenSoundRecorder(); } - handlePromptCallback (input, optionSelection) { + + /* + * Pass along information about proposed name and variable options (scope and isCloud) + * and additional potentially conflicting variable names from the VM + * to the variable validation prompt callback used in scratch-blocks. + */ + handlePromptCallback (input, variableOptions) { this.state.prompt.callback( input, this.props.vm.runtime.getAllVarNamesOfType(this.state.prompt.varType), - optionSelection); + variableOptions); this.handlePromptClose(); } handlePromptClose () { diff --git a/src/containers/prompt.jsx b/src/containers/prompt.jsx index 36640ff3a..3b5a53568 100644 --- a/src/containers/prompt.jsx +++ b/src/containers/prompt.jsx @@ -24,8 +24,10 @@ class Prompt extends React.Component { if (event.key === 'Enter') this.handleOk(); } handleOk () { - // TODO CLOUD add cloud variable option info - this.props.onOk(this.state.inputValue, (this.state.globalSelected ? 'global' : 'local')); + this.props.onOk(this.state.inputValue, { + scope: this.state.globalSelected ? 'global' : 'local', + isCloud: this.state.cloudSelected + }); } handleCancel () { this.props.onCancel(); diff --git a/src/lib/cloud-provider.js b/src/lib/cloud-provider.js index 9ca5424e3..62c3bce25 100644 --- a/src/lib/cloud-provider.js +++ b/src/lib/cloud-provider.js @@ -44,15 +44,7 @@ class CloudProvider { const messageString = event.data; log.info(`Received websocket message: ${messageString}`); const message = JSON.parse(messageString); - if (message.method === 'set') { - const varData = { - varUpdate: { - name: message.name, - value: message.value - } - }; - this.vm.postIOData('cloud', varData); - } + this.parseMessage(message); }; this.connection.onopen = () => { @@ -65,6 +57,26 @@ class CloudProvider { }; } + parseMessage (message) { + const varData = {}; + switch (message.method) { + case 'ack': { + varData.varCreate = { + name: message.name + }; + break; + } + case 'set': { + varData.varUpdate = { + name: message.name, + value: message.value + }; + break; + } + } + this.vm.postIOData('cloud', varData); + } + /** * Format and send a message to the cloud data server. * @param {string} methodName The message method, indicating the action to perform. @@ -100,6 +112,16 @@ class CloudProvider { log.info(`Sent message to clouddata server: ${data}`); } + /** + * Provides an API for the VM's cloud IO device to create + * a new cloud variable on the server. + * @param {string} name The name of the variable to create + * @param {string | number} value The value of the new cloud variable. + */ + createVariable (name, value) { + this.writeToServer('create', name, value); + } + /** * Provides an API for the VM's cloud IO device to update * a cloud variable on the server.