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
- return params . splat ;
19
+ const last = params . location . pathname . split ( 'cloud_code/' ) [ 1 ]
20
+ return last ;
19
21
}
20
22
21
23
export default class CloudCode extends DashboardView {
@@ -26,17 +28,19 @@ export default class CloudCode extends DashboardView {
26
28
27
29
this . state = {
28
30
files : undefined ,
29
- source : undefined
31
+ source : undefined ,
32
+ saveState : SaveButton . States . WAITING ,
33
+ saveError : '' ,
30
34
} ;
31
35
}
32
36
33
37
componentWillMount ( ) {
34
- this . fetchSource ( this . context . currentApp , getPath ( this . props . params ) ) ;
38
+ this . fetchSource ( this . context . currentApp , getPath ( this . props ) ) ;
35
39
}
36
40
37
41
componentWillReceiveProps ( nextProps , nextContext ) {
38
42
if ( this . context !== nextContext ) {
39
- this . fetchSource ( nextContext . currentApp , getPath ( nextProps . params ) ) ;
43
+ this . fetchSource ( nextContext . currentApp , getPath ( nextProps ) ) ;
40
44
}
41
45
}
42
46
@@ -52,11 +56,17 @@ export default class CloudCode extends DashboardView {
52
56
53
57
if ( ! fileName || release . files [ fileName ] === undefined ) {
54
58
// Means we're still in /cloud_code/. Let's redirect to /cloud_code/main.js
55
- history . replace ( this . context . generatePath ( ' cloud_code/main.js' ) )
59
+ history . replace ( this . context . generatePath ( ` cloud_code/${ Object . keys ( release . files ) [ 0 ] } ` ) )
56
60
} else {
57
61
// Means we can load /cloud_code/<fileName>
62
+ this . setState ( { source : undefined } )
58
63
app . getSource ( fileName ) . then (
59
- ( source ) => this . setState ( { source : source } ) ,
64
+ ( source ) => {
65
+ this . setState ( { source : source } )
66
+ if ( this . editor ) {
67
+ this . editor . value = source ;
68
+ }
69
+ } ,
60
70
( ) => this . setState ( { source : undefined } )
61
71
) ;
62
72
}
@@ -66,7 +76,7 @@ export default class CloudCode extends DashboardView {
66
76
}
67
77
68
78
renderSidebar ( ) {
69
- let current = getPath ( this . props . params ) || '' ;
79
+ let current = getPath ( this . props ) || '' ;
70
80
let files = this . state . files ;
71
81
if ( ! files ) {
72
82
return null ;
@@ -86,11 +96,27 @@ export default class CloudCode extends DashboardView {
86
96
</ div >
87
97
) ;
88
98
}
89
-
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
+ }
90
116
renderContent ( ) {
91
117
let toolbar = null ;
92
118
let content = null ;
93
- let fileName = getPath ( this . props . params ) ;
119
+ let fileName = getPath ( this . props ) ;
94
120
95
121
if ( ! this . state . files || Object . keys ( this . state . files ) . length === 0 ) {
96
122
content = (
@@ -110,10 +136,20 @@ export default class CloudCode extends DashboardView {
110
136
subsection = { fileName } /> ;
111
137
112
138
let source = this . state . files [ fileName ] ;
113
- if ( source && source . source ) {
139
+ if ( ( source && source . source ) || this . state . source ) {
114
140
content = (
115
141
< div className = { styles . content } >
116
- < 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 >
117
153
</ div >
118
154
) ;
119
155
}
0 commit comments