Skip to content

Commit

Permalink
Allow Writing Cloud Code
Browse files Browse the repository at this point in the history
  • Loading branch information
dblythy committed Nov 2, 2020
1 parent e9d3ea7 commit 2a5c050
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 19 deletions.
3 changes: 3 additions & 0 deletions src/components/SaveButton/SaveButton.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export const demos = [
<div style={{padding: 10}}>
<SaveButton state={SaveButton.States.FAILED} />
</div>
<div style={{padding: 10}}>
<SaveButton state={SaveButton.States.WAITING} />
</div>
</div>
)
}, {
Expand Down
2 changes: 1 addition & 1 deletion src/components/SaveButton/SaveButton.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ let SaveButton = ({
</span>;
};

SaveButton.States = keyMirror(['SAVING', 'SUCCEEDED', 'FAILED']);
SaveButton.States = keyMirror(['SAVING', 'SUCCEEDED', 'FAILED', 'WAITING']);

let {...forwardedButtonProps} = Button.propTypes;
delete forwardedButtonProps.value;
Expand Down
47 changes: 41 additions & 6 deletions src/dashboard/Data/CloudCode/CloudCode.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
import CodeSnippet from 'components/CodeSnippet/CodeSnippet.react';
import CodeEditor from 'components/CodeEditor/CodeEditor.react';
import DashboardView from 'dashboard/DashboardView.react';
import EmptyState from 'components/EmptyState/EmptyState.react';
import FileTree from 'components/FileTree/FileTree.react';
import history from 'dashboard/history';
import React from 'react';
import styles from 'dashboard/Data/CloudCode/CloudCode.scss';
import Toolbar from 'components/Toolbar/Toolbar.react';
import SaveButton from 'components/SaveButton/SaveButton.react';

function getPath(params) {
const last = params.location.pathname.split('cloud_code/')[1]
Expand All @@ -27,7 +28,9 @@ export default class CloudCode extends DashboardView {

this.state = {
files: undefined,
source: undefined
source: undefined,
saveState: SaveButton.States.WAITING,
saveError: '',
};
}

Expand Down Expand Up @@ -56,8 +59,14 @@ export default class CloudCode extends DashboardView {
history.replace(this.context.generatePath(`cloud_code/${Object.keys(release.files)[0]}`))
} else {
// Means we can load /cloud_code/<fileName>
this.setState({ source: undefined })
app.getSource(fileName).then(
(source) => this.setState({ source: source }),
(source) => {
this.setState({ source: source })
if (this.editor) {
this.editor.value = source;
}
},
() => this.setState({ source: undefined })
);
}
Expand Down Expand Up @@ -87,7 +96,23 @@ export default class CloudCode extends DashboardView {
</div>
);
}

async getCode() {
if (!this.editor) {
return;
}
this.setState({ saveState: SaveButton.States.SAVING });
let fileName = getPath(this.props);
try {
await this.context.currentApp.saveSource(fileName,this.editor.value);
this.setState({ saveState: SaveButton.States.SUCCEEDED });
setTimeout(()=> {
this.setState({ saveState: SaveButton.States.WAITING });
},2000);
} catch (e) {
this.setState({ saveState: SaveButton.States.FAILED });
this.setState({ saveError: e.message || e });
}
}
renderContent() {
let toolbar = null;
let content = null;
Expand All @@ -111,10 +136,20 @@ export default class CloudCode extends DashboardView {
subsection={fileName} />;

let source = this.state.files[fileName];
if (source && source.source) {
if ((source && source.source) || this.state.source) {
content = (
<div className={styles.content}>
<CodeSnippet source={source.source} language='javascript' />
<CodeEditor
placeHolder={this.state.source || source.source}
ref={editor => (this.editor = editor)}
fontSize={'14px'}
/>
<SaveButton
state={this.state.saveState}
waitingText={this.state.submitText}
savingText={this.state.inProgressText}
failedText={this.state.saveError}
onClick={() => this.getCode(this)}></SaveButton>
</div>
);
}
Expand Down
22 changes: 10 additions & 12 deletions src/lib/ParseApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ export default class ParseApp {
return this.apiRequest('GET', path, {}, { useMasterKey: true });
}

/**
* Saves source of a Cloud Code hosted file from api.parse.com
* fileName - the name of the file to be fetched
* data - the text to save to the cloud file
*/
saveSource(fileName,data) {
return this.apiRequest('POST', `scripts/${fileName}`, {data}, { useMasterKey: true });
}

/**
* Fetches source of a Cloud Code hosted file from api.parse.com
* fileName - the name of the file to be fetched
Expand All @@ -127,22 +136,11 @@ export default class ParseApp {
// No release yet
return Promise.resolve(null);
}

let fileMetaData = release.files[fileName];
if (fileMetaData && fileMetaData.source) {
return Promise.resolve(fileMetaData.source);
}

let params = {
version: fileMetaData.version,
checksum: fileMetaData.checksum
}
return this.apiRequest('GET', `scripts/${fileName}`, params, { useMasterKey: true });
return this.apiRequest('GET', `scripts/${fileName}`, {}, { useMasterKey: true });
}).then((source) => {
if (this.latestRelease.files) {
this.latestRelease.files[fileName].source = source;
}

return Promise.resolve(source);
});
}
Expand Down

0 comments on commit 2a5c050

Please sign in to comment.