static-cms/src/containers/EntryPage.js

80 lines
2.1 KiB
JavaScript
Raw Normal View History

import React from 'react';
import { connect } from 'react-redux';
import {
loadEntry,
createDraft,
discardDraft,
changeDraft,
addMediaToDraft,
removeMediaFromDraft,
persist
} from '../actions/entries';
import { selectEntry } from '../reducers/entries';
2016-05-30 16:55:32 -07:00
import EntryEditor from '../components/EntryEditor';
2016-05-30 17:08:30 -07:00
class EntryPage extends React.Component {
constructor(props) {
super(props);
this.props.loadEntry(props.collection, props.slug);
this.handlePersist = this.handlePersist.bind(this);
}
componentDidMount() {
if (this.props.entry) {
this.props.createDraft(this.props.entry);
}
}
componentWillReceiveProps(nextProps) {
if (this.props.entry !== nextProps.entry && !nextProps.entry.get('isFetching')) {
this.props.createDraft(nextProps.entry);
}
}
componentWillUnmount() {
this.props.discardDraft();
}
handlePersist() {
this.props.persist(this.props.collection, this.props.entryDraft);
}
render() {
const { entry, entryDraft, collection, handleDraftChange, handleDraftAddMedia, handleDraftRemoveMedia } = this.props;
if (entry == null || entryDraft.get('entry') == undefined || entry.get('isFetching')) {
return <div>Loading...</div>;
}
return (
<EntryEditor
entry={entryDraft.get('entry')}
collection={collection}
onChange={handleDraftChange}
onAddMedia={handleDraftAddMedia}
onRemoveMedia={handleDraftRemoveMedia}
onPersist={this.handlePersist}
/>
);
}
}
function mapStateToProps(state, ownProps) {
const { collections, entryDraft } = state;
2016-05-30 16:55:32 -07:00
const collection = collections.get(ownProps.params.name);
const slug = ownProps.params.slug;
const entry = selectEntry(state, collection.get('name'), slug);
return {collection, collections, entryDraft, slug, entry};
}
export default connect(
mapStateToProps,
{
handleDraftChange: changeDraft,
handleDraftAddMedia: addMediaToDraft,
handleDraftRemoveMedia: removeMediaFromDraft,
loadEntry,
createDraft,
discardDraft,
persist
}
)(EntryPage);