2018-02-07 19:06:07 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
2019-08-12 17:21:42 +02:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { bindActionCreators, compose } from 'redux';
|
2016-07-13 22:13:28 +02:00
|
|
|
import { reduxForm } from 'redux-form';
|
2017-02-22 20:29:35 +01:00
|
|
|
import NewFileForm from './NewFileForm';
|
2019-08-12 17:21:42 +02:00
|
|
|
import { closeNewFileModal } from '../actions/ide';
|
|
|
|
import { createFile } from '../actions/files';
|
2019-09-26 22:17:22 +02:00
|
|
|
import { CREATE_FILE_REGEX } from '../../../../server/utils/fileUtils';
|
2017-02-22 20:29:35 +01:00
|
|
|
|
2020-04-30 00:34:37 +02:00
|
|
|
import ExitIcon from '../../../images/exit.svg';
|
2016-07-13 22:13:28 +02:00
|
|
|
|
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() {
|
|
|
|
return (
|
2019-08-12 17:21:42 +02:00
|
|
|
<section className="modal" ref={(element) => { this.modal = element; }}>
|
2016-11-30 01:18:11 +01:00
|
|
|
<div className="modal-content">
|
|
|
|
<div className="modal__header">
|
2019-08-12 17:21:42 +02:00
|
|
|
<h2 className="modal__title">Create File</h2>
|
2020-05-06 01:03:58 +02:00
|
|
|
<button
|
|
|
|
className="modal__exit-button"
|
|
|
|
onClick={this.props.closeNewFileModal}
|
|
|
|
aria-label="Close New File Modal"
|
|
|
|
>
|
|
|
|
<ExitIcon focusable="false" aria-hidden="true" />
|
2016-11-30 01:18:11 +01:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<NewFileForm
|
|
|
|
focusOnModal={this.focusOnModal}
|
|
|
|
{...this.props}
|
|
|
|
/>
|
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 = {
|
2019-08-12 17:21:42 +02:00
|
|
|
createFile: PropTypes.func.isRequired,
|
|
|
|
closeNewFileModal: PropTypes.func.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;
|
|
|
|
}
|
|
|
|
|
2020-03-24 23:07:58 +01:00
|
|
|
function mapStateToProps() {
|
|
|
|
return {};
|
2019-08-12 17:21:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function mapDispatchToProps(dispatch) {
|
|
|
|
return bindActionCreators({ createFile, closeNewFileModal }, dispatch);
|
|
|
|
}
|
2016-07-13 22:13:28 +02:00
|
|
|
|
2019-08-12 17:21:42 +02:00
|
|
|
export default compose(
|
|
|
|
connect(mapStateToProps, mapDispatchToProps),
|
|
|
|
reduxForm({
|
|
|
|
form: 'new-file',
|
|
|
|
fields: ['name'],
|
|
|
|
validate
|
|
|
|
})
|
|
|
|
)(NewFileModal);
|