1
+ import update from 'react-addons-update' ;
2
+ import { Component } from 'react' ;
3
+ import ReactDOM from 'react-dom' ;
4
+ import Modal from 'react-bootstrap-modal'
5
+
6
+ // The API utils
7
+ import CentralConfigAPIUtils from '../utils/CentralConfigAPIUtils' ;
8
+
9
+ class EditItemModal extends Component {
10
+
11
+ constructor ( props ) {
12
+
13
+ super ( props ) ;
14
+
15
+ // Set initial state:
16
+ this . state = {
17
+ configItem : props . configItem
18
+ } ;
19
+
20
+ // Bind our events:
21
+ this . _onApplicationChange = this . _onApplicationChange . bind ( this ) ;
22
+ this . _onNameChange = this . _onNameChange . bind ( this ) ;
23
+ this . _onValueChange = this . _onValueChange . bind ( this ) ;
24
+ this . _onMachineChange = this . _onMachineChange . bind ( this ) ;
25
+
26
+ this . _handleKeyPress = this . _handleKeyPress . bind ( this ) ;
27
+ this . _saveItem = this . _saveItem . bind ( this ) ;
28
+ }
29
+
30
+ render ( ) {
31
+ // Return the app HTML to render
32
+ return (
33
+ < Modal show = { this . props . show } onHide = { this . props . hide } aria-labelledby = "ModalHeader" >
34
+ < Modal . Header closeButton >
35
+ < Modal . Title id = 'ModalHeader' > Edit config item</ Modal . Title >
36
+ </ Modal . Header >
37
+
38
+ < Modal . Body >
39
+ < div className = "form-group" >
40
+ < label htmlFor = "txtNewAppName" > Application</ label >
41
+ < input type = "text" className = "form-control" id = "txtNewAppName" autoFocus value = { this . state . configItem . application } onChange = { this . _onApplicationChange } onKeyPress = { this . _handleKeyPress } placeholder = "Your application name" />
42
+ </ div >
43
+ < div className = "form-group" >
44
+ < label htmlFor = "txtNewName" > Name</ label >
45
+ < input type = "text" className = "form-control" id = "txtNewName" value = { this . state . configItem . name } onChange = { this . _onNameChange } onKeyPress = { this . _handleKeyPress } placeholder = "Config item name" />
46
+ </ div >
47
+ < div className = "form-group" >
48
+ < label htmlFor = "txtNewValue" > Value</ label >
49
+ < input type = "text" className = "form-control" id = "txtNewValue" value = { this . state . configItem . value } onChange = { this . _onValueChange } onKeyPress = { this . _handleKeyPress } placeholder = "Config value" />
50
+ </ div >
51
+ < div className = "form-group" >
52
+ < label htmlFor = "txtNewMachine" > Machine</ label >
53
+ < input type = "text" className = "form-control" id = "txtNewMachine" value = { this . state . configItem . machine } onChange = { this . _onMachineChange } onKeyPress = { this . _handleKeyPress } placeholder = "Optional machine name" />
54
+ </ div >
55
+ </ Modal . Body >
56
+
57
+ < Modal . Footer >
58
+ < Modal . Dismiss className = 'btn btn-default' > Cancel</ Modal . Dismiss >
59
+ < button className = 'btn btn-primary' type = 'button' onClick = { this . _saveItem } >
60
+ Save
61
+ </ button >
62
+ </ Modal . Footer >
63
+ </ Modal >
64
+ ) ;
65
+ }
66
+
67
+ _handleKeyPress ( event ) {
68
+ if ( event . which == 13 ) {
69
+ this . _saveItem ( event ) ;
70
+ }
71
+ }
72
+
73
+ _onApplicationChange ( event ) {
74
+ // Using new Immutability helpers from
75
+ // https://facebook.github.io/react/docs/update.html
76
+ var newState = update ( this . state , {
77
+ configItem : { application : { $set : event . target . value } }
78
+ } ) ;
79
+ this . setState ( newState ) ;
80
+ }
81
+
82
+ _onNameChange ( event ) {
83
+ // Using new Immutability helpers from
84
+ // https://facebook.github.io/react/docs/update.html
85
+ var newState = update ( this . state , {
86
+ configItem : { name : { $set : event . target . value } }
87
+ } ) ;
88
+ this . setState ( newState ) ;
89
+ }
90
+
91
+ _onValueChange ( event ) {
92
+ // Using new Immutability helpers from
93
+ // https://facebook.github.io/react/docs/update.html
94
+ var newState = update ( this . state , {
95
+ configItem : { value : { $set : event . target . value } }
96
+ } ) ;
97
+ this . setState ( newState ) ;
98
+ }
99
+
100
+ _onMachineChange ( event ) {
101
+ // Using new Immutability helpers from
102
+ // https://facebook.github.io/react/docs/update.html
103
+ var newState = update ( this . state , {
104
+ configItem : { machine : { $set : event . target . value } }
105
+ } ) ;
106
+ this . setState ( newState ) ;
107
+ }
108
+
109
+ _saveItem ( event ) {
110
+ // Validate inputs
111
+
112
+ // Set the item and get all items:
113
+ CentralConfigAPIUtils . setConfigItem ( this . state . configItem ) . then ( CentralConfigAPIUtils . getAllConfigItems ) ;
114
+
115
+ // Reset state:
116
+ this . state . configItem = { } ;
117
+
118
+ // Hide the dialog:
119
+ this . props . hide ( ) ;
120
+ }
121
+
122
+ }
123
+
124
+ export default EditItemModal
0 commit comments