2018-02-07 18:06:07 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
2019-08-12 15:21:42 +00:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { bindActionCreators, compose } from 'redux';
|
2016-07-13 20:13:28 +00:00
|
|
|
import { reduxForm } from 'redux-form';
|
2017-02-22 19:29:35 +00:00
|
|
|
import NewFileForm from './NewFileForm';
|
2019-08-12 15:21:42 +00:00
|
|
|
import { closeNewFileModal } from '../actions/ide';
|
|
|
|
import { createFile } from '../actions/files';
|
2019-09-26 20:17:22 +00:00
|
|
|
import { CREATE_FILE_REGEX } from '../../../../server/utils/fileUtils';
|
2017-02-22 19:29:35 +00:00
|
|
|
|
2020-04-29 22:34:37 +00:00
|
|
|
import ExitIcon from '../../../images/exit.svg';
|
2016-07-13 20:13:28 +00:00
|
|
|
|
2016-07-15 23:05:18 +00:00
|
|
|
|
2016-07-13 20:13:28 +00: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 00:18:11 +00:00
|
|
|
class NewFileModal extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.focusOnModal = this.focusOnModal.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.focusOnModal();
|
|
|
|
}
|
|
|
|
|
|
|
|
focusOnModal() {
|
2017-02-22 19:29:35 +00:00
|
|
|
this.modal.focus();
|
2016-11-30 00:18:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2019-08-12 15:21:42 +00:00
|
|
|
<section className="modal" ref={(element) => { this.modal = element; }}>
|
2016-11-30 00:18:11 +00:00
|
|
|
<div className="modal-content">
|
|
|
|
<div className="modal__header">
|
2019-08-12 15:21:42 +00:00
|
|
|
<h2 className="modal__title">Create File</h2>
|
2020-05-05 23:03:58 +00: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 00:18:11 +00:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<NewFileForm
|
|
|
|
focusOnModal={this.focusOnModal}
|
|
|
|
{...this.props}
|
|
|
|
/>
|
2016-07-13 20:13:28 +00:00
|
|
|
</div>
|
2016-11-30 00:18:11 +00:00
|
|
|
</section>
|
|
|
|
);
|
|
|
|
}
|
2016-07-13 20:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NewFileModal.propTypes = {
|
2019-08-12 15:21:42 +00:00
|
|
|
createFile: PropTypes.func.isRequired,
|
|
|
|
closeNewFileModal: PropTypes.func.isRequired
|
2016-07-13 20:13:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function validate(formProps) {
|
|
|
|
const errors = {};
|
|
|
|
|
|
|
|
if (!formProps.name) {
|
|
|
|
errors.name = 'Please enter a name';
|
2019-09-26 20:17:22 +00: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 20:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
2020-03-24 22:07:58 +00:00
|
|
|
function mapStateToProps() {
|
|
|
|
return {};
|
2019-08-12 15:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function mapDispatchToProps(dispatch) {
|
|
|
|
return bindActionCreators({ createFile, closeNewFileModal }, dispatch);
|
|
|
|
}
|
2016-07-13 20:13:28 +00:00
|
|
|
|
2019-08-12 15:21:42 +00:00
|
|
|
export default compose(
|
|
|
|
connect(mapStateToProps, mapDispatchToProps),
|
|
|
|
reduxForm({
|
|
|
|
form: 'new-file',
|
|
|
|
fields: ['name'],
|
|
|
|
validate
|
|
|
|
})
|
|
|
|
)(NewFileModal);
|