/*
* STANDARD_RIFT_IO_COPYRIGHT
*/
import React from 'react';
import ReactDOM from 'react-dom';
import AppHeader from 'widgets/header/header.jsx';
import RedundancyStore from './redundancyStore.js';
import SkyquakeComponent from 'widgets/skyquake_container/skyquakeComponent.jsx';
import SkyquakeRBAC from 'widgets/skyquake_rbac/skyquakeRBAC.jsx';
import 'style/layout.scss';
import {Panel, PanelWrapper} from 'widgets/panel/panel';
import {InputCollection, FormSection} from 'widgets/form_controls/formControls.jsx';
import TextInput from 'widgets/form_controls/textInput.jsx';
import Input from 'widgets/form_controls/input.jsx';
import Button, {ButtonGroup} from 'widgets/button/sq-button.jsx';
import SelectOption from 'widgets/form_controls/selectOption.jsx';
import 'widgets/form_controls/formControls.scss';
import imgAdd from '../../node_modules/open-iconic/svg/plus.svg'
import imgRemove from '../../node_modules/open-iconic/svg/trash.svg'
import _ from 'lodash';
import ROLES from 'utils/roleConstants.js';
import './redundancy.scss';
const PLATFORM = ROLES.PLATFORM;
class SiteManagementDashboard extends React.Component {
constructor(props) {
super(props);
this.Store = this.props.flux.stores.hasOwnProperty('RedundancyStore') ? this.props.flux.stores.RedundancyStore : this.props.flux.createStore(RedundancyStore, 'RedundancyStore');
this.state = this.Store.getState();
this.actions = this.state.actions;
}
componentDidUpdate() {
let self = this;
ReactDOM.findDOMNode(this.siteList).addEventListener('transitionend', this.onTransitionEnd, false);
setTimeout(function() {
let element = self[`site-ref-${self.state.activeIndex}`]
element && !isElementInView(element) && element.scrollIntoView({block: 'end', behavior: 'smooth'});
})
}
componentWillMount() {
this.state = this.Store.getState();
this.Store.getRedundancy();
this.Store.listen(this.updateState);
}
componentWillUnmount() {
this.Store.unlisten(this.updateState);
}
updateState = (state) => {
this.setState(state);
}
updateInput = (key, e) => {
let property = key;
let siteData = this.state.siteData;
siteData[property] = e.target.value;
this.actions.handleUpdateInput({
siteData
})
}
updateServiceTargetInput = (serviceName, key, e) => {
let state = this.state;
let siteData = this.state.siteData;
let value = e.target.value;
let index = _.findIndex(state.siteData['target-endpoint'], function(o) {
return o.name == serviceName
});
if((index == undefined) || index == -1) {
index = siteData['target-endpoint'].push({name: serviceName}) - 1
}
siteData['target-endpoint'][index]['name'] = serviceName;
if(value.trim() == '') {
delete siteData['target-endpoint'][index][key];
} else {
siteData['target-endpoint'][index][key] = value
}
this.actions.handleUpdateInput({
siteData
})
}
updateInstanceInput = (index, key, e) => {
let siteData = this.state.siteData;
siteData['rw-instances'][index][key] = e.target.value;
this.actions.handleUpdateInput({
siteData
})
}
updateInstanceInputEndpoint = (index, serviceName, key, e) => {
let siteData = this.state.siteData;
let state = this.state;
let value = e.target.value;
let listIndex = _.findIndex(siteData['rw-instances'][index].endpoint, function(o) {
return o.name == serviceName
});
if(!siteData['rw-instances'][index].endpoint) {
siteData['rw-instances'][index].endpoint = []
}
if(listIndex == undefined || listIndex == -1) {
listIndex = siteData['rw-instances'][index].endpoint.push({name: serviceName}) - 1;
}
if(value.trim() == '') {
delete siteData['rw-instances'][index].endpoint[listIndex][key];
} else {
siteData['rw-instances'][index].endpoint[listIndex][key] = value
}
siteData['rw-instances'][index].endpoint[listIndex]['name'] = serviceName;
this.actions.handleUpdateInput({
siteData
})
}
addSite = () => {
this.actions.handleAddSite();
}
viewSite = (un, index) => {
this.actions.viewSite(un, index, true);
}
editSite = () => {
this.actions.editSite(false);
}
cancelEditSite = () => {
this.actions.editSite(true)
}
closePanel = () => {
this.actions.handleCloseSitePanel();
}
deleteSite = (e) => {
e.preventDefault();
e.stopPropagation();
if (confirm('Are you sure you want to delete this site?')) {
this.Store.deleteSite({
'site-name': this.state.siteData['site-name']
});
}
}
createSite = (e) => {
let self = this;
e.preventDefault();
e.stopPropagation();
let siteData = self.state.siteData;
if (this.validateFields(self, siteData)) {
this.Store.createSite(siteData);
}
}
updateSite = (e) => {
let self = this;
e.preventDefault();
e.stopPropagation();
let siteData = self.state.siteData;
if (this.validateFields(self, siteData)) {
this.Store.updateSite(siteData);
}
}
validateFields(self, siteData) {
if (!siteData['site-name'] || siteData['site-name'].trim() == '') {
self.props.flux.actions.global.showNotification("Please enter a site name");
return false;
}
let instanceInvalid = false;
siteData['rw-instances'] && siteData['rw-instances'].map(function(rw) {
if (!rw['rwinstance-id'] || rw['rwinstance-id'].trim() == '' ) {
instanceInvalid = true;;
}
});
if (instanceInvalid) {
self.props.flux.actions.global.showNotification("One or more of your RIFT.WARE Instances is missing it's FQDN/IP Address");
return false;
}
return true;
}
evaluateSubmit = (e) => {
if (e.keyCode == 13) {
if (this.props.isEdit) {
this.updateSite(e);
} else {
this.createSite(e);
}
e.preventDefault();
e.stopPropagation();
}
}
onTransitionEnd = (e) => {
this.actions.handleHideColumns(e);
console.log('transition end')
}
render() {
let self = this;
let html;
let props = this.props;
let state = this.state;
let passwordSectionHTML = null;
let formButtonsHTML = (
INSTANCE
{
(state.isReadOnly) ? null :
Remove
}