6
6
* the root directory of this source tree.
7
7
*/
8
8
import CodeSnippet from 'components/CodeSnippet/CodeSnippet.react' ;
9
+ import CodeEditor from 'components/CodeEditor/CodeEditor.react' ;
9
10
import DashboardView from 'dashboard/DashboardView.react' ;
10
11
import EmptyState from 'components/EmptyState/EmptyState.react' ;
11
12
import FileTree from 'components/FileTree/FileTree.react' ;
@@ -14,11 +15,13 @@ import styles from 'dashboard/Data/CloudCode/CloudCode.scss';
14
15
import Toolbar from 'components/Toolbar/Toolbar.react' ;
15
16
import generatePath from 'lib/generatePath' ;
16
17
import { withRouter } from 'lib/withRouter' ;
18
+ import SaveButton from 'components/SaveButton/SaveButton.react' ;
17
19
18
20
function getPath ( params ) {
19
- return params . splat ;
21
+ return params [ '*' ] ;
20
22
}
21
23
24
+
22
25
@withRouter
23
26
class CloudCode extends DashboardView {
24
27
constructor ( ) {
@@ -29,6 +32,8 @@ class CloudCode extends DashboardView {
29
32
this . state = {
30
33
files : undefined ,
31
34
source : undefined ,
35
+ saveState : SaveButton . States . WAITING ,
36
+ saveError : '' ,
32
37
} ;
33
38
}
34
39
@@ -37,7 +42,7 @@ class CloudCode extends DashboardView {
37
42
}
38
43
39
44
componentWillReceiveProps ( nextProps , nextContext ) {
40
- if ( this . context !== nextContext ) {
45
+ if ( this . context !== nextContext || getPath ( nextProps . params ) !== getPath ( this . props . params ) ) {
41
46
this . fetchSource ( nextContext , getPath ( nextProps . params ) ) ;
42
47
}
43
48
}
@@ -54,9 +59,13 @@ class CloudCode extends DashboardView {
54
59
55
60
if ( ! fileName || release . files [ fileName ] === undefined ) {
56
61
// 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
+ ) ;
58
66
} else {
59
67
// Means we can load /cloud_code/<fileName>
68
+ this . setState ( { source : undefined } ) ;
60
69
app . getSource ( fileName ) . then (
61
70
source => this . setState ( { source : source } ) ,
62
71
( ) => this . setState ( { source : undefined } )
@@ -90,6 +99,24 @@ class CloudCode extends DashboardView {
90
99
) ;
91
100
}
92
101
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
+
93
120
renderContent ( ) {
94
121
let toolbar = null ;
95
122
let content = null ;
@@ -113,11 +140,23 @@ class CloudCode extends DashboardView {
113
140
if ( fileName ) {
114
141
toolbar = < Toolbar section = "Cloud Code" subsection = { fileName } /> ;
115
142
116
- const source = this . state . files [ fileName ] ;
117
- if ( source && source . source ) {
143
+ const source = this . state . source ;
144
+ if ( source ) {
118
145
content = (
119
146
< 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 >
121
160
</ div >
122
161
) ;
123
162
}
0 commit comments