<commit_before>import React from 'react';
import PropTypes from 'prop-types';
import { Alert, Button, Modal } from 'react-bootstrap';
import Dialog from 'react-bootstrap-dialog';
import { t } from '@superset-ui/translation';
import { SupersetClient } from '@superset-ui/connection';

import getClientErrorObject from '../utils/getClientErrorObject';
import DatasourceEditor from '../datasource/DatasourceEditor';
import withToasts from '../messageToasts/enhancers/withToasts';


const propTypes = {
  onChange: PropTypes.func,
  datasource: PropTypes.object.isRequired,
  show: PropTypes.bool.isRequired,
  onHide: PropTypes.func,
  onDatasourceSave: PropTypes.func,
  addSuccessToast: PropTypes.func.isRequired,
};

const defaultProps = {
  onChange: () => {},
  onHide: () => {},
  onDatasourceSave: () => {},
  show: false,
};

class DatasourceModal extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      errors: [],
      showDatasource: false,
      datasource: props.datasource,
    };
    this.toggleShowDatasource = this.toggleShowDatasource.bind(this);
    this.setSearchRef = this.setSearchRef.bind(this);
    this.onDatasourceChange = this.onDatasourceChange.bind(this);
    this.onClickSave = this.onClickSave.bind(this);
    this.onConfirmSave = this.onConfirmSave.bind(this);
    this.setDialogRef = this.setDialogRef.bind(this);
  }

  onClickSave() {
    this.dialog.show({
      title: t('Confirm save'),
      bsSize: 'medium',
      actions: [
        Dialog.CancelAction(),
        Dialog.OKAction(this.onConfirmSave),
      ],
      body: this.renderSaveDialog(),
    });
  }

  onConfirmSave() {
    SupersetClient.post({
      endpoint: '/datasource/save/',
      postPayload: {
        data: this.state.datasource,
      },
    })
      .then(({ json }) => {
        this.props.addSuccessToast(t('The datasource has been saved'));
        this.props.onDatasourceSave(json);
        this.props.onHide();
      })
      .catch(response =>
        getClientErrorObject(response).then(({ error, statusText }) => {
          this.dialog.show({
            title: 'Error',
            bsSize: 'medium',
            bsStyle: 'danger',
            actions: [
              Dialog.DefaultAction('Ok', () => {}, 'btn-danger'),
            ],
            body: error || statusText || t('An error has occurred'),
          });
        }),
      );
  }

  onDatasourceChange(datasource, errors) {
    this.setState({ datasource, errors });
  }

  setSearchRef(searchRef) {
    this.searchRef = searchRef;
  }

  setDialogRef(ref) {
    this.dialog = ref;
  }

  toggleShowDatasource() {
    this.setState({ showDatasource: !this.state.showDatasource });
  }

  renderSaveDialog() {
    return (
      <div>
        <Alert bsStyle="warning" className="pointer" onClick={this.hideAlert}>
          <div>
            <i className="fa fa-exclamation-triangle" />{' '}
            {t(`The data source configuration exposed here
                affects all the charts using this datasource.
                Be mindful that changing settings
                here may affect other charts
                in undesirable ways.`)}
          </div>
        </Alert>
        {t('Are you sure you want to save and apply changes?')}
      </div>
    );
  }

  render() {
    return (
      <Modal
        show={this.props.show}
        onHide={this.props.onHide}
        bsSize="lg"
      >
        <Modal.Header closeButton>
          <Modal.Title>
            <div>
              <span className="float-left">
                {t('Datasource Editor for ')}
                <strong>{this.props.datasource.name}</strong>
              </span>
            </div>
          </Modal.Title>
        </Modal.Header>
        <Modal.Body>
          {this.props.show &&
            <DatasourceEditor
              datasource={this.props.datasource}
              onChange={this.onDatasourceChange}
            />}
        </Modal.Body>
        <Modal.Footer>
          <span className="float-right"><commit_msg>Add See Tableview Form button to the Datasource Editor modal (#6430)<commit_after>import React from 'react';
import PropTypes from 'prop-types';
import { Alert, Button, Modal } from 'react-bootstrap';
import Dialog from 'react-bootstrap-dialog';
import { t } from '@superset-ui/translation';
import { SupersetClient } from '@superset-ui/connection';

import getClientErrorObject from '../utils/getClientErrorObject';
import DatasourceEditor from '../datasource/DatasourceEditor';
import withToasts from '../messageToasts/enhancers/withToasts';


const propTypes = {
  onChange: PropTypes.func,
  datasource: PropTypes.object.isRequired,
  show: PropTypes.bool.isRequired,
  onHide: PropTypes.func,
  onDatasourceSave: PropTypes.func,
  addSuccessToast: PropTypes.func.isRequired,
};

const defaultProps = {
  onChange: () => {},
  onHide: () => {},
  onDatasourceSave: () => {},
  show: false,
};

class DatasourceModal extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      errors: [],
      showDatasource: false,
      datasource: props.datasource,
    };
    this.toggleShowDatasource = this.toggleShowDatasource.bind(this);
    this.setSearchRef = this.setSearchRef.bind(this);
    this.onDatasourceChange = this.onDatasourceChange.bind(this);
    this.onClickSave = this.onClickSave.bind(this);
    this.onConfirmSave = this.onConfirmSave.bind(this);
    this.setDialogRef = this.setDialogRef.bind(this);
  }

  onClickSave() {
    this.dialog.show({
      title: t('Confirm save'),
      bsSize: 'medium',
      actions: [
        Dialog.CancelAction(),
        Dialog.OKAction(this.onConfirmSave),
      ],
      body: this.renderSaveDialog(),
    });
  }

  onConfirmSave() {
    SupersetClient.post({
      endpoint: '/datasource/save/',
      postPayload: {
        data: this.state.datasource,
      },
    })
      .then(({ json }) => {
        this.props.addSuccessToast(t('The datasource has been saved'));
        this.props.onDatasourceSave(json);
        this.props.onHide();
      })
      .catch(response =>
        getClientErrorObject(response).then(({ error, statusText }) => {
          this.dialog.show({
            title: 'Error',
            bsSize: 'medium',
            bsStyle: 'danger',
            actions: [
              Dialog.DefaultAction('Ok', () => {}, 'btn-danger'),
            ],
            body: error || statusText || t('An error has occurred'),
          });
        }),
      );
  }

  onDatasourceChange(datasource, errors) {
    this.setState({ datasource, errors });
  }

  setSearchRef(searchRef) {
    this.searchRef = searchRef;
  }

  setDialogRef(ref) {
    this.dialog = ref;
  }

  toggleShowDatasource() {
    this.setState({ showDatasource: !this.state.showDatasource });
  }

  renderSaveDialog() {
    return (
      <div>
        <Alert bsStyle="warning" className="pointer" onClick={this.hideAlert}>
          <div>
            <i className="fa fa-exclamation-triangle" />{' '}
            {t(`The data source configuration exposed here
                affects all the charts using this datasource.
                Be mindful that changing settings
                here may affect other charts
                in undesirable ways.`)}
          </div>
        </Alert>
        {t('Are you sure you want to save and apply changes?')}
      </div>
    );
  }

  render() {
    return (
      <Modal
        show={this.props.show}
        onHide={this.props.onHide}
        bsSize="lg"
      >
        <Modal.Header closeButton>
          <Modal.Title>
            <div>
              <span className="float-left">
                {t('Datasource Editor for ')}
                <strong>{this.props.datasource.name}</strong>
              </span>
            </div>
          </Modal.Title>
        </Modal.Header>
        <Modal.Body>
          {this.props.show &&
            <DatasourceEditor
              datasource={this.props.datasource}
              onChange={this.onDatasourceChange}
            />}
        </Modal.Body>
        <Modal.Footer>
          <span className="float-left">
            <Button
              bsSize="sm"
              bsStyle="default"
              target="_blank"
              href={this.props.datasource.edit_url}
            >
              {t('Use Legacy Datasource Editor')}
            </Button>
          </span>

          <span className="float-right">
{'fraction_non_alphanumeric': 0.13273287508888362, 'fraction_numerical': 0.00047404598246029864, 'mean_word_length': 2.216082317073171, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7385706', 'n_tokens_mistral': 2513, 'n_tokens_neox': 2457, 'n_words': 474}