<commit_before>import React, { PropTypes } from 'react';
import { Link, browserHistory } from 'react-router';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import CircularProgress from 'material-ui/CircularProgress';
import * as nonghDemoModelActions from '../../../actions/nonghDemoModelActions';
import rangeCheck from 'range_check';
import { getDeployed } from '../../../api/Nongh/getDeployed';
import { getWebAppStatus } from '../../../api/Generic/getWebAppStatus';
import RaisedButton from 'material-ui/RaisedButton';
import StopNow from 'material-ui/svg-icons/action/pan-tool';
import Checkbox from 'material-ui/Checkbox';
import GoAhead from 'material-ui/svg-icons/action/check-circle';
import { red500, green500 } from 'material-ui/styles/colors';
import TextField from 'material-ui/TextField';
import toastr from 'toastr';

toastr.options.closeButton = true;

class RegisterPage extends React.Component {
  constructor(props, context) {
    super(props, context);
    this.state = {
      showOutput: 'hidden',
      id: Math.floor(Math.random()*10000000).toString(),
      currentProject: {},
      nonghDemoModel: {},
      name: '',
      description: '',
      nameErrorText: '',
      addressErrorText: '',
      portErrorText: '',
      address: '',
      port: '',
      currentPort: '',
      webappaddress: '',
      tempwebaddress: '',
      deploymentBoxSelectedStatus: false,
      webappUnreachableErrorText: '',
      webappLocalUnreachableErrorText: '',
      showLocalDeploymentCheckBox: false,
      returning: false
    };
    this.socket = this.context.socket;
    this.toggleShow = this.toggleShow.bind(this);
    this.updateDemoModelData = this.updateDemoModelData.bind(this);
    this.onLocalDeploymentCheckBoxCheck = this.onLocalDeploymentCheckBoxCheck.bind(this);
    this.updateDescription = this.updateDescription.bind(this);
    this.updateAddress = this.updateAddress.bind(this);
    this.updateName = this.updateName.bind(this);
    this.updatePort = this.updatePort.bind(this);
    this.validateTempwebaddress = this.validateTempwebaddress.bind(this);
    this.validateIP = this.validateIP.bind(this);
    this.validatePort= this.validatePort.bind(this);
  }

  componentWillMount() {
    getDeployed(this.props.params.repoId).then(singleRepo => {
      if (this.props.params.repoId) {
        if (JSON.parse(singleRepo).length != 0) {
          this.setState({returning: true});
          this.setState({tempwebaddress: JSON.parse(singleRepo)[0].token.split(':')[1]});
          if (JSON.parse(singleRepo)[0].token.split(':')[1] === '0.0.0.0') {
            this.setState({showLocalDeploymentCheckBox: true});
          }
          this.setState({id: JSON.parse(singleRepo)[0].id});
          this.setState({name: JSON.parse(singleRepo)[0].name});
          this.setState({address: JSON.parse(singleRepo)[0].token.split(':')[1]});
          this.setState({tempwebaddress: JSON.parse(singleRepo)[0].token.split(':')[5]});
          this.setState({port: JSON.parse(singleRepo)[0].token.split(':')[4]});
          this.setState({description: JSON.parse(singleRepo)[0].description});
          if (JSON.parse(singleRepo)[0].token.split(':')[5] === '0.0.0.0') {
            this.setState({deploymentBoxSelectedStatus: true});
          }
      }
      }
    }).then(() => {
      this.socket.emit('fetchcurrentport');
      this.socket.emit('getpublicipaddress');
      this.socket.on('fetchedcurrentport', (port) => {
        this.setState({currentPort: port});
      });
      this.socket.on('gotpublicip', (ip) => {
        this.setState({webappaddress: ip}, () => {
          if (this.state.tempwebaddress.length == 0) {
            this.setState({tempwebaddress: this.state.webappaddress});
          }
        });
        getWebAppStatus(ip).then(() => {
        }).catch(err => {
          this.setState({webappUnreachableErrorText: 'This WebApp cannot be reached on its public IP'});
          this.setState({showLocalDeploymentCheckBox: true});
        });
        this.toggleShow();
      });
      this.socket.on('erroringettingpublicip', err => {
        toastr.error('Error in getting public IP :(');
      });
    });
  }

  componentWillReceiveProps(nextProps) {
    if (this.state.nonghDemoModel != nextProps.nonghDemoModel) {
      this.setState({nonghDemoModel: nextProps.nonghDemoModel});
    }
  }

  onLocalDeploymentCheckBoxCheck(e) {
    if (!this.state.deploymentBoxSelectedStatus) {
      getWebAppStatus('0.0.0.0').then(() => {
        })
        .catch(err => {
          this.setState({webappLocalUnreachableErrorText: "This WebApp cannot be reached locally on 0.0.0.0"});
        });
    }
    let selectionPool = ['0.0.0.0', this.state.webappaddress];
    this.setState({tempwebaddress: selectionPool[this.state.deploymentBoxSelectedStatus ? 1 : 0]});
    this.setState({deploymentBoxSelectedStatus: !this.state.deploymentBoxSelectedStatus});
  }

  updateDemoModelData() {
    if (!this.validateIP()) {
      this.setState({addressErrorText: "Invalid IP address"});
    } else {
      this.setState({addressErrorText: ""});
    }
    if (!this.validatePort(this.state.port)) {
      this.setState({portErrorText: "Invalid port number"});
    } else {
      this.setState({portErrorText: ""});
    }
    if (this.state.name.length == 0) {
      this.setState({nameErrorText: "Invalid Project Name"});
    } else {
      this.setState({nameErrorText: ""});
    }
    if (this.state.name.length > 0 && this.validateIP() && this.validatePort(this.state.port)) {
      let dataToPut = {
        name: this.state.name,
        id: this.state.id,
        address: this.state.address,
        description: this.state.description,
        timestamp: Date.now(),
        token: `nongh:${this.state.address}:${this.state.id}:${this.state.currentPort}:${this.state.port}:${this.state.tempwebaddress}`,
        status: 'input'
      };
      this.props.nonghModelActions.addToDBNonGHDemoModel(dataToPut).then(() => {
        this.props.nonghModelActions.updateNonGHDemoModel(dataToPut).then(() => {
          browserHistory.push(`/ngh/user/${this.state.name}/${this.state.id}/inputcomponent`);
        });
      });
    }
  }<commit_msg>added fix for Demo button not showing up when registration info is modified in non-gh deployments<commit_after>import React, { PropTypes } from 'react';
import { Link, browserHistory } from 'react-router';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import CircularProgress from 'material-ui/CircularProgress';
import * as nonghDemoModelActions from '../../../actions/nonghDemoModelActions';
import rangeCheck from 'range_check';
import { getDeployed } from '../../../api/Nongh/getDeployed';
import { getWebAppStatus } from '../../../api/Generic/getWebAppStatus';
import RaisedButton from 'material-ui/RaisedButton';
import StopNow from 'material-ui/svg-icons/action/pan-tool';
import Checkbox from 'material-ui/Checkbox';
import GoAhead from 'material-ui/svg-icons/action/check-circle';
import { red500, green500 } from 'material-ui/styles/colors';
import TextField from 'material-ui/TextField';
import toastr from 'toastr';

toastr.options.closeButton = true;

class RegisterPage extends React.Component {
  constructor(props, context) {
    super(props, context);
    this.state = {
      showOutput: 'hidden',
      id: Math.floor(Math.random()*10000000).toString(),
      currentProject: {},
      nonghDemoModel: {},
      name: '',
      description: '',
      nameErrorText: '',
      addressErrorText: '',
      portErrorText: '',
      address: '',
      port: '',
      currentPort: '',
      webappaddress: '',
      tempwebaddress: '',
      deploymentBoxSelectedStatus: false,
      status: '',
      webappUnreachableErrorText: '',
      webappLocalUnreachableErrorText: '',
      showLocalDeploymentCheckBox: false,
      returning: false
    };
    this.socket = this.context.socket;
    this.toggleShow = this.toggleShow.bind(this);
    this.updateDemoModelData = this.updateDemoModelData.bind(this);
    this.onLocalDeploymentCheckBoxCheck = this.onLocalDeploymentCheckBoxCheck.bind(this);
    this.updateDescription = this.updateDescription.bind(this);
    this.updateAddress = this.updateAddress.bind(this);
    this.updateName = this.updateName.bind(this);
    this.updatePort = this.updatePort.bind(this);
    this.validateTempwebaddress = this.validateTempwebaddress.bind(this);
    this.validateIP = this.validateIP.bind(this);
    this.validatePort= this.validatePort.bind(this);
  }

  componentWillMount() {
    getDeployed(this.props.params.repoId).then(singleRepo => {
      if (this.props.params.repoId) {
        if (JSON.parse(singleRepo).length != 0) {
          this.setState({returning: true});
          this.setState({tempwebaddress: JSON.parse(singleRepo)[0].token.split(':')[1]});
          if (JSON.parse(singleRepo)[0].token.split(':')[1] === '0.0.0.0') {
            this.setState({showLocalDeploymentCheckBox: true});
          }
          this.setState({id: JSON.parse(singleRepo)[0].id});
          this.setState({name: JSON.parse(singleRepo)[0].name});
          this.setState({status: JSON.parse(singleRepo)[0].status});
          this.setState({address: JSON.parse(singleRepo)[0].token.split(':')[1]});
          this.setState({tempwebaddress: JSON.parse(singleRepo)[0].token.split(':')[5]});
          this.setState({port: JSON.parse(singleRepo)[0].token.split(':')[4]});
          this.setState({description: JSON.parse(singleRepo)[0].description});
          if (JSON.parse(singleRepo)[0].token.split(':')[5] === '0.0.0.0') {
            this.setState({deploymentBoxSelectedStatus: true});
          }
      }
      }
    }).then(() => {
      this.socket.emit('fetchcurrentport');
      this.socket.emit('getpublicipaddress');
      this.socket.on('fetchedcurrentport', (port) => {
        this.setState({currentPort: port});
      });
      this.socket.on('gotpublicip', (ip) => {
        this.setState({webappaddress: ip}, () => {
          if (this.state.tempwebaddress.length == 0) {
            this.setState({tempwebaddress: this.state.webappaddress});
          }
        });
        getWebAppStatus(ip).then(() => {
        }).catch(err => {
          this.setState({webappUnreachableErrorText: 'This WebApp cannot be reached on its public IP'});
          this.setState({showLocalDeploymentCheckBox: true});
        });
        this.toggleShow();
      });
      this.socket.on('erroringettingpublicip', err => {
        toastr.error('Error in getting public IP :(');
      });
    });
  }

  componentWillReceiveProps(nextProps) {
    if (this.state.nonghDemoModel != nextProps.nonghDemoModel) {
      this.setState({nonghDemoModel: nextProps.nonghDemoModel});
    }
  }

  onLocalDeploymentCheckBoxCheck(e) {
    if (!this.state.deploymentBoxSelectedStatus) {
      getWebAppStatus('0.0.0.0').then(() => {
        })
        .catch(err => {
          this.setState({webappLocalUnreachableErrorText: "This WebApp cannot be reached locally on 0.0.0.0"});
        });
    }
    let selectionPool = ['0.0.0.0', this.state.webappaddress];
    this.setState({tempwebaddress: selectionPool[this.state.deploymentBoxSelectedStatus ? 1 : 0]});
    this.setState({deploymentBoxSelectedStatus: !this.state.deploymentBoxSelectedStatus});
  }

  updateDemoModelData() {
    if (!this.validateIP()) {
      this.setState({addressErrorText: "Invalid IP address"});
    } else {
      this.setState({addressErrorText: ""});
    }
    if (!this.validatePort(this.state.port)) {
      this.setState({portErrorText: "Invalid port number"});
    } else {
      this.setState({portErrorText: ""});
    }
    if (this.state.name.length == 0) {
      this.setState({nameErrorText: "Invalid Project Name"});
    } else {
      this.setState({nameErrorText: ""});
    }
    if (this.state.name.length > 0 && this.validateIP() && this.validatePort(this.state.port)) {
      let dataToPut = {
        name: this.state.name,
        id: this.state.id,
        address: this.state.address,
        description: this.state.description,
        timestamp: Date.now(),
        token: `nongh:${this.state.address}:${this.state.id}:${this.state.currentPort}:${this.state.port}:${this.state.tempwebaddress}`,
        status: this.state.status || 'input'
      };
      this.props.nonghModelActions.addToDBNonGHDemoModel(dataToPut).then(() => {
        this.props.nonghModelActions.updateNonGHDemoModel(dataToPut).then(() => {
          browserHistory.push(`/ngh/user/${this.state.name}/${this.state.id}/inputcomponent`);
        });
      });
    }
  }
{'fraction_non_alphanumeric': 0.16052506721492962, 'fraction_numerical': 0.008777479044757236, 'mean_word_length': 3.243959731543624, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 10, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5215596', 'n_tokens_mistral': 3886, 'n_tokens_neox': 3902, 'n_words': 575}