Allow the creation of new entries

This commit is contained in:
Cássio Zen
2016-08-24 21:36:44 -03:00
parent fd79381160
commit b717874e7b
9 changed files with 73 additions and 29 deletions

View File

@ -4,6 +4,7 @@ import { connect } from 'react-redux';
import {
loadEntry,
createDraftFromEntry,
createEmptyDraft,
discardDraft,
changeDraft,
persistEntry
@ -15,19 +16,27 @@ import EntryEditor from '../components/EntryEditor';
class EntryPage extends React.Component {
constructor(props) {
super(props);
this.props.loadEntry(props.collection, props.slug);
this.createDraft = this.createDraft.bind(this);
this.handlePersistEntry = this.handlePersistEntry.bind(this);
}
componentDidMount() {
if (this.props.entry) {
this.props.createDraftFromEntry(this.props.entry);
if (!this.props.newEntry) {
this.props.loadEntry(this.props.collection, this.props.slug);
this.createDraft(this.props.entry);
} else {
this.props.createEmptyDraft(this.props.collection);
}
}
componentWillReceiveProps(nextProps) {
if (this.props.entry !== nextProps.entry && !nextProps.entry.get('isFetching')) {
this.props.createDraftFromEntry(nextProps.entry);
if (this.props.entry === nextProps.entry) return;
if (nextProps.entry && !nextProps.entry.get('isFetching')) {
this.createDraft(nextProps.entry);
} else if (nextProps.newEntry) {
this.props.createEmptyDraft(nextProps.collection);
}
}
@ -35,17 +44,19 @@ class EntryPage extends React.Component {
this.props.discardDraft();
}
createDraft(entry) {
if (entry) this.props.createDraftFromEntry(entry);
}
handlePersistEntry() {
this.props.persistEntry(this.props.collection, this.props.entryDraft);
}
render() {
const {
entry, entryDraft, boundGetMedia, collection, changeDraft, addMedia, removeMedia
} = this.props;
if (entry == null || entryDraft.get('entry') == undefined || entry.get('isFetching')) {
if (entryDraft == null || entryDraft.get('entry') == undefined || entry && entry.get('isFetching')) {
return <div>Loading...</div>;
}
return (
@ -68,22 +79,25 @@ EntryPage.propTypes = {
changeDraft: PropTypes.func.isRequired,
collection: ImmutablePropTypes.map.isRequired,
createDraftFromEntry: PropTypes.func.isRequired,
createEmptyDraft: PropTypes.func.isRequired,
discardDraft: PropTypes.func.isRequired,
entry: ImmutablePropTypes.map.isRequired,
entry: ImmutablePropTypes.map,
entryDraft: ImmutablePropTypes.map.isRequired,
loadEntry: PropTypes.func.isRequired,
persistEntry: PropTypes.func.isRequired,
removeMedia: PropTypes.func.isRequired,
slug: PropTypes.string.isRequired,
slug: PropTypes.string,
newEntry: PropTypes.bool.isRequired,
};
function mapStateToProps(state, ownProps) {
const { collections, entryDraft } = state;
const collection = collections.get(ownProps.params.name);
const newEntry = ownProps.route && ownProps.route.newRecord === true;
const slug = ownProps.params.slug;
const entry = selectEntry(state, collection.get('name'), slug);
const entry = newEntry ? null : selectEntry(state, collection.get('name'), slug);
const boundGetMedia = getMedia.bind(null, state);
return { collection, collections, entryDraft, boundGetMedia, slug, entry };
return { collection, collections, newEntry, entryDraft, boundGetMedia, slug, entry };
}
export default connect(
@ -94,6 +108,7 @@ export default connect(
removeMedia,
loadEntry,
createDraftFromEntry,
createEmptyDraft,
discardDraft,
persistEntry
}