mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-09-11 12:18:02 +00:00
Fix: prevent saving changes to stale cached document object (#14065)
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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: () => {
|
||||
|
||||
Reference in New Issue
Block a user