fix: Doc history does not load when linked to directly (race condition)

This commit is contained in:
Tom Moor
2020-04-05 13:20:47 -07:00
parent 9712b8d205
commit 3bd1c1f047
2 changed files with 19 additions and 25 deletions

View File

@ -30,29 +30,12 @@ class DocumentHistory extends React.Component<Props> {
@observable isFetching: boolean = false; @observable isFetching: boolean = false;
@observable offset: number = 0; @observable offset: number = 0;
@observable allowLoadMore: boolean = true; @observable allowLoadMore: boolean = true;
@observable document: Document;
constructor(props) {
super();
this.document = props.documents.getByUrl(props.match.params.documentSlug);
}
async componentDidMount() { async componentDidMount() {
await this.loadMoreResults(); await this.loadMoreResults();
this.selectFirstRevision(); 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 () => { fetchResults = async () => {
this.isFetching = true; this.isFetching = true;
@ -60,7 +43,7 @@ class DocumentHistory extends React.Component<Props> {
const results = await this.props.revisions.fetchPage({ const results = await this.props.revisions.fetchPage({
limit, limit,
offset: this.offset, offset: this.offset,
id: this.document.id, id: this.props.match.params.documentSlug,
}); });
if ( if (
@ -78,8 +61,13 @@ class DocumentHistory extends React.Component<Props> {
selectFirstRevision = () => { selectFirstRevision = () => {
if (this.revisions.length) { if (this.revisions.length) {
const document = this.props.documents.getByUrl(
this.props.match.params.documentSlug
);
if (!document) return;
this.props.history.replace( 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<Props> {
@action @action
loadMoreResults = async () => { loadMoreResults = async () => {
// Don't paginate if there aren't more results or were in the middle of fetching // Don't paginate if there aren't more results or were in the middle of fetching
if (!this.allowLoadMore || this.isFetching || !this.document) return; if (!this.allowLoadMore || this.isFetching) return;
await this.fetchResults(); await this.fetchResults();
}; };
get revisions() { get revisions() {
if (!this.document) return []; const document = this.props.documents.getByUrl(
return this.props.revisions.getDocumentRevisions(this.document.id); this.props.match.params.documentSlug
);
if (!document) return [];
return this.props.revisions.getDocumentRevisions(document.id);
} }
render() { 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 ( return (
<Sidebar> <Sidebar>
@ -115,7 +109,7 @@ class DocumentHistory extends React.Component<Props> {
<Revision <Revision
key={revision.id} key={revision.id}
revision={revision} revision={revision}
document={this.document} document={document}
showMenu={index !== 0} showMenu={index !== 0}
/> />
))} ))}

View File

@ -439,7 +439,7 @@ router.post('documents.revisions', auth(), pagination(), async ctx => {
authorize(user, 'read', document); authorize(user, 'read', document);
const revisions = await Revision.findAll({ const revisions = await Revision.findAll({
where: { documentId: id }, where: { documentId: document.id },
order: [[sort, direction]], order: [[sort, direction]],
offset: ctx.state.pagination.offset, offset: ctx.state.pagination.offset,
limit: ctx.state.pagination.limit, limit: ctx.state.pagination.limit,