/*
*
* Copyright 2016 RIFT.IO Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import React from 'react';
import Button from 'widgets/button/rw.button.js';
import _ from 'lodash';
import SkyquakeComponent from 'widgets/skyquake_container/skyquakeComponent.jsx';
import Crouton from 'react-crouton';
import TextInput from 'widgets/form_controls/textInput.jsx';
import {AccountConnectivityStatus} from '../account_sidebar/accountSidebar.jsx';
import 'style/common.scss';
import './account.scss';
class Account extends React.Component {
constructor(props) {
super(props);
this.state = {};
this.state.account = {};
}
storeListener = (state) => {
if(!state.account) {
this.setUp(this.props)
}
state.account && this.setState({account: state.account,accountType: state.accountType, types: state.types, sdnOptions: state.sdnOptions})
}
componentWillMount() {
this.props.store.listen(this.storeListener);
this.setUp(this.props);
}
componentWillReceiveProps(nextProps) {
if(JSON.stringify(nextProps.params) != JSON.stringify(this.props.params)){
this.setUp(nextProps);
}
}
componentWillUnmount() {
this.props.store.unlisten(this.storeListener);
}
setUp(props){
if(props.params.name != 'create') {
this.props.store.viewAccount({type: props.params.type, name: props.params.name});
} else {
this.props.store.setAccountTemplate(props.params.type);
}
}
create(e) {
e.preventDefault();
var self = this;
var Account = this.state.account;
let AccountType = this.state.accountType;
if (Account.name == "") {
self.props.flux.actions.global.showNotification("Please give the account a name");
return;
} else {
var type = Account['account-type'];
var params = Account.params;
if(params) {
for (var i = 0; i < params.length; i++) {
var param = params[i].ref;
if (typeof(Account[type]) == 'undefined' || typeof(Account[type][param]) == 'undefined' || Account[type][param] == "") {
if (!params[i].optional) {
self.props.flux.actions.global.showNotification("Please fill all account details");
return;
}
}
}
}
let nestedParams = Account.nestedParams && Account.nestedParams;
if (nestedParams && nestedParams.params) {
for (let i = 0; i < nestedParams.params.length; i++) {
let nestedParam = nestedParams.params[i].ref;
if (typeof(Account[type]) == 'undefined' || typeof(Account[type][nestedParams['container-name']][nestedParam]) == 'undefined' || Account[type][nestedParams['container-name']][nestedParam] == "") {
if (!nestedParams.params[i].optional) {
self.props.flux.actions.global.showNotification("Please fill all account details");
return;
}
}
}
}
}
let newAccount = _.cloneDeep(removeTrailingWhitespace(Account));
delete newAccount.params;
newAccount.nestedParams &&
newAccount.nestedParams['container-name'] &&
delete newAccount[newAccount.nestedParams['container-name']];
delete newAccount.nestedParams;
this.props.flux.actions.global.showScreenLoader();
this.props.store.create(newAccount, AccountType).then(function() {
self.props.router.push({pathname:'accounts'});
self.props.flux.actions.global.hideScreenLoader.defer();
},
function() {
self.props.flux.actions.global.showNotification("There was an error creating your account. Please contact your system administrator.");
self.props.flux.actions.global.hideScreenLoader.defer();
});
}
update(e) {
e.preventDefault();
var self = this;
var Account = this.state.account;
let AccountType = this.state.accountType;
this.props.flux.actions.global.showScreenLoader();
this.props.store.update(Account, AccountType).then(function() {
self.props.router.push({pathname:'accounts'});
self.props.flux.actions.global.hideScreenLoader();
},
function() {
});
}
cancel = (e) => {
e.preventDefault();
e.stopPropagation();
this.props.router.push({pathname:'accounts'});
}
handleDelete = () => {
let self = this;
let msg = 'Preparing to delete "' + self.state.account.name + '"' +
' Are you sure you want to delete this ' + self.state.accountType + ' account?"';
if (window.confirm(msg)) {
this.props.store.delete(self.state.accountType, self.state.account.name).then(function() {
self.props.flux.actions.global.hideScreenLoader();
self.props.router.push({pathname:'accounts'});
}, function(){
// self.props.flux.actions.global.hideScreenLoader.defer();
// console.log('Delete Account Fail');
});
} else {
self.props.flux.actions.global.hideScreenLoader();
}
}
handleNameChange(event) {
this.props.store.handleNameChange(event);
}
handleAccountTypeChange(node, event) {
this.props.store.handleAccountTypeChange(node, event);
}
handleSelectSdnAccount = (e) => {
var tmp = this.state.account;
if(e) {
tmp['sdn-account'] = e;
} else {
if(tmp['sdn-account']) {
delete tmp['sdn-account'];
}
}
console.log(e, tmp)
}
preventDefault = (e) => {
e.preventDefault();
e.stopPropagation();
}
evaluateSubmit = (e) => {
if (e.keyCode == 13) {
if (this.props.edit) {
this.update(e);
} else {
this.create(e);
}
e.preventDefault();
e.stopPropagation();
}
}
render() {
let self = this;
let {store, ...props} = this.props;
// This section builds elements that only show up on the create page.
// var name = ;
var name =