// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See License.txt for license information. import $ from 'jquery'; import ReactDOM from 'react-dom'; import {FormattedMessage} from 'react-intl'; import PropTypes from 'prop-types'; import React from 'react'; export default class SettingsUpload extends React.Component { constructor(props) { super(props); this.doFileSelect = this.doFileSelect.bind(this); this.doSubmit = this.doSubmit.bind(this); this.state = { clientError: this.props.clientError, serverError: this.props.serverError, filename: '' }; } componentWillReceiveProps() { this.setState({ clientError: this.props.clientError, serverError: this.props.serverError }); } doFileSelect(e) { e.preventDefault(); var filename = $(e.target).val(); if (filename.substring(3, 11) === 'fakepath') { filename = filename.substring(12); } this.setState({ clientError: '', serverError: '', filename }); } doSubmit(e) { e.preventDefault(); var inputnode = ReactDOM.findDOMNode(this.refs.uploadinput); if (inputnode.files && inputnode.files[0]) { this.props.submit(inputnode.files[0]); } else { this.setState({clientError: true}); } } render() { let clientError = null; if (this.state.clientError) { clientError = (
); } let serverError = null; if (this.state.serverError) { serverError = (
{this.state.serverError}
); } let fileNameText = null; let submitButtonClass = 'btn btn-sm btn-primary disabled'; if (this.state.filename) { fileNameText = (
{this.state.filename}
); submitButtonClass = 'btn btn-sm btn-primary'; } return ( ); } } SettingsUpload.propTypes = { title: PropTypes.string.isRequired, submit: PropTypes.func.isRequired, fileTypesAccepted: PropTypes.string.isRequired, clientError: PropTypes.string, serverError: PropTypes.string, helpText: PropTypes.object };