55 * This source code is licensed under the license found in the LICENSE file in
66 * the root directory of this source tree.
77 */
8- import CodeSnippet from 'components/CodeSnippet/CodeSnippet .react' ;
8+ import CodeEditor from 'components/CodeEditor/CodeEditor .react' ;
99import DashboardView from 'dashboard/DashboardView.react' ;
1010import EmptyState from 'components/EmptyState/EmptyState.react' ;
1111import FileTree from 'components/FileTree/FileTree.react' ;
1212import history from 'dashboard/history' ;
1313import React from 'react' ;
1414import styles from 'dashboard/Data/CloudCode/CloudCode.scss' ;
1515import Toolbar from 'components/Toolbar/Toolbar.react' ;
16+ import SaveButton from 'components/SaveButton/SaveButton.react' ;
1617
1718function getPath ( params ) {
1819 const last = params . location . pathname . split ( 'cloud_code/' ) [ 1 ]
@@ -27,7 +28,9 @@ export default class CloudCode extends DashboardView {
2728
2829 this . state = {
2930 files : undefined ,
30- source : undefined
31+ source : undefined ,
32+ saveState : SaveButton . States . WAITING ,
33+ saveError : '' ,
3134 } ;
3235 }
3336
@@ -56,8 +59,14 @@ export default class CloudCode extends DashboardView {
5659 history . replace ( this . context . generatePath ( `cloud_code/${ Object . keys ( release . files ) [ 0 ] } ` ) )
5760 } else {
5861 // Means we can load /cloud_code/<fileName>
62+ this . setState ( { source : undefined } )
5963 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+ } ,
6170 ( ) => this . setState ( { source : undefined } )
6271 ) ;
6372 }
@@ -87,7 +96,23 @@ export default class CloudCode extends DashboardView {
8796 </ div >
8897 ) ;
8998 }
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+ }
91116 renderContent ( ) {
92117 let toolbar = null ;
93118 let content = null ;
@@ -111,10 +136,20 @@ export default class CloudCode extends DashboardView {
111136 subsection = { fileName } /> ;
112137
113138 let source = this . state . files [ fileName ] ;
114- if ( source && source . source ) {
139+ if ( ( source && source . source ) || this . state . source ) {
115140 content = (
116141 < 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 >
118153 </ div >
119154 ) ;
120155 }
0 commit comments