<commit_before>/*
 * dbKoda - a modern, open source code editor, for MongoDB.
 * Copyright (C) 2017-2018 Southbank Software
 *
 * This file is part of dbKoda.
 *
 * dbKoda is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * dbKoda is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with dbKoda.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @Author: <NAME> <wahaj>
 * @Date:   2017-04-21T10:47:14+10:00
 * @Email:  <EMAIL>
 * @Last modified by:   wahaj
 * @Last modified time: 2017-08-23T15:19:01+10:00
 */

/**
 * connection profile panel class
 */
/* eslint-disable react/jsx-no-bind */
import React from 'react';
import { observer } from 'mobx-react';
import autobind from 'autobind-decorator';
import { Button } from '@blueprintjs/core';
import _ from 'lodash';
import Radio from './Radio';
import Input from './Input';
import FileInput from './FileInput';
import Checkbox from './Checkbox';
import './style.scss';
import Label from './Label';

@observer
export default class Panel extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      connecting: false,
      testing: false,
      checkboxHost: true,
      checkboxUrl: false,
      checkboxSSL: false,
      checkboxScram: false,
      hasAliasChanged: false,
    };
  }

  @autobind
  _onClickHost() {
    this.props.form.$('hostRadio').set('value', true);
    this.props.form.$('urlRadio').set('value', false);
  }

  @autobind
  _onClickURL() {
    this.props.form.$('hostRadio').set('value', false);
    this.props.form.$('urlRadio').set('value', true);
  }

  @autobind
  _onClickUserName() {
    this.props.form.$('sha').set('value', true);
  }

  @autobind
  _onClickAuthDB() {
    this.props.form.$('sha').set('value', true);
  }

  @autobind
  _hostRadioOnChange() {
    if (!this.props.form.$('hostRadio').get('value')) {
      this.props.form
        .$('hostRadio')
        .set('value', !this.props.form.$('hostRadio').get('value'));
      this.props.form
        .$('urlRadio')
        .set('value', !this.props.form.$('hostRadio').get('value'));
    }
  }

  @autobind
  _urlRadioOnChange() {
    if (!this.props.form.$('urlRadio').get('value')) {
      this.props.form
        .$('hostRadio')
        .set('value', !this.props.form.$('hostRadio').get('value'));
      this.props.form
        .$('urlRadio')
        .set('value', !this.props.form.$('hostRadio').get('value'));
    }
  }

  @autobind
  _passRadioOnChange() {
    if (!this.props.form.$('passRadio').get('value')) {
      this.props.form
        .$('passRadio')
        .set('value', !this.props.form.$('passRadio').get('value'));
      this.props.form
        .$('keyRadio')
        .set('value', !this.props.form.$('passRadio').get('value'));
    }
  }

  @autobind
  _keyRadioOnChange() {
    if (!this.props.form.$('keyRadio').get('value')) {
      this.props.form
        .$('passRadio')
        .set('value', !this.props.form.$('passRadio').get('value'));
      this.props.form
        .$('keyRadio')
        .set('value', !this.props.form.$('passRadio').get('value'));
    }
  }

  @autobind
  _onClickRemotePass() {
    this.props.form.$('passRadio').set('value', true);
    this.props.form.$('keyRadio').set('value', false);
  }

  @autobind
  _onClickKey() {
    this.props.form.$('passRadio').set('value', false);
    this.props.form.$('keyRadio').set('value', true);
  }

  @autobind
  _connect(data) {
    this.setState({ connecting: true });
    this.props
      .connect(data)
      .then(() => this.setState({ connecting: false }))
      .catch(() => this.setState({ connecting: false }));
  }

  @autobind
  _test(data) {
    this.setState({ testing: true });
    this.props
      .connect(data)
      .then(() => this.setState({ testing: false }))
      .catch(() => this.setState({ testing: false }));
  }

  @autobind
  _getFormErrors() {
    // invalidate the form with a custom error message
    const errorMsg = [];
    const error = this.props.form.errors();
    _.keys(error).forEach((key) => {
      if (error[key]) {
        errorMsg.push(error[key]);
      }
    });
    return errorMsg;
  }

  @autobind
  _updateAliasState() {
    this.state.hasAliasChanged = true;
  }

  @autobind
  _save(data) {
    this.props.save(data);
  }
  render() {
    const { form, edit, title, profiles } = this.props;
    form.connect = this._connect;
    form.test = this._test;
    form.save = this._save;
    if (
      !edit &&
      form.$('hostRadio').get('value') &&
      profiles &&
      !this.state.hasAliasChanged
    ) {
      form.$('alias').value =
        form.$('host').value +
        ':' +
        form.$('port').value +
        ' - ' +
        (profiles.size + 1);
    } else if (
      !edit &&
      form.$('urlRadio').get('value') &&
      profiles &&
      !this.state.hasAliasChanged
    ) {
      console.log(form.$('url').get('value').length);
      if (form.$('url').get('value').length > 25) {
        if (form.$('url').value.split('//').length > 1) {
          form.$('alias').value = form
            .$('url')
            .value.split('//')[1]
            .substring(0, 25);
        } else {
          form.$('alias').value = form.$('url').value.substring(0, 25);
        }
      } else if (form.$('url').value.split('//').length > 1) {
        console.log(form.$('url').value.split('//')[1]);
        if (form.$('url').value.split('//')[1] === '') {
          form.$('alias').value = 'New Profile - ' + (profiles.size + 1);
        } else {
          form.$('alias').value = form.$('url').value.split('//')[1];
        }
      } else {
        form.$('alias').value = form.$('url').value;
      }
    }

    const formErrors = this._getFormErrors();

    return (
      <div className="pt-dark connection-profile">
        <Button
          className="close-button pt-button pt-intent-primary"
          text="X"
          onClick={this.props.close}
        /><commit_msg>Added Alias Limit for Hostname.<commit_after>/*
 * dbKoda - a modern, open source code editor, for MongoDB.
 * Copyright (C) 2017-2018 Southbank Software
 *
 * This file is part of dbKoda.
 *
 * dbKoda is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * dbKoda is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with dbKoda.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @Author: <NAME> <wahaj>
 * @Date:   2017-04-21T10:47:14+10:00
 * @Email:  <EMAIL>
 * @Last modified by:   wahaj
 * @Last modified time: 2017-08-23T15:19:01+10:00
 */

/**
 * connection profile panel class
 */
/* eslint-disable react/jsx-no-bind */
import React from 'react';
import { observer } from 'mobx-react';
import autobind from 'autobind-decorator';
import { Button } from '@blueprintjs/core';
import _ from 'lodash';
import Radio from './Radio';
import Input from './Input';
import FileInput from './FileInput';
import Checkbox from './Checkbox';
import './style.scss';
import Label from './Label';

const MAX_URL_ALIAS_LENGTH = 25;
const MAX_HOSTNAME_ALIAS_LENGTH = 20;

@observer
export default class Panel extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      connecting: false,
      testing: false,
      checkboxHost: true,
      checkboxUrl: false,
      checkboxSSL: false,
      checkboxScram: false,
      hasAliasChanged: false,
    };
  }

  @autobind
  _onClickHost() {
    this.props.form.$('hostRadio').set('value', true);
    this.props.form.$('urlRadio').set('value', false);
  }

  @autobind
  _onClickURL() {
    this.props.form.$('hostRadio').set('value', false);
    this.props.form.$('urlRadio').set('value', true);
  }

  @autobind
  _onClickUserName() {
    this.props.form.$('sha').set('value', true);
  }

  @autobind
  _onClickAuthDB() {
    this.props.form.$('sha').set('value', true);
  }

  @autobind
  _hostRadioOnChange() {
    if (!this.props.form.$('hostRadio').get('value')) {
      this.props.form
        .$('hostRadio')
        .set('value', !this.props.form.$('hostRadio').get('value'));
      this.props.form
        .$('urlRadio')
        .set('value', !this.props.form.$('hostRadio').get('value'));
    }
  }

  @autobind
  _urlRadioOnChange() {
    if (!this.props.form.$('urlRadio').get('value')) {
      this.props.form
        .$('hostRadio')
        .set('value', !this.props.form.$('hostRadio').get('value'));
      this.props.form
        .$('urlRadio')
        .set('value', !this.props.form.$('hostRadio').get('value'));
    }
  }

  @autobind
  _passRadioOnChange() {
    if (!this.props.form.$('passRadio').get('value')) {
      this.props.form
        .$('passRadio')
        .set('value', !this.props.form.$('passRadio').get('value'));
      this.props.form
        .$('keyRadio')
        .set('value', !this.props.form.$('passRadio').get('value'));
    }
  }

  @autobind
  _keyRadioOnChange() {
    if (!this.props.form.$('keyRadio').get('value')) {
      this.props.form
        .$('passRadio')
        .set('value', !this.props.form.$('passRadio').get('value'));
      this.props.form
        .$('keyRadio')
        .set('value', !this.props.form.$('passRadio').get('value'));
    }
  }

  @autobind
  _onClickRemotePass() {
    this.props.form.$('passRadio').set('value', true);
    this.props.form.$('keyRadio').set('value', false);
  }

  @autobind
  _onClickKey() {
    this.props.form.$('passRadio').set('value', false);
    this.props.form.$('keyRadio').set('value', true);
  }

  @autobind
  _connect(data) {
    this.setState({ connecting: true });
    this.props
      .connect(data)
      .then(() => this.setState({ connecting: false }))
      .catch(() => this.setState({ connecting: false }));
  }

  @autobind
  _test(data) {
    this.setState({ testing: true });
    this.props
      .connect(data)
      .then(() => this.setState({ testing: false }))
      .catch(() => this.setState({ testing: false }));
  }

  @autobind
  _getFormErrors() {
    // invalidate the form with a custom error message
    const errorMsg = [];
    const error = this.props.form.errors();
    _.keys(error).forEach((key) => {
      if (error[key]) {
        errorMsg.push(error[key]);
      }
    });
    return errorMsg;
  }

  @autobind
  _updateAliasState() {
    this.state.hasAliasChanged = true;
  }

  @autobind
  _save(data) {
    this.props.save(data);
  }
  render() {
    const { form, edit, title, profiles } = this.props;
    form.connect = this._connect;
    form.test = this._test;
    form.save = this._save;
    if (
      !edit &&
      form.$('hostRadio').get('value') &&
      profiles &&
      !this.state.hasAliasChanged
    ) {
      if (form.$('host').get('value').length > MAX_HOSTNAME_ALIAS_LENGTH) {
        form.$('alias').value =
          form.$('host').value.substring(0, MAX_HOSTNAME_ALIAS_LENGTH) +
          ':' +
          form.$('port').value +
          ' - ' +
          (profiles.size + 1);
      } else {
        form.$('alias').value =
          form.$('host').value +
          ':' +
          form.$('port').value +
          ' - ' +
          (profiles.size + 1);
      }
    } else if (
      !edit &&
      form.$('urlRadio').get('value') &&
      profiles &&
      !this.state.hasAliasChanged
    ) {
      console.log(form.$('url').get('value').length);
      if (form.$('url').get('value').length > MAX_URL_ALIAS_LENGTH) {
        if (form.$('url').value.split('//').length > 1) {
          form.$('alias').value = form
            .$('url')
            .value.split('//')[1]
            .substring(0, MAX_URL_ALIAS_LENGTH);
        } else {
          form.$('alias').value = form
            .$('url')
            .value.substring(0, MAX_URL_ALIAS_LENGTH);
        }
      } else if (form.$('url').value.split('//').length > 1) {
        console.log(form.$('url').value.split('//')[1]);
        if (form.$('url').value.split('//')[1] === '') {
          form.$('alias').value = 'New Profile - ' + (profiles.size + 1);
        } else {
          form.$('alias').value = form.$('url').value.split('//')[1];
        }
      } else {
        form.$('alias').value = form.$('url').value;
      }
    }

    const formErrors = this._getFormErrors();

    return (
      <div className="pt-dark connection-profile">
        <Button
          className="close-button pt-button pt-intent-primary"
          text="X"
          onClick={this.props.close}
        />
{'fraction_non_alphanumeric': 0.18858654572940287, 'fraction_numerical': 0.009221466364323506, 'mean_word_length': 2.543385109801821, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '5887524', 'n_tokens_mistral': 4402, 'n_tokens_neox': 4275, 'n_words': 864}