<commit_before>// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.

export default class SettingsUpload extends React.Component {
    constructor(props) {
        super(props);

        this.doFileSelect = this.doFileSelect.bind(this);
        this.doSubmit = this.doSubmit.bind(this);
        this.onFileSelect = this.onFileSelect.bind(this);

        this.state = {
            clientError: this.props.clientError,
            serverError: this.props.serverError
        };
    }

    componentWillReceiveProps() {
        this.setState({
            clientError: this.props.clientError,
            serverError: this.props.serverError
        });
    }

    doFileSelect(e) {
        e.preventDefault();
        this.setState({
            clientError: '',
            serverError: ''
        });
    }

    doSubmit(e) {
        e.preventDefault();
        var inputnode = React.findDOMNode(this.refs.uploadinput);
        if (inputnode.files && inputnode.files[0]) {
            this.props.submit(inputnode.files[0]);
        } else {
            this.setState({clientError: 'No file selected.'});
        }
    }

    onFileSelect(e) {
        var filename = $(e.target).val();
        if (filename.substring(3, 11) === 'fakepath') {
            filename = filename.substring(12);
        }
        $(e.target).closest('li').find('.file-status').addClass('hide');
        $(e.target).closest('li').find('.file-name').removeClass('hide').html(filename);
    }

    render() {
        var clientError = null;
        if (this.state.clientError) {
            clientError = (
                <div className='file-status'>{this.state.clientError}</div>
            );
        }
        var serverError = null;
        if (this.state.serverError) {
            serverError = (
                <div className='file-status'>{this.state.serverError}</div>
            );
        }
        return (
            <ul className='section-max'>
                <li className='col-sm-12 section-title'>{this.props.title}</li>
                <li className='col-sm-offset-3 col-sm-9'>{this.props.helpText}</li>
                <li className='col-sm-offset-3 col-sm-9'>
                    <ul className='setting-list'>
                        <li className='setting-list-item'>
                            <span className='btn btn-sm btn-primary btn-file sel-btn'>
                                Select file
                                <input
                                    ref='uploadinput'
                                    accept={this.props.fileTypesAccepted}
                                    type='file'
                                    onChange={this.onFileSelect}
                                />
                            </span>
                            <a
                                className={'btn btn-sm btn-primary'}
                                onClick={this.doSubmit}
                            >
                                Import
                            </a>
                            <div className='file-status file-name hide'></div>
                            {serverError}
                            {clientError}
                        </li>
                    </ul>
                </li>
            </ul>
        );
    }
}

SettingsUpload.propTypes = {
    title: React.PropTypes.string.isRequired,
    submit: React.PropTypes.func.isRequired,
    fileTypesAccepted: React.PropTypes.string.isRequired,
    clientError: React.PropTypes.string,<commit_msg>plt-383 - Slack upload improvement<commit_after>// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.

export default class SettingsUpload extends React.Component {
    constructor(props) {
        super(props);

        this.doFileSelect = this.doFileSelect.bind(this);
        this.doSubmit = this.doSubmit.bind(this);
        this.onFileSelect = this.onFileSelect.bind(this);

        this.state = {
            clientError: this.props.clientError,
            serverError: this.props.serverError
        };
    }

    componentWillReceiveProps() {
        this.setState({
            clientError: this.props.clientError,
            serverError: this.props.serverError
        });
    }

    doFileSelect(e) {
        e.preventDefault();
        this.setState({
            clientError: '',
            serverError: ''
        });
    }

    doSubmit(e) {
        e.preventDefault();
        var inputnode = React.findDOMNode(this.refs.uploadinput);
        if (inputnode.files && inputnode.files[0]) {
            this.props.submit(inputnode.files[0]);
        } else {
            this.setState({clientError: 'No file selected.'});
        }
    }

    onFileSelect(e) {
        var filename = $(e.target).val();
        if (filename.substring(3, 11) === 'fakepath') {
            filename = filename.substring(12);
        }
        $(e.target).closest('li').find('.file-status').addClass('hide');
        $(e.target).closest('li').find('.file-name').removeClass('hide').html(filename);
        $(React.findDOMNode(this.refs.importButton)).removeClass('disabled');
    }

    render() {
        var clientError = null;
        if (this.state.clientError) {
            clientError = (
                <div className='file-status'>{this.state.clientError}</div>
            );
        }
        var serverError = null;
        if (this.state.serverError) {
            serverError = (
                <div className='file-status'>{this.state.serverError}</div>
            );
        }
        return (
            <ul className='section-max'>
                <li className='col-sm-12 section-title'>{this.props.title}</li>
                <li className='col-sm-offset-3 col-sm-9'>{this.props.helpText}</li>
                <li className='col-sm-offset-3 col-sm-9'>
                    <ul className='setting-list'>
                        <li className='setting-list-item'>
                            <span className='btn btn-sm btn-primary btn-file sel-btn'>
                                Select file
                                <input
                                    ref='uploadinput'
                                    accept={this.props.fileTypesAccepted}
                                    type='file'
                                    onChange={this.onFileSelect}
                                />
                            </span>
                            <a
                                ref='importButton'
                                className={'btn btn-sm btn-primary disabled'}
                                onClick={this.doSubmit}
                            >
                                Import
                            </a>
                            <div className='file-status file-name hide'></div>
                            {serverError}
                            {clientError}
                        </li>
                    </ul>
                </li>
            </ul>
        );
    }
}

SettingsUpload.propTypes = {
    title: React.PropTypes.string.isRequired,
    submit: React.PropTypes.func.isRequired,
    fileTypesAccepted: React.PropTypes.string.isRequired,
    clientError: React.PropTypes.string,
{'fraction_non_alphanumeric': 0.12049348869088417, 'fraction_numerical': 0.005071967100753941, 'mean_word_length': 1.3132530120481927, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6599821', 'n_tokens_mistral': 1896, 'n_tokens_neox': 1834, 'n_words': 291}