From ee27e298bc119aa8f86d921ede88be1558084e04 Mon Sep 17 00:00:00 2001 From: Vivek Agrawal Date: Thu, 4 Jan 2024 16:20:35 +0530 Subject: [PATCH] Fix node availability when propogating import --- .../shared/data/applyRemoteChanges.js | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/contentcuration/contentcuration/frontend/shared/data/applyRemoteChanges.js b/contentcuration/contentcuration/frontend/shared/data/applyRemoteChanges.js index bed39ce5b1..f8ab13b79a 100644 --- a/contentcuration/contentcuration/frontend/shared/data/applyRemoteChanges.js +++ b/contentcuration/contentcuration/frontend/shared/data/applyRemoteChanges.js @@ -156,14 +156,23 @@ class ReturnedChanges extends ChangeDispatcher { } const { key, target, position, from_key } = change; - // copying takes the ID of the node to copy, so we use `from_key` - return resource.resolveTreeInsert({ id: from_key, target, position, isCreate: true }, data => { - return transaction(change, () => { - // Update the ID on the payload to match the received change, since isCreate=true - // would generate new IDs - data.payload.id = key; - return resource.tableCopy(data); - }); + // 1. Fetch `from_key` node from indexed DB, if not there then + // only fetches from server. + return resource.get(from_key, false).then(sourceNode => { + // 2. Pass the node we get from above to `resolveTreeInsert` as sourceNode. + // because its actually the "source" node. + return resource.resolveTreeInsert( + // copying takes the ID of the node to copy, so we use `from_key`. + { id: from_key, target, position, isCreate: true, sourceNode: sourceNode }, + data => { + return transaction(change, () => { + // Update the ID on the payload to match the received change, since isCreate=true + // would generate new IDs + data.payload.id = key; + return resource.tableCopy(data); + }); + } + ); }); }