2019-07-09 18:24:09 +02:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
|
|
|
import { Helmet } from 'react-helmet';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { browserHistory } from 'react-router';
|
|
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
import * as CollectionsActions from '../../IDE/actions/collections';
|
|
|
|
|
|
|
|
import { generateCollectionName } from '../../../utils/generateRandomName';
|
|
|
|
|
|
|
|
class CollectionCreate extends React.Component {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
const name = generateCollectionName();
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
generatedCollectionName: name,
|
|
|
|
collection: {
|
|
|
|
name,
|
|
|
|
description: ''
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
getTitle() {
|
|
|
|
return 'p5.js Web Editor | Create collection';
|
|
|
|
}
|
|
|
|
|
|
|
|
handleTextChange = field => (evt) => {
|
|
|
|
this.setState({
|
|
|
|
collection: {
|
|
|
|
...this.state.collection,
|
|
|
|
[field]: evt.target.value,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCreateCollection = (event) => {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
this.props.createCollection(this.state.collection)
|
|
|
|
.then(({ id, owner }) => {
|
2019-09-08 18:54:49 +02:00
|
|
|
const pathname = `/${owner.username}/collections/${id}`;
|
|
|
|
const location = { pathname, state: { skipSavingPath: true } };
|
|
|
|
|
|
|
|
browserHistory.replace(location);
|
2019-07-09 18:24:09 +02:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error('Error creating collection', error);
|
|
|
|
this.setState({
|
|
|
|
creationError: error,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { generatedCollectionName, creationError } = this.state;
|
|
|
|
const { name, description } = this.state.collection;
|
|
|
|
|
|
|
|
const invalid = name === '' || name == null;
|
|
|
|
|
|
|
|
return (
|
2019-10-02 17:01:52 +02:00
|
|
|
<div className="collection-create">
|
2019-07-09 18:24:09 +02:00
|
|
|
<Helmet>
|
|
|
|
<title>{this.getTitle()}</title>
|
|
|
|
</Helmet>
|
2019-10-02 17:01:52 +02:00
|
|
|
<div className="sketches-table-container">
|
|
|
|
<form className="form" onSubmit={this.handleCreateCollection}>
|
|
|
|
{creationError && <span className="form-error">Couldn't create collection</span>}
|
|
|
|
<p className="form__field">
|
|
|
|
<label htmlFor="name" className="form__label">Collection name</label>
|
|
|
|
<input
|
|
|
|
className="form__input"
|
|
|
|
aria-label="name"
|
|
|
|
type="text"
|
|
|
|
id="name"
|
|
|
|
value={name}
|
|
|
|
placeholder={generatedCollectionName}
|
|
|
|
onChange={this.handleTextChange('name')}
|
|
|
|
/>
|
|
|
|
{invalid && <span className="form-error">Collection name is required</span>}
|
|
|
|
</p>
|
|
|
|
<p className="form__field">
|
|
|
|
<label htmlFor="description" className="form__label">Description (optional)</label>
|
|
|
|
<textarea
|
|
|
|
className="form__input form__input-flexible-height"
|
|
|
|
aria-label="description"
|
|
|
|
type="text"
|
|
|
|
id="description"
|
|
|
|
value={description}
|
|
|
|
onChange={this.handleTextChange('description')}
|
|
|
|
placeholder="My fave sketches"
|
|
|
|
rows="4"
|
|
|
|
/>
|
|
|
|
</p>
|
|
|
|
<input type="submit" disabled={invalid} value="Create collection" aria-label="create collection" />
|
|
|
|
</form>
|
2019-09-08 18:54:49 +02:00
|
|
|
</div>
|
2019-10-02 17:01:52 +02:00
|
|
|
</div>
|
2019-07-09 18:24:09 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CollectionCreate.propTypes = {
|
|
|
|
user: PropTypes.shape({
|
|
|
|
username: PropTypes.string,
|
|
|
|
authenticated: PropTypes.bool.isRequired
|
|
|
|
}).isRequired,
|
|
|
|
createCollection: PropTypes.func.isRequired,
|
|
|
|
collection: PropTypes.shape({}).isRequired, // TODO
|
|
|
|
sorting: PropTypes.shape({
|
|
|
|
field: PropTypes.string.isRequired,
|
|
|
|
direction: PropTypes.string.isRequired
|
|
|
|
}).isRequired
|
|
|
|
};
|
|
|
|
|
|
|
|
function mapStateToProps(state, ownProps) {
|
|
|
|
return {
|
|
|
|
user: state.user,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapDispatchToProps(dispatch) {
|
|
|
|
return bindActionCreators(Object.assign({}, CollectionsActions), dispatch);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(CollectionCreate);
|