Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

Fix: prevent saving changes to stale cached document object (#14065)

shamoondf8e95cbd4cc
diff --git a/src-ui/src/app/services/open-documents.service.spec.ts b/src-ui/src/app/services/open-documents.service.spec.ts
index 2678097df..a8df98f8c 100644
--- a/src-ui/src/app/services/open-documents.service.spec.ts
+++ b/src-ui/src/app/services/open-documents.service.spec.ts
@@ -221,6 +221,25 @@ describe('OpenDocumentsService', () => {
expect(openDocumentsService.getOpenDocuments()).toHaveLength(1)
})
+ it('should refresh documents in place and keep unsaved edits', () => {
+ const openDoc = { ...documents[0] }
+ subscriptions.push(openDocumentsService.openDocument(openDoc).subscribe())
+ openDoc.title = 'Unsaved title'
+ openDocumentsService.setDirty(openDoc, true, { title: openDoc.title })
+
+ openDocumentsService.refreshDocument(openDoc.id)
+ httpTestingController
+ .expectOne(
+ `${environment.apiBaseUrl}documents/${openDoc.id}/?full_perms=true`
+ )
+ .flush({ ...documents[0], tags: [4] })
+
+ const refreshed = openDocumentsService.getOpenDocument(openDoc.id)
+ expect(refreshed).toBe(openDoc)
+ expect(refreshed.title).toEqual('Unsaved title')
+ expect(refreshed.tags).toEqual([4])
+ })
+
it('should handle error on refresh documents', () => {
subscriptions.push(
openDocumentsService.openDocument(documents[1]).subscribe()
diff --git a/src-ui/src/app/services/open-documents.service.ts b/src-ui/src/app/services/open-documents.service.ts
index 4d6085f1e..288343c07 100644
--- a/src-ui/src/app/services/open-documents.service.ts
+++ b/src-ui/src/app/services/open-documents.service.ts
@@ -50,7 +50,15 @@ export class OpenDocumentsService {
if (index > -1) {
this.documentService.get(id).subscribe({
next: (doc) => {
- this.openDocuments[index] = doc
+ const openDoc = this.openDocuments.find((d) => d.id == id)
+ if (!openDoc) return
+ const unsavedEdits = Object.fromEntries(
+ (openDoc.__changedFields ?? []).map((field) => [
+ field,
+ openDoc[field],
+ ])
+ )
+ Object.assign(openDoc, doc, unsavedEdits)
this.save()
},
error: () => {