feat: Add child document preloading
This commit is contained in:
@@ -27,6 +27,20 @@ type Props = {
|
||||
class DocumentLink extends React.Component<Props> {
|
||||
@observable menuOpen = false;
|
||||
|
||||
componentDidMount() {
|
||||
if (this.isActiveDocument() && this.hasChildDocuments()) {
|
||||
this.props.documents.fetchChildDocuments(this.props.node.id);
|
||||
}
|
||||
}
|
||||
|
||||
componentDidUpdate(prevProps: Props) {
|
||||
if (prevProps.activeDocument !== this.props.activeDocument) {
|
||||
if (this.isActiveDocument() && this.hasChildDocuments()) {
|
||||
this.props.documents.fetchChildDocuments(this.props.node.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
handleMouseEnter = (ev: SyntheticEvent<>) => {
|
||||
const { node, prefetchDocument } = this.props;
|
||||
|
||||
@@ -35,6 +49,17 @@ class DocumentLink extends React.Component<Props> {
|
||||
prefetchDocument(node.id);
|
||||
};
|
||||
|
||||
isActiveDocument = () => {
|
||||
return (
|
||||
this.props.activeDocument &&
|
||||
this.props.activeDocument.id === this.props.node.id
|
||||
);
|
||||
};
|
||||
|
||||
hasChildDocuments = () => {
|
||||
return !!this.props.node.children.length;
|
||||
};
|
||||
|
||||
render() {
|
||||
const {
|
||||
node,
|
||||
@@ -46,7 +71,6 @@ class DocumentLink extends React.Component<Props> {
|
||||
depth,
|
||||
} = this.props;
|
||||
|
||||
const isActiveDocument = activeDocument && activeDocument.id === node.id;
|
||||
const showChildren = !!(
|
||||
activeDocument &&
|
||||
collection &&
|
||||
@@ -54,16 +78,15 @@ class DocumentLink extends React.Component<Props> {
|
||||
.pathToDocument(activeDocument)
|
||||
.map(entry => entry.id)
|
||||
.includes(node.id) ||
|
||||
isActiveDocument)
|
||||
this.isActiveDocument())
|
||||
);
|
||||
const hasChildren = !!node.children.length;
|
||||
const document = documents.get(node.id);
|
||||
|
||||
return (
|
||||
<Flex
|
||||
column
|
||||
key={node.id}
|
||||
ref={isActiveDocument ? activeDocumentRef : undefined}
|
||||
ref={this.isActiveDocument() ? activeDocumentRef : undefined}
|
||||
onMouseEnter={this.handleMouseEnter}
|
||||
>
|
||||
<DropToImport documentId={node.id} activeClassName="activeDropZone">
|
||||
@@ -92,7 +115,7 @@ class DocumentLink extends React.Component<Props> {
|
||||
)
|
||||
}
|
||||
>
|
||||
{hasChildren && (
|
||||
{this.hasChildDocuments() && (
|
||||
<DocumentChildren column>
|
||||
{node.children.map(childNode => (
|
||||
<DocumentLink
|
||||
|
||||
@@ -171,6 +171,18 @@ export default class DocumentsStore extends BaseStore<Document> {
|
||||
);
|
||||
}
|
||||
|
||||
@action
|
||||
fetchChildDocuments = async (documentId: string): Promise<?(Document[])> => {
|
||||
const res = await client.post(`/documents.list`, {
|
||||
parentDocumentId: documentId,
|
||||
});
|
||||
invariant(res && res.data, 'Document list not available');
|
||||
const { data } = res;
|
||||
runInAction('DocumentsStore#fetchChildDocuments', () => {
|
||||
data.forEach(this.add);
|
||||
});
|
||||
};
|
||||
|
||||
@action
|
||||
fetchNamedPage = async (
|
||||
request: string = 'list',
|
||||
|
||||
Reference in New Issue
Block a user