5
5
* This source code is licensed under the license found in the LICENSE file in
6
6
* the root directory of this source tree.
7
7
*/
8
- import CodeSnippet from 'components/CodeSnippet/CodeSnippet .react' ;
8
+ import CodeEditor from 'components/CodeEditor/CodeEditor .react' ;
9
9
import DashboardView from 'dashboard/DashboardView.react' ;
10
10
import EmptyState from 'components/EmptyState/EmptyState.react' ;
11
11
import FileTree from 'components/FileTree/FileTree.react' ;
12
12
import history from 'dashboard/history' ;
13
13
import React from 'react' ;
14
14
import styles from 'dashboard/Data/CloudCode/CloudCode.scss' ;
15
15
import Toolbar from 'components/Toolbar/Toolbar.react' ;
16
+ import SaveButton from 'components/SaveButton/SaveButton.react' ;
16
17
17
18
function getPath ( params ) {
18
19
const last = params . location . pathname . split ( 'cloud_code/' ) [ 1 ]
@@ -27,7 +28,9 @@ export default class CloudCode extends DashboardView {
27
28
28
29
this . state = {
29
30
files : undefined ,
30
- source : undefined
31
+ source : undefined ,
32
+ saveState : SaveButton . States . WAITING ,
33
+ saveError : '' ,
31
34
} ;
32
35
}
33
36
@@ -56,8 +59,14 @@ export default class CloudCode extends DashboardView {
56
59
history . replace ( this . context . generatePath ( `cloud_code/${ Object . keys ( release . files ) [ 0 ] } ` ) )
57
60
} else {
58
61
// Means we can load /cloud_code/<fileName>
62
+ this . setState ( { source : undefined } )
59
63
app . getSource ( fileName ) . then (
60
- ( source ) => this . setState ( { source : source } ) ,
64
+ ( source ) => {
65
+ this . setState ( { source : source } )
66
+ if ( this . editor ) {
67
+ this . editor . value = source ;
68
+ }
69
+ } ,
61
70
( ) => this . setState ( { source : undefined } )
62
71
) ;
63
72
}
@@ -87,7 +96,23 @@ export default class CloudCode extends DashboardView {
87
96
</ div >
88
97
) ;
89
98
}
90
-
99
+ async getCode ( ) {
100
+ if ( ! this . editor ) {
101
+ return ;
102
+ }
103
+ this . setState ( { saveState : SaveButton . States . SAVING } ) ;
104
+ let fileName = getPath ( this . props ) ;
105
+ try {
106
+ await this . context . currentApp . saveSource ( fileName , this . editor . value ) ;
107
+ this . setState ( { saveState : SaveButton . States . SUCCEEDED } ) ;
108
+ setTimeout ( ( ) => {
109
+ this . setState ( { saveState : SaveButton . States . WAITING } ) ;
110
+ } , 2000 ) ;
111
+ } catch ( e ) {
112
+ this . setState ( { saveState : SaveButton . States . FAILED } ) ;
113
+ this . setState ( { saveError : e . message || e } ) ;
114
+ }
115
+ }
91
116
renderContent ( ) {
92
117
let toolbar = null ;
93
118
let content = null ;
@@ -111,10 +136,20 @@ export default class CloudCode extends DashboardView {
111
136
subsection = { fileName } /> ;
112
137
113
138
let source = this . state . files [ fileName ] ;
114
- if ( source && source . source ) {
139
+ if ( ( source && source . source ) || this . state . source ) {
115
140
content = (
116
141
< div className = { styles . content } >
117
- < CodeSnippet source = { source . source } language = 'javascript' />
142
+ < CodeEditor
143
+ placeHolder = { this . state . source || source . source }
144
+ ref = { editor => ( this . editor = editor ) }
145
+ fontSize = { '14px' }
146
+ />
147
+ < SaveButton
148
+ state = { this . state . saveState }
149
+ waitingText = { this . state . submitText }
150
+ savingText = { this . state . inProgressText }
151
+ failedText = { this . state . saveError }
152
+ onClick = { ( ) => this . getCode ( this ) } > </ SaveButton >
118
153
</ div >
119
154
) ;
120
155
}
0 commit comments