2016-02-25 20:40:35 -08:00
|
|
|
import React from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
2016-06-08 04:42:24 -03:00
|
|
|
import {
|
|
|
|
loadEntry,
|
|
|
|
createDraft,
|
|
|
|
discardDraft,
|
|
|
|
changeDraft,
|
|
|
|
persist
|
|
|
|
} from '../actions/entries';
|
2016-06-10 00:16:01 -03:00
|
|
|
import { addMedia } from '../actions/media';
|
|
|
|
import { selectEntry, getMedia } from '../reducers';
|
2016-05-30 16:55:32 -07:00
|
|
|
import EntryEditor from '../components/EntryEditor';
|
2016-02-25 20:40:35 -08:00
|
|
|
|
2016-05-30 17:08:30 -07:00
|
|
|
class EntryPage extends React.Component {
|
2016-06-05 01:52:18 -07:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2016-06-08 04:42:24 -03:00
|
|
|
this.props.loadEntry(props.collection, props.slug);
|
2016-06-06 21:53:22 -03:00
|
|
|
this.handlePersist = this.handlePersist.bind(this);
|
|
|
|
}
|
|
|
|
|
2016-06-08 04:42:24 -03:00
|
|
|
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);
|
2016-06-05 01:52:18 -07:00
|
|
|
}
|
|
|
|
|
2016-02-25 20:40:35 -08:00
|
|
|
render() {
|
2016-06-10 00:16:01 -03:00
|
|
|
const {
|
|
|
|
entry, entryDraft, boundGetMedia, collection, handleDraftChange, handleAddMedia, handleDraftRemoveMedia
|
|
|
|
} = this.props;
|
|
|
|
|
2016-06-08 04:42:24 -03:00
|
|
|
if (entry == null || entryDraft.get('entry') == undefined || entry.get('isFetching')) {
|
2016-06-05 01:52:18 -07:00
|
|
|
return <div>Loading...</div>;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<EntryEditor
|
2016-06-08 04:42:24 -03:00
|
|
|
entry={entryDraft.get('entry')}
|
2016-06-10 00:16:01 -03:00
|
|
|
getMedia={boundGetMedia}
|
2016-06-05 01:52:18 -07:00
|
|
|
collection={collection}
|
2016-06-08 04:42:24 -03:00
|
|
|
onChange={handleDraftChange}
|
2016-06-10 00:16:01 -03:00
|
|
|
onAddMedia={handleAddMedia}
|
2016-06-06 21:53:22 -03:00
|
|
|
onPersist={this.handlePersist}
|
2016-06-05 01:52:18 -07:00
|
|
|
/>
|
|
|
|
);
|
2016-02-25 20:40:35 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapStateToProps(state, ownProps) {
|
2016-06-08 04:42:24 -03:00
|
|
|
const { collections, entryDraft } = state;
|
2016-05-30 16:55:32 -07:00
|
|
|
const collection = collections.get(ownProps.params.name);
|
2016-06-05 01:52:18 -07:00
|
|
|
const slug = ownProps.params.slug;
|
|
|
|
const entry = selectEntry(state, collection.get('name'), slug);
|
2016-06-10 00:16:01 -03:00
|
|
|
const boundGetMedia = getMedia.bind(null, state);
|
|
|
|
return {collection, collections, entryDraft, boundGetMedia, slug, entry};
|
2016-02-25 20:40:35 -08:00
|
|
|
}
|
|
|
|
|
2016-06-08 04:42:24 -03:00
|
|
|
export default connect(
|
|
|
|
mapStateToProps,
|
|
|
|
{
|
|
|
|
handleDraftChange: changeDraft,
|
2016-06-10 00:16:01 -03:00
|
|
|
handleAddMedia: addMedia,
|
2016-06-08 04:42:24 -03:00
|
|
|
loadEntry,
|
|
|
|
createDraft,
|
|
|
|
discardDraft,
|
|
|
|
persist
|
|
|
|
}
|
|
|
|
)(EntryPage);
|