<commit_before>import React from 'react';
import ReactDOM from 'react-dom';
import autoBind from 'react-autobind';
import Reflux from 'reflux';
import {dataInterface} from '../dataInterface';
import actions from '../actions';
import reactMixin from 'react-mixin';
import bem from '../bem';
import {t, notify, isAValidUrl} from '../utils';
import stores from '../stores';
import ui from '../ui';
import alertify from 'alertifyjs';
import icons from '../../xlform/src/view.icons';
import Select from 'react-select';
import {
  VALIDATION_STATUSES
} from '../constants';


class Submission extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      submission: {},
      loading: true,
      error: false,
      enketoEditLink: false,
      previous: -1, 
      next: -1,
      sid: props.sid
    };
    autoBind(this);
  }
  componentDidMount() {
    this.getSubmission(this.props.asset.uid, this.state.sid);
    this.listenTo(actions.resources.updateSubmissionValidationStatus.completed, this.refreshSubmission);
  }

  refreshSubmission(result, sid) {
    if (result.uid) {
      this.state.submission._validation_status = result;
      this.setState({submission: this.state.submission});
    }
  }

  getSubmission(assetUid, sid) {
    dataInterface.getSubmission(assetUid, sid).done((data) => {
      this.setState({
        submission: data,
        loading: false
      });
      dataInterface.getEnketoEditLink(assetUid, sid).done((data) => {
        if (data.url)
          this.setState({enketoEditLink: data.url});
      });

      if (this.props.ids && sid) {
        const c = this.props.ids.findIndex(k => k==sid);

        if (c > 0)
          this.setState({previous: this.props.ids[c - 1]});
        else
          this.setState({previous: -1});

        if (c < this.props.ids.length)
          this.setState({next: this.props.ids[c + 1]});
        else
          this.setState({next: -1});
      }

    }).fail((error)=>{
      if (error.responseText)
        this.setState({error: error.responseText, loading: false});
      else if (error.statusText)
        this.setState({error: error.statusText, loading: false});
      else
        this.setState({error: t('Error: could not load data.'), loading: false});
    });    
  }

  componentWillReceiveProps(nextProps) {
    this.setState({
      sid: nextProps.sid
    });

    this.getSubmission(nextProps.asset.uid, nextProps.sid);
  }

  deleteSubmission() {
    let dialog = alertify.dialog('confirm');
    let opts = {
      title: t('Delete submission?'),
      message: `${t('Are you sure you want to delete this submission?')} ${t('This action cannot be undone')}.`,
      labels: {ok: t('Delete'), cancel: t('Cancel')},
      onok: (evt, val) => {
        dataInterface.deleteSubmission(this.props.asset.uid, this.props.sid).done((data) => {
          stores.pageState.hideModal();
          notify(t('submission deleted'));
        });
      },
      oncancel: () => {
        dialog.destroy();
      }
    };
    dialog.set(opts).show();
  }

  renderAttachment(filename, type) {
    const s = this.state.submission;
    var download_url = null;

    s._attachments.forEach(function(a) {
      if (a.download_url.includes(encodeURI(filename))) {
        download_url = a.download_url;
      }
    });

    var kc_server = document.createElement('a');
    kc_server.href = this.props.asset.deployment__identifier;
    var kobocollect_url = kc_server.origin;
    if (type === 'image') {
      if (download_url && isAValidUrl(download_url))
        return <img src={download_url} />
      else if (download_url)
        return <img src={`${kobocollect_url}/${download_url}`} />
      else
        return filename;
    } else {
      if (download_url && isAValidUrl(download_url))
        return <a href={download_url}>{filename}</a>
      else if (download_url)
        return <a href={download_url}>{filename}</a>
      else
        return filename;

    }
  }

  switchSubmission(evt) {
    const sid = evt.target.getAttribute('data-sid');
    stores.pageState.showModal({
      type: 'submission',
      sid: sid,
      asset: this.props.asset,
      ids: this.props.ids
    });
  }

  validationStatusChange(e) {
    const data = {"validation_status_uid": e.value};
    actions.resources.updateSubmissionValidationStatus(this.props.asset.uid, this.state.sid, data);
  }
  render () {
    if (this.state.loading) {
      return (
        <bem.Loading>
          <bem.Loading__inner>
            <i />
            {t('loading...')}
          </bem.Loading__inner>
        </bem.Loading>
      );
    }

    if (this.state.error) {
      return (
        <ui.Panel>
          <bem.Loading>
            <bem.Loading__inner>
              {this.state.error}<commit_msg>minor typo fix in validation status API call<commit_after>import React from 'react';
import ReactDOM from 'react-dom';
import autoBind from 'react-autobind';
import Reflux from 'reflux';
import {dataInterface} from '../dataInterface';
import actions from '../actions';
import reactMixin from 'react-mixin';
import bem from '../bem';
import {t, notify, isAValidUrl} from '../utils';
import stores from '../stores';
import ui from '../ui';
import alertify from 'alertifyjs';
import icons from '../../xlform/src/view.icons';
import Select from 'react-select';
import {
  VALIDATION_STATUSES
} from '../constants';


class Submission extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      submission: {},
      loading: true,
      error: false,
      enketoEditLink: false,
      previous: -1, 
      next: -1,
      sid: props.sid
    };
    autoBind(this);
  }
  componentDidMount() {
    this.getSubmission(this.props.asset.uid, this.state.sid);
    this.listenTo(actions.resources.updateSubmissionValidationStatus.completed, this.refreshSubmission);
  }

  refreshSubmission(result, sid) {
    if (result.uid) {
      this.state.submission._validation_status = result;
      this.setState({submission: this.state.submission});
    }
  }

  getSubmission(assetUid, sid) {
    dataInterface.getSubmission(assetUid, sid).done((data) => {
      this.setState({
        submission: data,
        loading: false
      });
      dataInterface.getEnketoEditLink(assetUid, sid).done((data) => {
        if (data.url)
          this.setState({enketoEditLink: data.url});
      });

      if (this.props.ids && sid) {
        const c = this.props.ids.findIndex(k => k==sid);

        if (c > 0)
          this.setState({previous: this.props.ids[c - 1]});
        else
          this.setState({previous: -1});

        if (c < this.props.ids.length)
          this.setState({next: this.props.ids[c + 1]});
        else
          this.setState({next: -1});
      }

    }).fail((error)=>{
      if (error.responseText)
        this.setState({error: error.responseText, loading: false});
      else if (error.statusText)
        this.setState({error: error.statusText, loading: false});
      else
        this.setState({error: t('Error: could not load data.'), loading: false});
    });    
  }

  componentWillReceiveProps(nextProps) {
    this.setState({
      sid: nextProps.sid
    });

    this.getSubmission(nextProps.asset.uid, nextProps.sid);
  }

  deleteSubmission() {
    let dialog = alertify.dialog('confirm');
    let opts = {
      title: t('Delete submission?'),
      message: `${t('Are you sure you want to delete this submission?')} ${t('This action cannot be undone')}.`,
      labels: {ok: t('Delete'), cancel: t('Cancel')},
      onok: (evt, val) => {
        dataInterface.deleteSubmission(this.props.asset.uid, this.props.sid).done((data) => {
          stores.pageState.hideModal();
          notify(t('submission deleted'));
        });
      },
      oncancel: () => {
        dialog.destroy();
      }
    };
    dialog.set(opts).show();
  }

  renderAttachment(filename, type) {
    const s = this.state.submission;
    var download_url = null;

    s._attachments.forEach(function(a) {
      if (a.download_url.includes(encodeURI(filename))) {
        download_url = a.download_url;
      }
    });

    var kc_server = document.createElement('a');
    kc_server.href = this.props.asset.deployment__identifier;
    var kobocollect_url = kc_server.origin;
    if (type === 'image') {
      if (download_url && isAValidUrl(download_url))
        return <img src={download_url} />
      else if (download_url)
        return <img src={`${kobocollect_url}/${download_url}`} />
      else
        return filename;
    } else {
      if (download_url && isAValidUrl(download_url))
        return <a href={download_url}>{filename}</a>
      else if (download_url)
        return <a href={download_url}>{filename}</a>
      else
        return filename;

    }
  }

  switchSubmission(evt) {
    const sid = evt.target.getAttribute('data-sid');
    stores.pageState.showModal({
      type: 'submission',
      sid: sid,
      asset: this.props.asset,
      ids: this.props.ids
    });
  }

  validationStatusChange(e) {
    const data = {"validation_status__uid": e.value};
    actions.resources.updateSubmissionValidationStatus(this.props.asset.uid, this.state.sid, data);
  }
  render () {
    if (this.state.loading) {
      return (
        <bem.Loading>
          <bem.Loading__inner>
            <i />
            {t('loading...')}
          </bem.Loading__inner>
        </bem.Loading>
      );
    }

    if (this.state.error) {
      return (
        <ui.Panel>
          <bem.Loading>
            <bem.Loading__inner>
              {this.state.error}
{'fraction_non_alphanumeric': 0.14837228714524206, 'fraction_numerical': 0.0014607679465776294, 'mean_word_length': 2.5395125553914326, 'pattern_counts': {'":': 2, '<': 33, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3932950', 'n_tokens_mistral': 3004, 'n_tokens_neox': 2985, 'n_words': 604}