<commit_before>import React from 'react';
import PropTypes from 'prop-types'; // see https://github.com/facebook/prop-types#prop-types
import Select from 'react-select';
import { Spin } from 'antd';
import Button from '@gen3/ui-component/dist/components/Button';
import BackLink from '../components/BackLink';
import HIVCohortFilter from '../HIVCohortFilter/HIVCohortFilter';
import { analysisApps } from '../localconf';
import './AnalysisApp.css';

class AnalysisApp extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      jobInput: null,
      loaded: false,
      app: null,
      results: null,
      analysisIsFullscreen: false,
      isIframeApp: false,
    };
  }

  componentDidMount() {
    this.updateApp();
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.job && nextProps.job.status === 'Completed') {
      this.fetchJobResult()
        .then((res) => {
          this.setState({ results: `${res.data.output}`.split('\n') });
        });
    }
  }

  componentWillUnmount() {
    this.props.resetJobState();
  }

  onSubmitJob = (e) => {
    e.preventDefault();
    this.clearResult();
    this.props.submitJob({ organ: this.state.jobInput ? this.state.jobInput.value : null });
    this.props.checkJobStatus();
  }

  getAppContent = (app) => {
    switch (app) {
    case 'vaGWAS':
      return (
        <React.Fragment>
          <Select
            value={this.state.jobInput}
            placeholder='Select your organ'
            options={analysisApps[app].options}
            onChange={this.selectChange}
          />
          <Button label='Run Analysis' buttonType='primary' onClick={this.onSubmitJob} isPending={this.isJobRunning()} />
        </React.Fragment>
      );
    case 'ndhHIV':
      return (
        <HIVCohortFilter />
      );
    case 'ndhVirus':
      return (
        <React.Fragment>
          <input className='text-input' type='text' placeholder='input data' name='input' />
          <Button label='Run' buttonType='primary' onClick={this.onSubmitJob} isPending={this.isJobRunning()} />
        </React.Fragment>
      );
    default:
      this.state.isIframeApp = true;
      return (
        <React.Fragment>
          <div className='analysis-app__iframe-wrapper'>
            <iframe
              className='analysis-app__iframe'
              title='Analysis App'
              frameBorder='0'
              src={`${this.state.app.applicationUrl}`}
            />
          </div>
        </React.Fragment>
      );
    }
  }

  isJobRunning = () => this.props.job && this.props.job.status === 'Running';

  selectChange = (option) => {
    this.setState({
      jobInput: option,
      results: null,
    }, () => {
      if (option === null || this.props.job) {
        this.props.resetJobState();
      }
    });
  }

  fetchJobResult = async () => this.props.fetchJobResult(this.props.job.uid);

  clearResult = () => {
    this.setState({ results: null });
  }

  updateApp = async () => {
    this.setState({
      app: analysisApps[decodeURIComponent(this.props.params.app)],
      loaded: true,
    });
  }

  handleFullscreenButtonClick = () => {
    this.setState({
      analysisIsFullscreen: !this.state.analysisIsFullscreen,
    });
  }

  render() {
    const { job, params } = this.props;
    const { loaded, app, results } = this.state;
    if (!app) {
      return <Spin size='large' tip='Loading App...' />;
    }

    const fullscreenButton = (
      <Button
        className='analysis-app__button'
        onClick={this.handleFullscreenButtonClick}
        label={this.state.analysisIsFullscreen ? 'Exit Fullscreen' : 'Make Fullscreen'}
        buttonType='secondary'
        rightIcon={this.state.analysisIsFullscreen ? 'back' : 'external-link'}<commit_msg>moved state mutation to separate handle function<commit_after>import React from 'react';
import PropTypes from 'prop-types'; // see https://github.com/facebook/prop-types#prop-types
import Select from 'react-select';
import { Spin } from 'antd';
import Button from '@gen3/ui-component/dist/components/Button';
import BackLink from '../components/BackLink';
import HIVCohortFilter from '../HIVCohortFilter/HIVCohortFilter';
import { analysisApps } from '../localconf';
import './AnalysisApp.css';

class AnalysisApp extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      jobInput: null,
      loaded: false,
      app: null,
      results: null,
      analysisIsFullscreen: false,
      isIframeApp: false,
    };
  }

  componentDidMount() {
    this.updateApp();
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.job && nextProps.job.status === 'Completed') {
      this.fetchJobResult()
        .then((res) => {
          this.setState({ results: `${res.data.output}`.split('\n') });
        });
    }
  }

  componentWillUnmount() {
    this.props.resetJobState();
  }

  onSubmitJob = (e) => {
    e.preventDefault();
    this.clearResult();
    this.props.submitJob({ organ: this.state.jobInput ? this.state.jobInput.value : null });
    this.props.checkJobStatus();
  }

  getAppContent = (app) => {
    switch (app) {
    case 'vaGWAS':
      return (
        <React.Fragment>
          <Select
            value={this.state.jobInput}
            placeholder='Select your organ'
            options={analysisApps[app].options}
            onChange={this.selectChange}
          />
          <Button label='Run Analysis' buttonType='primary' onClick={this.onSubmitJob} isPending={this.isJobRunning()} />
        </React.Fragment>
      );
    case 'ndhHIV':
      return (
        <HIVCohortFilter />
      );
    case 'ndhVirus':
      return (
        <React.Fragment>
          <input className='text-input' type='text' placeholder='input data' name='input' />
          <Button label='Run' buttonType='primary' onClick={this.onSubmitJob} isPending={this.isJobRunning()} />
        </React.Fragment>
      );
    default:
      return (
        <React.Fragment>
          <div className='analysis-app__iframe-wrapper'>
            <iframe
              className='analysis-app__iframe'
              title='Analysis App'
              frameBorder='0'
              src={`${this.state.app.applicationUrl}`}
              onLoad={this.handleIframeApp}
            />
          </div>
        </React.Fragment>
      );
    }
  }

  isJobRunning = () => this.props.job && this.props.job.status === 'Running';

  selectChange = (option) => {
    this.setState({
      jobInput: option,
      results: null,
    }, () => {
      if (option === null || this.props.job) {
        this.props.resetJobState();
      }
    });
  }

  fetchJobResult = async () => this.props.fetchJobResult(this.props.job.uid);

  clearResult = () => {
    this.setState({ results: null });
  }

  updateApp = async () => {
    this.setState({
      app: analysisApps[decodeURIComponent(this.props.params.app)],
      loaded: true,
    });
  }

  handleFullscreenButtonClick = () => {
    this.setState({
      analysisIsFullscreen: !this.state.analysisIsFullscreen,
    });
  }

  handleIframeApp = () => {
    this.setState({
      isIframeApp: true,
    });
  }

  render() {
    const { job, params } = this.props;
    const { loaded, app, results } = this.state;
    if (!app) {
      return <Spin size='large' tip='Loading App...' />;
    }

    const fullscreenButton = (
      <Button
        className='analysis-app__button'
        onClick={this.handleFullscreenButtonClick}
        label={this.state.analysisIsFullscreen ? 'Exit Fullscreen' : 'Make Fullscreen'}
        buttonType='secondary'
        rightIcon={this.state.analysisIsFullscreen ? 'back' : 'external-link'}
{'fraction_non_alphanumeric': 0.14844155844155843, 'fraction_numerical': 0.0005194805194805195, 'mean_word_length': 2.4626798561151078, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 54, 'https://': 2, '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': '2334483', 'n_tokens_mistral': 2308, 'n_tokens_neox': 2304, 'n_words': 410}