<commit_before>/**
  * CrossFileOverlay.jsx
  * Created by <NAME> 6/16/16
  **/
import React from 'react';
import _ from 'lodash';
import { hashHistory } from 'react-router';
import * as Icons from '../SharedComponents/icons/Icons.jsx';
import CommonOverlay from '../SharedComponents/overlays/CommonOverlay.jsx';
import LoadingBauble from '../SharedComponents/overlays/LoadingBauble.jsx';

const defaultProps = {
	errors: ['error'],
	loading: true
};

class CrossFileLoadingDetail extends React.Component {
	render() {
		return (
			<div>
				You can return to this page at any time to check the validation status by using this link:
				<br />
				<a href={window.location.href}>{window.location.href}</a>
			</div>
		)
	}
}

export default class CrossFileOverlay extends React.Component {

	constructor(props) {
		super(props);

		this.defaultOverlay = {
			icon: <Icons.ExclamationCircle />,
			iconClass: 'usa-da-errorRed',
			uploadButtonClass: '-disabled',
			uploadButtonDisabled: true,
			nextButtonClass: ' hide',
			nextButtonDisabled: true,
			hideButtons: false,
			message: 'You must the correct the cross-file validation errors listed above.',
			detail: null,
			buttonText: 'Upload Corrected CSV Files'
		};

		this.state = {
			allowUpload: false,
			overlay: this.defaultOverlay
		};
	}

	componentDidMount() {
		this.prepareOverlayContents();
	}

	componentDidUpdate(prevProps, prevState) {
		if (!_.isEqual(prevProps.submission.files, this.props.submission.files) || !_.isEqual(prevProps.submission.crossFile, this.props.submission.crossFile)) {
			console.log("CHANGE");
			this.setState({
				allowUpload: this.isReadyForUpload()
			}, () => {
				this.prepareOverlayContents();
			});
		}
	}

	pressedNext(e) {
		e.preventDefault();
		hashHistory.push('/generateEF/' + this.props.submission.id);
	}

	isUploadingFiles() {
		if (Object.keys(this.props.submission.files).length > 0) {
			return true;
		}
		return false;
	}

	isReadyForUpload() {

		const expectedPairs = [];
		for (let pair of this.props.submission.crossFileOrder) {
			expectedPairs.push(pair.key);
		}

		for (let key in this.props.submission.crossFile) {

			// check if this is a valid cross-file pair
			if (_.indexOf(expectedPairs, key) == -1) {
				continue;
			}
			
			const firstKey = key.split('-')[0];
			const secondKey = key.split('-')[1];

			if (!this.props.submission.files.hasOwnProperty(firstKey) && !this.props.submission.files.hasOwnProperty(secondKey)) {
				// neither file in the pair is staged for upload, submission isn't ready for re-upload
				return false;
			}
		}

		// if we hit this point, the files are ready
		return true;
	}

	prepareOverlayContents() {
		// prepare the props to send down the common overlay component
		// create a clone of the default state
		const overlay = Object.assign({}, this.defaultOverlay);

		if (this.props.loading) {
			// still loading data
			overlay.uploadButtonDisabled = true;
			overlay.uploadButtonClass = ' hide';
			overlay.nextButtonClass = ' hide';
			overlay.nextButtonDisabled = true;
			overlay.icon = <LoadingBauble />;
			overlay.iconClass = 'overlay-animation';
			overlay.message = 'Your files are being validated.';
			overlay.hideButtons = true;
			overlay.detail = <CrossFileLoadingDetail />;
		}
		else if (this.props.mode == 'success') {
			// loading finished, show success (default state is to show errors)
			overlay.icon = <Icons.CheckCircle />;
			overlay.iconClass = 'usa-da-successGreen';
			overlay.message = 'Your files have been successfully cross-validated.';
			overlay.detail = 'Click Next to generate Files E and F.';
			overlay.uploadButtonDisabled = true;
			overlay.uploadButtonClass = '-disabled';
			overlay.nextButtonClass = ' btn-primary';
			overlay.nextButtonDisabled = false;
		}
		else if (this.props.mode == 'warnings') {
			// loading finishe,d show warnings
			overlay.icon = <Icons.ExclamationCircle />;
			overlay.iconClass = 'usa-da-warningYellow';
			overlay.message = 'Some cross-file pairs have validation warnings.';
			overlay.detail = 'You can either correct your files or click Next to generate Files E and F.';
			overlay.uploadButtonDisabled = true;
			overlay.uploadButtonClass = '-disabled';
			overlay.nextButtonClass = ' btn-primary';
			overlay.nextButtonDisabled = false;
		}

		// handle uploading events
		if (this.props.submission.state == 'uploading') {
			overlay.uploadButtonDisabled = true;
			overlay.uploadButtonClass = '-disabled';
			overlay.nextButtonDisabled = true;
			overlay.nextButtonClass = '-disabled';
			overlay.buttonText = 'Uploading files...';
		}
		else if (this.props.submission.state == 'prepare') { 
			// new files uploaded, page is now reloading
			overlay.uploadButtonDisabled = true;
			overlay.uploadButtonDisabled = '-disabled';<commit_msg>Bug fix to handle delayed state/data changes<commit_after>/**
  * CrossFileOverlay.jsx
  * Created by <NAME> 6/16/16
  **/
import React from 'react';
import _ from 'lodash';
import { hashHistory } from 'react-router';
import * as Icons from '../SharedComponents/icons/Icons.jsx';
import CommonOverlay from '../SharedComponents/overlays/CommonOverlay.jsx';
import LoadingBauble from '../SharedComponents/overlays/LoadingBauble.jsx';

const defaultProps = {
	errors: ['error'],
	loading: true
};

class CrossFileLoadingDetail extends React.Component {
	render() {
		return (
			<div>
				You can return to this page at any time to check the validation status by using this link:
				<br />
				<a href={window.location.href}>{window.location.href}</a>
			</div>
		)
	}
}

export default class CrossFileOverlay extends React.Component {

	constructor(props) {
		super(props);

		this.defaultOverlay = {
			icon: <Icons.ExclamationCircle />,
			iconClass: 'usa-da-errorRed',
			uploadButtonClass: '-disabled',
			uploadButtonDisabled: true,
			nextButtonClass: ' hide',
			nextButtonDisabled: true,
			hideButtons: false,
			message: 'You must the correct the cross-file validation errors listed above.',
			detail: null,
			buttonText: 'Upload Corrected CSV Files'
		};

		this.state = {
			allowUpload: false,
			overlay: this.defaultOverlay
		};
	}

	componentDidMount() {
		this.prepareOverlayContents();
	}

	componentDidUpdate(prevProps, prevState) {
		if (!_.isEqual(prevProps.submission.files, this.props.submission.files) || !_.isEqual(prevProps.submission.crossFile, this.props.submission.crossFile)) {
			this.setState({
				allowUpload: this.isReadyForUpload()
			}, () => {
				this.prepareOverlayContents();
			});
		}
		else if (prevProps.loading != this.props.loading || prevProps.mode != this.props.mode) {
			this.prepareOverlayContents();
		}
	}

	pressedNext(e) {
		e.preventDefault();
		hashHistory.push('/generateEF/' + this.props.submission.id);
	}

	isUploadingFiles() {
		if (Object.keys(this.props.submission.files).length > 0) {
			return true;
		}
		return false;
	}

	isReadyForUpload() {

		const expectedPairs = [];
		for (let pair of this.props.submission.crossFileOrder) {
			expectedPairs.push(pair.key);
		}

		for (let key in this.props.submission.crossFile) {

			// check if this is a valid cross-file pair
			if (_.indexOf(expectedPairs, key) == -1) {
				continue;
			}
			
			const firstKey = key.split('-')[0];
			const secondKey = key.split('-')[1];

			if (!this.props.submission.files.hasOwnProperty(firstKey) && !this.props.submission.files.hasOwnProperty(secondKey)) {
				// neither file in the pair is staged for upload, submission isn't ready for re-upload
				return false;
			}
		}

		// if we hit this point, the files are ready
		return true;
	}

	prepareOverlayContents() {
		// prepare the props to send down the common overlay component
		// create a clone of the default state
		const overlay = Object.assign({}, this.defaultOverlay);

		if (this.props.loading) {
			// still loading data
			overlay.uploadButtonDisabled = true;
			overlay.uploadButtonClass = ' hide';
			overlay.nextButtonClass = ' hide';
			overlay.nextButtonDisabled = true;
			overlay.icon = <LoadingBauble />;
			overlay.iconClass = 'overlay-animation';
			overlay.message = 'Your files are being validated.';
			overlay.hideButtons = true;
			overlay.detail = <CrossFileLoadingDetail />;
		}
		else if (this.props.mode == 'success') {
			// loading finished, show success (default state is to show errors)
			overlay.icon = <Icons.CheckCircle />;
			overlay.iconClass = 'usa-da-successGreen';
			overlay.message = 'Your files have been successfully cross-validated.';
			overlay.detail = 'Click Next to generate Files E and F.';
			overlay.uploadButtonDisabled = true;
			overlay.uploadButtonClass = '-disabled';
			overlay.nextButtonClass = ' btn-primary';
			overlay.nextButtonDisabled = false;
		}
		else if (this.props.mode == 'warnings') {
			// loading finishe,d show warnings
			overlay.icon = <Icons.ExclamationCircle />;
			overlay.iconClass = 'usa-da-warningYellow';
			overlay.message = 'Some cross-file pairs have validation warnings.';
			overlay.detail = 'You can correct your files or click Next to generate Files E and F.';
			overlay.uploadButtonDisabled = true;
			overlay.uploadButtonClass = '-disabled';
			overlay.nextButtonClass = ' btn-primary';
			overlay.nextButtonDisabled = false;
		}

		// handle uploading events
		if (this.props.submission.state == 'uploading') {
			overlay.uploadButtonDisabled = true;
			overlay.uploadButtonClass = '-disabled';
			overlay.nextButtonDisabled = true;
			overlay.nextButtonClass = '-disabled';
			overlay.buttonText = 'Uploading files...';
		}
		else if (this.props.submission.state == 'prepare') { 
			// new files uploaded, page is now reloading
			overlay.uploadButtonDisabled = true;
			overlay.uploadButtonDisabled = '-disabled';
{'fraction_non_alphanumeric': 0.12537282731667182, 'fraction_numerical': 0.0018512804689910522, 'mean_word_length': 3.693050193050193, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 29, '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': '3394108', 'n_tokens_mistral': 3354, 'n_tokens_neox': 2930, 'n_words': 742}