// 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, fileSelected: '' }; } componentWillReceiveProps() { this.setState({ clientError: this.props.clientError, serverError: this.props.serverError, fileSelected: '' }); } doFileSelect(e) { e.preventDefault(); this.setState({ clientError: '', serverError: '', fileSelected: '' }); } 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); } this.setState({ fileSelected: filename }); } render() { var clientError = null; if (this.state.clientError) { clientError = (
{this.state.clientError}
); } var serverError = null; if (this.state.serverError) { serverError = (
{this.state.serverError}
); } var fileSelected = (
{'Select file'} {'Import'}
); if (this.state.fileSelected) { fileSelected = (
{'Select file'} {'Import'}
{this.state.fileSelected}
); } return ( ); } } SettingsUpload.propTypes = { title: React.PropTypes.string.isRequired, submit: React.PropTypes.func.isRequired, fileTypesAccepted: React.PropTypes.string.isRequired, clientError: React.PropTypes.string, serverError: React.PropTypes.string, helpText: React.PropTypes.object };