/* * * 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 _cloneDeep from 'lodash/cloneDeep'; 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(error) { self.props.flux.actions.global.showNotification(error); 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.params.name != 'create') { 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 = ; let params = null; let selectAccount = null; let resfreshStatus = null; let Account = this.state.account; // AccountType is for the view, not the data account-type value; let AccountType = this.state.accountType; let Types = this.state.types; let isEdit = this.props.params.name != 'create'; var buttons; let cloudResources = Account['cloud-resources-state'] && Account['cloud-resources-state'][Account['account-type']]; let cloudResourcesStateHTML = null; // Account Type Radio var selectAccountStack = []; if (!isEdit) { buttons = [ , { (Account['connection-status'] && Account['connection-status'].status && Account['connection-status'].status.toUpperCase()) === 'FAILURE' ? displayFailureMessage(Account['connection-status'].details) : null } ) : null; // cloudResourcesStateHTML = ( //
//

Resources Status

//
//
    // { // cloudResources && props.AccountMeta.resources[Account['account-type']].map(function(r, i) { // return ( //
  • // {r}: {cloudResources[r]} //
  • // ) // }) || 'No Additional Resources' // } //
//
//
// ) } var html = (
* required

Account

{name}

{ isEdit ? (
{props.AccountMeta.labelByType[Account['account-type']]}
) : null }
{selectAccount} {sdnAccounts} {resfreshStatus} {cloudResourcesStateHTML}
    {params}
{buttons}
) return html; } } function displayFailureMessage(msg) { return (
Details:
{msg}
) } class SelectOption extends React.Component { constructor(props){ super(props); } handleOnChange = (e) => { this.props.onChange(JSON.parse(e.target.value)); } render() { let html; html = ( ); return html; } } SelectOption.defaultProps = { options: [], onChange: function(e) { console.dir(e) } } function removeTrailingWhitespace(Account) { 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][param]) == 'string') { Account[type][param] = Account[type][param].trim(); } } } let nestedParams = Account.nestedParams; if (nestedParams && nestedParams.params) { for (let i = 0; i < nestedParams.params.length; i++) { let nestedParam = nestedParams.params[i].ref; let nestedParamValue = Account[type][nestedParams['container-name']][nestedParam]; if (typeof(nestedParamValue) == 'string') { Account[type][nestedParams['container-name']][nestedParam] = nestedParamValue.trim(); } } } return Account; } export default SkyquakeComponent(Account)