// @flow
import React, { Component } from 'react';
import get from 'lodash/get';
import styled from 'styled-components';
import { observer } from 'mobx-react';
import { withRouter, Prompt } from 'react-router';
import { Flex } from 'reflexbox';
import DocumentStore from './DocumentStore';
import Breadcrumbs from './components/Breadcrumbs';
import Menu from './components/Menu';
import Editor from 'components/Editor';
import Layout, { HeaderAction, SaveAction } from 'components/Layout';
import PublishingInfo from 'components/PublishingInfo';
import AtlasPreviewLoading from 'components/AtlasPreviewLoading';
import CenteredContent from 'components/CenteredContent';
const DISCARD_CHANGES = `
You have unsaved changes.
Are you sure you want to discard them?
`;
const Container = styled.div`
position: relative;
font-weight: 400;
font-size: 1em;
line-height: 1.5em;
padding: 0 3em;
width: 50em;
`;
const Meta = styled.div`
position: absolute;
top: 12px;
`;
type Props = {
match: Object,
history: Object,
keydown: Object,
newChildDocument?: boolean,
};
@observer class Document extends Component {
store: DocumentStore;
props: Props;
constructor(props: Props) {
super(props);
this.store = new DocumentStore({ history: this.props.history });
}
componentDidMount = () => {
if (this.props.newDocument) {
this.store.collectionId = this.props.match.params.id;
this.store.newDocument = true;
} else if (this.props.match.params.edit) {
this.store.documentId = this.props.match.params.id;
this.store.fetchDocument();
} else if (this.props.newChildDocument) {
this.store.documentId = this.props.match.params.id;
this.store.newChildDocument = true;
this.store.fetchDocument();
} else {
this.store.documentId = this.props.match.params.id;
this.store.newDocument = false;
this.store.fetchDocument();
}
};
onEdit = () => {
const url = `${this.store.document.url}/edit`;
this.props.history.push(url);
};
onSave = (options: { redirect?: boolean } = {}) => {
if (this.store.newDocument || this.store.newChildDocument) {
this.store.saveDocument(options);
} else {
this.store.updateDocument(options);
}
};
onImageUploadStart = () => {
this.store.updateUploading(true);
};
onImageUploadStop = () => {
this.store.updateUploading(false);
};
onCancel = () => {
this.props.history.goBack();
};
render() {
const isNew = this.props.newDocument || this.props.newChildDocument;
const isEditing = this.props.match.params.edit;
const title = (
);
const titleText =
this.store.document &&
`${get(this.store, 'document.collection.name')} - ${get(this.store, 'document.title')}`;
const actions = (
{isEditing
?
: Edit}
);
return (
{this.store.isFetching &&
}
{this.store.document &&
{!isEditing &&
}
}
);
}
}
export default withRouter(Document);