Skip to content

Commit 20d4864

Browse files
committed
add View/Edit Cloud code
1 parent 2c89db1 commit 20d4864

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

src/components/SaveButton/SaveButton.react.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const SaveButton = ({
5656
);
5757
};
5858

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

6161
const { ...forwardedButtonProps } = Button.propTypes;
6262
delete forwardedButtonProps.value;

src/dashboard/Data/CloudCode/CloudCode.react.js

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* the root directory of this source tree.
77
*/
88
import CodeSnippet from 'components/CodeSnippet/CodeSnippet.react';
9+
import CodeEditor from 'components/CodeEditor/CodeEditor.react';
910
import DashboardView from 'dashboard/DashboardView.react';
1011
import EmptyState from 'components/EmptyState/EmptyState.react';
1112
import FileTree from 'components/FileTree/FileTree.react';
@@ -14,11 +15,13 @@ import styles from 'dashboard/Data/CloudCode/CloudCode.scss';
1415
import Toolbar from 'components/Toolbar/Toolbar.react';
1516
import generatePath from 'lib/generatePath';
1617
import { withRouter } from 'lib/withRouter';
18+
import SaveButton from 'components/SaveButton/SaveButton.react';
1719

1820
function getPath(params) {
19-
return params.splat;
21+
return params['*'];
2022
}
2123

24+
2225
@withRouter
2326
class CloudCode extends DashboardView {
2427
constructor() {
@@ -29,6 +32,8 @@ class CloudCode extends DashboardView {
2932
this.state = {
3033
files: undefined,
3134
source: undefined,
35+
saveState: SaveButton.States.WAITING,
36+
saveError: '',
3237
};
3338
}
3439

@@ -37,7 +42,7 @@ class CloudCode extends DashboardView {
3742
}
3843

3944
componentWillReceiveProps(nextProps, nextContext) {
40-
if (this.context !== nextContext) {
45+
if (this.context !== nextContext || getPath(nextProps.params) !== getPath(this.props.params)) {
4146
this.fetchSource(nextContext, getPath(nextProps.params));
4247
}
4348
}
@@ -54,9 +59,13 @@ class CloudCode extends DashboardView {
5459

5560
if (!fileName || release.files[fileName] === undefined) {
5661
// Means we're still in /cloud_code/. Let's redirect to /cloud_code/main.js
57-
this.props.navigate(generatePath(this.context, 'cloud_code/main.js'), { replace: true });
62+
63+
this.props.navigate(
64+
generatePath(this.context, `cloud_code/${Object.keys(release.files)[0]}`)
65+
);
5866
} else {
5967
// Means we can load /cloud_code/<fileName>
68+
this.setState({ source: undefined });
6069
app.getSource(fileName).then(
6170
source => this.setState({ source: source }),
6271
() => this.setState({ source: undefined })
@@ -90,6 +99,24 @@ class CloudCode extends DashboardView {
9099
);
91100
}
92101

102+
async getCode() {
103+
if (!this.editor) {
104+
return;
105+
}
106+
this.setState({ saveState: SaveButton.States.SAVING });
107+
let fileName = getPath(this.props.params);
108+
try {
109+
await this.context.saveSource(fileName,this.editor.value);
110+
this.setState({ saveState: SaveButton.States.SUCCEEDED });
111+
setTimeout(()=> {
112+
this.setState({ saveState: SaveButton.States.WAITING });
113+
},2000);
114+
} catch (e) {
115+
this.setState({ saveState: SaveButton.States.FAILED });
116+
this.setState({ saveError: e.message || e });
117+
}
118+
}
119+
93120
renderContent() {
94121
let toolbar = null;
95122
let content = null;
@@ -113,11 +140,23 @@ class CloudCode extends DashboardView {
113140
if (fileName) {
114141
toolbar = <Toolbar section="Cloud Code" subsection={fileName} />;
115142

116-
const source = this.state.files[fileName];
117-
if (source && source.source) {
143+
const source = this.state.source;
144+
if (source) {
118145
content = (
119146
<div className={styles.content}>
120-
<CodeSnippet source={source.source} language="javascript" />
147+
{/* <CodeSnippet source={source.source} language="javascript" /> */}
148+
<CodeEditor
149+
defaultValue={source}
150+
ref={editor => (this.editor = editor)}
151+
fontSize={14}
152+
/>
153+
<div style={{ padding: 10, alignContent: 'center', display: 'flex', justifyContent: 'center' }}>
154+
<SaveButton
155+
state={this.state.saveState}
156+
failedText={this.state.saveError}
157+
onClick={() => this.getCode(this)}
158+
/>
159+
</div>
121160
</div>
122161
);
123162
}

src/lib/ParseApp.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ export default class ParseApp {
154154
return this.apiRequest('GET', path, {}, { useMasterKey: true });
155155
}
156156

157+
/**
158+
* Saves source of a Cloud Code hosted file from api.parse.com
159+
* fileName - the name of the file to be fetched
160+
* data - the text to save to the cloud file
161+
*/
162+
saveSource(fileName, data) {
163+
return this.apiRequest('POST', `scripts/${fileName}`, { data }, { useMasterKey: true });
164+
}
165+
157166
/**
158167
* Fetches source of a Cloud Code hosted file from api.parse.com
159168
* fileName - the name of the file to be fetched

0 commit comments

Comments
 (0)