2 * STANDARD_RIFT_IO_COPYRIGHT
5 import React from 'react';
6 // import AppHeaderActions from 'widgets/header/headerActions.js';
7 import AppHeaderActions from '../../../../framework/widgets/header/headerActions.js';
8 import './configAgentAccount.scss';
9 import Button from 'widgets/button/rw.button.js';
10 import Crouton from 'react-crouton';
11 import 'style/common.scss';
12 var ConfigAgentAccountStore = require('./configAgentAccountStore')
13 var ConfigAgentAccountActions = require('./configAgentAccountActions')
14 class ConfigAgentAccount extends React.Component {
17 this.state = ConfigAgentAccountStore.getState();
18 ConfigAgentAccountStore.listen(this.storeListener);
21 this.state.validateErrorEvent = 0;
22 this.state.validateErrorMsg = '';
25 storeListener = (state) => {
29 name:state.configAgentAccount.name,
30 "account-type":state.configAgentAccount["account-type"],
31 params:state.configAgentAccount[state.configAgentAccount["account-type"]]
33 isLoading: state.isLoading,
34 validateErrorMsg: state.validateErrorMsg,
35 validateErrorEvent: state.validateErrorEvent
41 if (self.state.configAgentAccount.name == "") {
42 self.validateError("Please give the config agent account a name");
45 var type = self.state.configAgentAccount['account-type'];
46 if (typeof(self.state.params[type]) != 'undefined') {
47 var params = self.state.params[type];
48 for (var i = 0; i < params.length; i++) {
49 var param = params[i].ref;
50 if (typeof(self.state.configAgentAccount[type]) == 'undefined' || typeof(self.state.configAgentAccount[type][param]) == 'undefined' || self.state.configAgentAccount[type][param] == "") {
51 if (!params[i].optional) {
52 self.validateError("Please fill all account details");
60 ConfigAgentAccountStore.create(self.state.configAgentAccount).then(function() {
61 self.context.router.push({pathname:'accounts'});
63 console.log("Caught error", arguments[0].responseJSON.errorMessage.body)
64 ConfigAgentAccountStore.createConfigAccountFailed(arguments[0].responseJSON.errorMessage.body);
70 if (self.state.configAgentAccount.name == "") {
71 self.validateError("Please give the config agent account a name");
76 'account-type':self.state.configAgentAccount['account-type'],
77 name:self.state.configAgentAccount.name
79 submit_obj[submit_obj['account-type']] = self.state.configAgentAccount[submit_obj['account-type']];
80 if(!submit_obj[submit_obj['account-type']]) {
81 submit_obj[submit_obj['account-type']] = {};
83 for (var key in self.state.configAgentAccount.params) {
84 if (submit_obj[submit_obj['account-type']][key]) {
85 //submit_obj[submit_obj['account-type']][key] = self.state.configAgentAccount.params[key];
88 submit_obj[submit_obj['account-type']][key] = self.state.configAgentAccount.params[key];
92 ConfigAgentAccountStore.update(submit_obj).then(function() {
93 self.context.router.push({pathname:'accounts'});
94 }).catch(function(){});
99 this.context.router.push({pathname:'accounts'});
101 componentWillReceiveProps(nextProps) {
103 componentWillUnmount() {
104 ConfigAgentAccountStore.unlisten(this.storeListener);
105 ConfigAgentAccountStore.validateReset();
107 shouldComponentUpdate(nextProps) {
110 handleDelete = () => {
112 ConfigAgentAccountStore.delete(self.state.configAgentAccount.name, function() {
113 self.context.router.push({pathname:'accounts'});
116 handleNameChange(event) {
117 var temp = this.state.configAgentAccount;
118 temp.name = event.target.value;
120 configAgentAccount: temp
123 handleAccountChange(node, event) {
124 var temp = this.state.configAgentAccount;
125 temp['account-type'] = event.target.value;
127 configAgentAccount: temp
130 handleParamChange(node, event) {
131 var temp = this.state.configAgentAccount;
132 if (!this.state.configAgentAccount[this.state.configAgentAccount['account-type']]) {
133 temp[temp['account-type']] = {}
135 temp[temp['account-type']][node.ref] = event.target.value;
137 configAgentAccount: temp
140 preventDefault = (e) => {
144 evaluateSubmit = (e) => {
145 if (e.keyCode == 13) {
151 validateError = (msg) => {
153 validateErrorEvent: true,
154 validateErrorMsg: msg
157 validateReset = () => {
159 validateErrorEvent: false
162 returnCrouton = () => {
165 message={this.state.validateErrorMsg}
167 hidden={!(this.state.validateErrorEvent && this.state.validateErrorMsg)}
168 onDismiss={this.validateReset}
172 // This section builds elements that only show up on the create page.
173 var name = <label>Name <input type="text" onChange={this.handleNameChange.bind(this)} style={{'textAlign':'left'}} /></label>
175 <Button key="0" onClick={this.cancel} className="cancel light" label="Cancel"></Button>,
176 <Button key="1" role="button" onClick={this.create.bind(this)} className="save dark" label="Save" />
178 if (this.props.edit) {
179 name = <label>{this.state.configAgentAccount.name}</label>
181 <a key="0" onClick={this.handleDelete} ng-click="create.delete(create.configAgentAccount)" className="delete">Remove Account</a>,
182 <Button key="1" onClick={this.cancel} className="cancel light" label="Cancel"></Button>,
183 <Button key="2" role="button" onClick={this.update.bind(this)} className="update dark" label="Update" />
185 let selectAccount = null;
188 // This creates the create screen radio button for account type.
189 var selectAccountStack = [];
190 if (!this.props.edit) {
191 for (var i = 0; i < this.state.accountType.length; i++) {
192 var node = this.state.accountType[i];
193 selectAccountStack.push(
195 <input type="radio" name="account" onChange={this.handleAccountChange.bind(this, node)} defaultChecked={node.name == this.state.accountType[0].name} value={node['account-type']} /> {node.name}
200 var selectAccount = (
201 <div className="select-type" style={{"marginLeft":"27px"}}>
210 // This sections builds the parameters for the account details.
212 if (this.state.params[this.state.configAgentAccount['account-type']]) {
213 var paramsStack = [];
214 for (var i = 0; i < this.state.params[this.state.configAgentAccount['account-type']].length; i++) {
215 var optionalField = '';
216 var node = this.state.params[this.state.configAgentAccount['account-type']][i];
218 if (this.state.configAgentAccount[this.state.configAgentAccount['account-type']]) {
219 value = this.state.configAgentAccount[this.state.configAgentAccount['account-type']][node.ref]
221 if (this.props.edit && this.state.configAgentAccount.params) {
222 value = this.state.configAgentAccount.params[node.ref];
225 // If you're on the edit page, but the params have not been recieved yet, this stops us from defining a default value that is empty.
226 if (this.props.edit && !this.state.configAgentAccount.params) {
230 optionalField = <span className="optional">Optional</span>;
234 <label className="create-fleet-pool-params">{node.label} {optionalField}</label>
235 <input className="create-fleet-pool-input" type="text" onChange={this.handleParamChange.bind(this, node)} defaultValue={value}/>
241 <li className="create-fleet-pool">
242 <h3> Enter Account Details</h3>
248 <li className="create-fleet-pool">
249 <h3> Enter Account Details</h3>
250 <label style={{'marginLeft':'17px', color:'#888'}}>No Details Required</label>
255 // This section builds elements that only show up in the edit page.
256 if (this.props.edit) {
257 name = <label>{this.state.configAgentAccount.name}</label>
259 <a key={0} onClick={this.handleDelete} ng-click="create.delete(create.configAgentAccount)" className="delete">Remove Account</a>,
260 <a key={1} onClick={this.cancel} className="cancel">Cancel</a>,
261 <Button key={2} role="button" onClick={this.update.bind(this)} className="update" label="Update" />
263 let selectAccount = null;
269 <form className="app-body create configAgentAccount" onSubmit={this.preventDefault} onKeyDown={this.evaluateSubmit}>
270 {this.returnCrouton()}
271 <h2 className="create-management-domain-header name-input">
275 <ol className="flex-row">
278 <div className="form-actions">
286 ConfigAgentAccount.contextTypes = {
287 router: React.PropTypes.object
289 export default ConfigAgentAccount