2016-07-13 22:13:28 +02:00
|
|
|
import React, { PropTypes } from 'react';
|
|
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
import { reduxForm } from 'redux-form';
|
|
|
|
import NewFileForm from './NewFileForm';
|
|
|
|
import * as FileActions from '../actions/files';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import InlineSVG from 'react-inlinesvg';
|
|
|
|
const exitUrl = require('../../../images/exit.svg');
|
|
|
|
|
2016-07-16 01:05:18 +02:00
|
|
|
import FileUploader from './FileUploader';
|
|
|
|
|
2016-07-13 22:13:28 +02:00
|
|
|
// At some point this will probably be generalized to a generic modal
|
|
|
|
// in which you can insert different content
|
|
|
|
// but for now, let's just make this work
|
2016-08-10 21:49:03 +02:00
|
|
|
class NewFileModal extends React.Component {
|
|
|
|
componentDidMount() {
|
|
|
|
document.getElementById('name').focus();
|
|
|
|
}
|
|
|
|
render() {
|
|
|
|
const modalClass = classNames({
|
|
|
|
modal: true,
|
|
|
|
'modal--reduced': !this.props.canUploadMedia
|
|
|
|
});
|
|
|
|
return (
|
|
|
|
<section className={modalClass}>
|
|
|
|
<div className="modal-content">
|
|
|
|
<div className="modal__header">
|
|
|
|
<h2 className="modal__title">Add File</h2>
|
|
|
|
<button className="modal__exit-button" onClick={this.props.closeModal}>
|
|
|
|
<InlineSVG src={exitUrl} alt="Close New File Modal" />
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<NewFileForm {...this.props} />
|
|
|
|
{(() => {
|
|
|
|
if (this.props.canUploadMedia) {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<p className="modal__divider">OR</p>
|
|
|
|
<FileUploader />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
})()}
|
2016-07-13 22:13:28 +02:00
|
|
|
</div>
|
2016-08-10 21:49:03 +02:00
|
|
|
</section>
|
|
|
|
);
|
|
|
|
}
|
2016-07-13 22:13:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
NewFileModal.propTypes = {
|
2016-07-21 04:18:20 +02:00
|
|
|
closeModal: PropTypes.func.isRequired,
|
|
|
|
canUploadMedia: PropTypes.bool.isRequired
|
2016-07-13 22:13:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function mapStateToProps(state) {
|
|
|
|
return {
|
|
|
|
file: state.files
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapDispatchToProps(dispatch) {
|
|
|
|
return bindActionCreators(FileActions, dispatch);
|
|
|
|
}
|
|
|
|
|
|
|
|
function validate(formProps) {
|
|
|
|
const errors = {};
|
|
|
|
|
|
|
|
if (!formProps.name) {
|
|
|
|
errors.name = 'Please enter a name';
|
2016-07-14 00:53:56 +02:00
|
|
|
} else if (!formProps.name.match(/(.+\.js$|.+\.css$)/)) {
|
|
|
|
errors.name = 'File must be of type JavaScript or CSS.';
|
2016-07-13 22:13:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default reduxForm({
|
|
|
|
form: 'new-file',
|
|
|
|
fields: ['name'],
|
|
|
|
validate
|
|
|
|
}, mapStateToProps, mapDispatchToProps)(NewFileModal);
|