2018-02-07 19:06:07 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
2016-07-13 22:13:28 +02:00
|
|
|
import { reduxForm } from 'redux-form';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import InlineSVG from 'react-inlinesvg';
|
2017-02-22 20:29:35 +01:00
|
|
|
import NewFileForm from './NewFileForm';
|
|
|
|
import FileUploader from './FileUploader';
|
2019-09-26 22:17:22 +02:00
|
|
|
import { CREATE_FILE_REGEX } from '../../../../server/utils/fileUtils';
|
2017-02-22 20:29:35 +01:00
|
|
|
|
2016-07-13 22:13:28 +02:00
|
|
|
const exitUrl = require('../../../images/exit.svg');
|
|
|
|
|
2016-07-16 01:05:18 +02:00
|
|
|
|
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-11-30 01:18:11 +01:00
|
|
|
class NewFileModal extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.focusOnModal = this.focusOnModal.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.focusOnModal();
|
|
|
|
}
|
|
|
|
|
|
|
|
focusOnModal() {
|
2017-02-22 20:29:35 +01:00
|
|
|
this.modal.focus();
|
2016-11-30 01:18:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const modalClass = classNames({
|
2017-02-22 20:29:35 +01:00
|
|
|
'modal': true,
|
2016-11-30 01:18:11 +01:00
|
|
|
'modal--reduced': !this.props.canUploadMedia
|
|
|
|
});
|
|
|
|
return (
|
2018-05-09 04:10:52 +02:00
|
|
|
<section className={modalClass} ref={(element) => { this.modal = element; }}>
|
2016-11-30 01:18:11 +01:00
|
|
|
<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
|
|
|
|
focusOnModal={this.focusOnModal}
|
|
|
|
{...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-11-30 01:18:11 +01: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 validate(formProps) {
|
|
|
|
const errors = {};
|
|
|
|
|
|
|
|
if (!formProps.name) {
|
|
|
|
errors.name = 'Please enter a name';
|
2019-09-26 22:17:22 +02:00
|
|
|
} else if (!formProps.name.match(CREATE_FILE_REGEX)) {
|
|
|
|
errors.name = 'Invalid file type. Valid extensions are .js, .css, .json, .txt, .csv, .tsv, .frag, and .vert.';
|
2016-07-13 22:13:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default reduxForm({
|
|
|
|
form: 'new-file',
|
|
|
|
fields: ['name'],
|
|
|
|
validate
|
2016-08-30 20:39:37 +02:00
|
|
|
})(NewFileModal);
|