Files
mattermost/webapp/components/setting_upload.jsx

130 lines
4.2 KiB
React
Raw Normal View History

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
2015-07-07 09:16:13 -04:00
// See License.txt for license information.
2016-03-14 08:50:46 -04:00
import $ from 'jquery';
import ReactDOM from 'react-dom';
import {FormattedMessage} from 'react-intl';
import PropTypes from 'prop-types';
2016-03-14 08:50:46 -04:00
import React from 'react';
2015-09-01 17:06:31 -07:00
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 = {
2015-07-07 09:16:13 -04:00
clientError: this.props.clientError,
2015-09-29 20:53:30 +05:00
serverError: this.props.serverError,
2015-09-30 19:35:35 +05:00
filename: ''
2015-07-07 09:16:13 -04:00
};
2015-09-01 17:06:31 -07:00
}
componentWillReceiveProps() {
2015-07-07 09:16:13 -04:00
this.setState({
clientError: this.props.clientError,
2015-09-30 19:35:35 +05:00
serverError: this.props.serverError
2015-07-07 09:16:13 -04:00
});
2015-09-01 17:06:31 -07:00
}
doFileSelect(e) {
2015-07-07 09:16:13 -04:00
e.preventDefault();
2015-09-30 19:35:35 +05:00
var filename = $(e.target).val();
if (filename.substring(3, 11) === 'fakepath') {
filename = filename.substring(12);
}
2015-07-07 09:16:13 -04:00
this.setState({
clientError: '',
2015-09-29 20:53:30 +05:00
serverError: '',
2015-09-30 19:35:35 +05:00
filename
2015-07-07 09:16:13 -04:00
});
2015-09-01 17:06:31 -07:00
}
doSubmit(e) {
2015-07-07 09:16:13 -04:00
e.preventDefault();
2015-10-15 12:07:06 -04:00
var inputnode = ReactDOM.findDOMNode(this.refs.uploadinput);
2015-07-07 09:16:13 -04:00
if (inputnode.files && inputnode.files[0]) {
this.props.submit(inputnode.files[0]);
} else {
this.setState({clientError: true});
2015-07-07 09:16:13 -04:00
}
2015-09-01 17:06:31 -07:00
}
render() {
2015-09-30 19:35:35 +05:00
let clientError = null;
2015-07-07 09:16:13 -04:00
if (this.state.clientError) {
clientError = (
<div className='file-status'>
<FormattedMessage
id='setting_upload.noFile'
defaultMessage='No file selected.'
/>
</div>
2015-07-07 09:16:13 -04:00
);
}
2015-09-30 19:35:35 +05:00
let serverError = null;
2015-07-07 09:16:13 -04:00
if (this.state.serverError) {
serverError = (
<div className='file-status'>{this.state.serverError}</div>
2015-07-07 09:16:13 -04:00
);
}
2015-09-30 19:35:35 +05:00
let fileNameText = null;
let submitButtonClass = 'btn btn-sm btn-primary disabled';
if (this.state.filename) {
fileNameText = (
<div className='file-status file-name'>{this.state.filename}</div>
);
2015-09-30 19:35:35 +05:00
submitButtonClass = 'btn btn-sm btn-primary';
2015-09-29 20:53:30 +05:00
}
2015-09-30 19:35:35 +05:00
2015-07-07 09:16:13 -04:00
return (
<ul className='section-max'>
2015-09-08 21:12:36 +05:00
<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'>
2015-07-07 09:16:13 -04:00
<ul className='setting-list'>
<li className='setting-list-item'>
2015-09-30 19:35:35 +05:00
<span className='btn btn-sm btn-primary btn-file sel-btn'>
<FormattedMessage
id='setting_upload.select'
defaultMessage='Select file'
/>
2015-09-30 19:35:35 +05:00
<input
ref='uploadinput'
accept={this.props.fileTypesAccepted}
type='file'
onChange={this.doFileSelect}
/>
</span>
<a
className={submitButtonClass}
onClick={this.doSubmit}
>
<FormattedMessage
id='setting_upload.import'
defaultMessage='Import'
/>
2015-09-30 19:35:35 +05:00
</a>
{fileNameText}
2015-07-07 09:16:13 -04:00
{serverError}
{clientError}
</li>
</ul>
</li>
</ul>
);
}
2015-09-01 17:06:31 -07:00
}
SettingsUpload.propTypes = {
title: PropTypes.string.isRequired,
submit: PropTypes.func.isRequired,
fileTypesAccepted: PropTypes.string.isRequired,
clientError: PropTypes.string,
serverError: PropTypes.string,
helpText: PropTypes.object
};