diff --git a/src-ui/src/app/components/document-detail/document-detail.component.spec.ts b/src-ui/src/app/components/document-detail/document-detail.component.spec.ts index 9be722c3a..80b160171 100644 --- a/src-ui/src/app/components/document-detail/document-detail.component.spec.ts +++ b/src-ui/src/app/components/document-detail/document-detail.component.spec.ts @@ -1030,6 +1030,22 @@ describe('DocumentDetailComponent', () => { }) }) + it('should restore changed fields and mark as dirty', () => { + jest + .spyOn(activatedRoute, 'paramMap', 'get') + .mockReturnValue(of(convertToParamMap({ id: 3, section: 'details' }))) + jest.spyOn(documentService, 'get').mockReturnValueOnce(of(doc)) + const docWithChanges = Object.assign({}, doc) + docWithChanges.__changedFields = ['title', 'tags', 'owner'] + jest + .spyOn(openDocumentsService, 'getOpenDocument') + .mockReturnValue(docWithChanges) + fixture.detectChanges() // calls ngOnInit + expect(component.documentForm.get('title').dirty).toBeTruthy() + expect(component.documentForm.get('tags').dirty).toBeTruthy() + expect(component.documentForm.get('permissions_form').dirty).toBeTruthy() + }) + it('should show custom field errors', () => { initNormally() component.error = { diff --git a/src-ui/src/app/components/document-detail/document-detail.component.ts b/src-ui/src/app/components/document-detail/document-detail.component.ts index 3f51712ed..55b8ade39 100644 --- a/src-ui/src/app/components/document-detail/document-detail.component.ts +++ b/src-ui/src/app/components/document-detail/document-detail.component.ts @@ -451,6 +451,15 @@ export class DocumentDetailComponent ] delete openDocument['permissions_form'] } + if (openDocument.__changedFields) { + openDocument.__changedFields.forEach((field) => { + if (field === 'owner' || field === 'set_permissions') { + this.documentForm.get('permissions_form').markAsDirty() + } else { + this.documentForm.get(field)?.markAsDirty() + } + }) + } this.updateComponent(openDocument) } else { this.openDocumentService.openDocument(doc) @@ -514,7 +523,7 @@ export class DocumentDetailComponent ) .subscribe({ next: ({ doc, dirty }) => { - this.openDocumentService.setDirty(doc, dirty) + this.openDocumentService.setDirty(doc, dirty, this.getChangedFields()) }, error: (error) => { this.router.navigate(['404'], { diff --git a/src-ui/src/app/data/document.ts b/src-ui/src/app/data/document.ts index 5c23a8600..8aae31945 100644 --- a/src-ui/src/app/data/document.ts +++ b/src-ui/src/app/data/document.ts @@ -158,4 +158,7 @@ export interface Document extends ObjectWithPermissions { remove_inbox_tags?: boolean page_count?: number + + // Frontend only + __changedFields?: string[] } 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 70c416bf8..2678097df 100644 --- a/src-ui/src/app/services/open-documents.service.spec.ts +++ b/src-ui/src/app/services/open-documents.service.spec.ts @@ -241,4 +241,15 @@ describe('OpenDocumentsService', () => { openDocumentsService.openDocument(doc) expect(consoleSpy).toHaveBeenCalled() }) + + it('should set dirty status with changed fields', () => { + subscriptions.push( + openDocumentsService.openDocument(documents[0]).subscribe() + ) + const changedFields = { title: 'foo', content: 'bar' } + openDocumentsService.setDirty(documents[0], true, changedFields) + expect( + openDocumentsService.getOpenDocument(documents[0].id).__changedFields + ).toEqual(['title', 'content']) + }) }) diff --git a/src-ui/src/app/services/open-documents.service.ts b/src-ui/src/app/services/open-documents.service.ts index 92c137fec..ebe90d8e8 100644 --- a/src-ui/src/app/services/open-documents.service.ts +++ b/src-ui/src/app/services/open-documents.service.ts @@ -84,10 +84,20 @@ export class OpenDocumentsService { this.save() } - setDirty(doc: Document, dirty: boolean) { - if (!this.openDocuments.find((d) => d.id == doc.id)) return - if (dirty) this.dirtyDocuments.add(doc.id) - else this.dirtyDocuments.delete(doc.id) + setDirty(doc: Document, dirty: boolean, changedFields: object = {}) { + const existingDoc = this.getOpenDocument(doc.id) + if (!existingDoc) return + if (dirty) { + this.dirtyDocuments.add(doc.id) + existingDoc.__changedFields = Object.keys(changedFields).filter( + (key) => key !== 'id' + ) + } else { + this.dirtyDocuments.delete(doc.id) + existingDoc.__changedFields = [] + } + + this.save() } hasDirty(): boolean {