diff --git a/app/components/DocumentHistory/DocumentHistory.js b/app/components/DocumentHistory/DocumentHistory.js index 257b3f02..b330d226 100644 --- a/app/components/DocumentHistory/DocumentHistory.js +++ b/app/components/DocumentHistory/DocumentHistory.js @@ -30,29 +30,12 @@ class DocumentHistory extends React.Component { @observable isFetching: boolean = false; @observable offset: number = 0; @observable allowLoadMore: boolean = true; - @observable document: Document; - - constructor(props) { - super(); - this.document = props.documents.getByUrl(props.match.params.documentSlug); - } async componentDidMount() { await this.loadMoreResults(); this.selectFirstRevision(); } - async componentWillReceiveProps(nextProps) { - const document = nextProps.documents.getByUrl( - nextProps.match.params.documentSlug - ); - if (!this.document && document) { - this.document = document; - await this.loadMoreResults(); - this.selectFirstRevision(); - } - } - fetchResults = async () => { this.isFetching = true; @@ -60,7 +43,7 @@ class DocumentHistory extends React.Component { const results = await this.props.revisions.fetchPage({ limit, offset: this.offset, - id: this.document.id, + id: this.props.match.params.documentSlug, }); if ( @@ -78,8 +61,13 @@ class DocumentHistory extends React.Component { selectFirstRevision = () => { if (this.revisions.length) { + const document = this.props.documents.getByUrl( + this.props.match.params.documentSlug + ); + if (!document) return; + this.props.history.replace( - documentHistoryUrl(this.document, this.revisions[0].id) + documentHistoryUrl(document, this.revisions[0].id) ); } }; @@ -87,17 +75,23 @@ class DocumentHistory extends React.Component { @action loadMoreResults = async () => { // Don't paginate if there aren't more results or we’re in the middle of fetching - if (!this.allowLoadMore || this.isFetching || !this.document) return; + if (!this.allowLoadMore || this.isFetching) return; await this.fetchResults(); }; get revisions() { - if (!this.document) return []; - return this.props.revisions.getDocumentRevisions(this.document.id); + const document = this.props.documents.getByUrl( + this.props.match.params.documentSlug + ); + if (!document) return []; + return this.props.revisions.getDocumentRevisions(document.id); } render() { - const showLoading = !this.isLoaded && this.isFetching; + const document = this.props.documents.getByUrl( + this.props.match.params.documentSlug + ); + const showLoading = (!this.isLoaded && this.isFetching) || !document; return ( @@ -115,7 +109,7 @@ class DocumentHistory extends React.Component { ))} diff --git a/server/api/documents.js b/server/api/documents.js index d7fcb05e..1bfeec45 100644 --- a/server/api/documents.js +++ b/server/api/documents.js @@ -439,7 +439,7 @@ router.post('documents.revisions', auth(), pagination(), async ctx => { authorize(user, 'read', document); const revisions = await Revision.findAll({ - where: { documentId: id }, + where: { documentId: document.id }, order: [[sort, direction]], offset: ctx.state.pagination.offset, limit: ctx.state.pagination.limit,