mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-10-30 03:56:23 -05:00 
			
		
		
		
	Rewrite nested subscriptions
This commit is contained in:
		| @@ -21,7 +21,7 @@ import { TextComponent } from '../common/input/text/text.component'; | |||||||
| import { SettingsService, SETTINGS_KEYS } from 'src/app/services/settings.service'; | import { SettingsService, SETTINGS_KEYS } from 'src/app/services/settings.service'; | ||||||
| import { dirtyCheck, DirtyComponent } from '@ngneat/dirty-check-forms'; | import { dirtyCheck, DirtyComponent } from '@ngneat/dirty-check-forms'; | ||||||
| import { Observable, Subject, BehaviorSubject } from 'rxjs'; | import { Observable, Subject, BehaviorSubject } from 'rxjs'; | ||||||
| import { first, takeUntil } from 'rxjs/operators'; | import { first, takeUntil, switchMap, map } from 'rxjs/operators'; | ||||||
| import { PaperlessDocumentSuggestions } from 'src/app/data/paperless-document-suggestions'; | import { PaperlessDocumentSuggestions } from 'src/app/data/paperless-document-suggestions'; | ||||||
| import { FILTER_FULLTEXT_MORELIKE } from 'src/app/data/filter-rule-type'; | import { FILTER_FULLTEXT_MORELIKE } from 'src/app/data/filter-rule-type'; | ||||||
|  |  | ||||||
| @@ -110,16 +110,18 @@ export class DocumentDetailComponent implements OnInit, OnDestroy, DirtyComponen | |||||||
|     this.correspondentService.listAll().pipe(first()).subscribe(result => this.correspondents = result.results) |     this.correspondentService.listAll().pipe(first()).subscribe(result => this.correspondents = result.results) | ||||||
|     this.documentTypeService.listAll().pipe(first()).subscribe(result => this.documentTypes = result.results) |     this.documentTypeService.listAll().pipe(first()).subscribe(result => this.documentTypes = result.results) | ||||||
|  |  | ||||||
|     this.route.paramMap.pipe(takeUntil(this.unsubscribeNotifier)).subscribe(paramMap => { |     this.route.paramMap.pipe(switchMap(paramMap => { | ||||||
|       this.documentId = +paramMap.get('id') |       const documentId = +paramMap.get('id') | ||||||
|       this.previewUrl = this.documentsService.getPreviewUrl(this.documentId) |       return this.documentsService.get(documentId).pipe(map(doc => ({doc, documentId}))) | ||||||
|       this.downloadUrl = this.documentsService.getDownloadUrl(this.documentId) |     })).pipe(switchMap(({doc, documentId}) => { | ||||||
|       this.downloadOriginalUrl = this.documentsService.getDownloadUrl(this.documentId, true) |       this.previewUrl = this.documentsService.getPreviewUrl(documentId) | ||||||
|  |       this.downloadUrl = this.documentsService.getDownloadUrl(documentId) | ||||||
|  |       this.downloadOriginalUrl = this.documentsService.getDownloadUrl(documentId, true) | ||||||
|       this.suggestions = null |       this.suggestions = null | ||||||
|       if (this.openDocumentService.getOpenDocument(this.documentId)) { |       if (this.openDocumentService.getOpenDocument(documentId)) { | ||||||
|         this.updateComponent(this.openDocumentService.getOpenDocument(this.documentId)) |         this.updateComponent(this.openDocumentService.getOpenDocument(documentId)) | ||||||
|       } |       } | ||||||
|       this.documentsService.get(this.documentId).pipe(first()).subscribe(doc => { |  | ||||||
|       // Initialize dirtyCheck |       // Initialize dirtyCheck | ||||||
|       this.store = new BehaviorSubject({ |       this.store = new BehaviorSubject({ | ||||||
|         title: doc.title, |         title: doc.title, | ||||||
| @@ -132,17 +134,19 @@ export class DocumentDetailComponent implements OnInit, OnDestroy, DirtyComponen | |||||||
|       }) |       }) | ||||||
|  |  | ||||||
|       this.isDirty$ = dirtyCheck(this.documentForm, this.store.asObservable()) |       this.isDirty$ = dirtyCheck(this.documentForm, this.store.asObservable()) | ||||||
|         this.isDirty$.pipe(takeUntil(this.unsubscribeNotifier)).subscribe(dirty => { |  | ||||||
|           this.openDocumentService.setDirty(this.documentId, dirty) |  | ||||||
|         }) |  | ||||||
|  |  | ||||||
|         if (!this.openDocumentService.getOpenDocument(this.documentId)) { |       return this.isDirty$.pipe(map(dirty => ({doc, documentId, dirty}))) | ||||||
|  |     })) | ||||||
|  |     .pipe(takeUntil(this.unsubscribeNotifier)) | ||||||
|  |     .subscribe(({doc, documentId, dirty}) => { | ||||||
|  |       this.documentId = documentId | ||||||
|  |       this.openDocumentService.setDirty(documentId, dirty) | ||||||
|  |  | ||||||
|  |       if (!this.openDocumentService.getOpenDocument(documentId)) { | ||||||
|         this.openDocumentService.openDocument(doc) |         this.openDocumentService.openDocument(doc) | ||||||
|         this.updateComponent(doc) |         this.updateComponent(doc) | ||||||
|       } |       } | ||||||
|     }, error => {this.router.navigate(['404'])}) |     }, error => {this.router.navigate(['404'])}) | ||||||
|     }) |  | ||||||
|  |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   ngOnDestroy() : void { |   ngOnDestroy() : void { | ||||||
| @@ -170,24 +174,28 @@ export class DocumentDetailComponent implements OnInit, OnDestroy, DirtyComponen | |||||||
|     var modal = this.modalService.open(DocumentTypeEditDialogComponent, {backdrop: 'static'}) |     var modal = this.modalService.open(DocumentTypeEditDialogComponent, {backdrop: 'static'}) | ||||||
|     modal.componentInstance.dialogMode = 'create' |     modal.componentInstance.dialogMode = 'create' | ||||||
|     if (newName) modal.componentInstance.object = { name: newName } |     if (newName) modal.componentInstance.object = { name: newName } | ||||||
|     modal.componentInstance.success.pipe(first()).subscribe(newDocumentType => { |     modal.componentInstance.success.pipe(switchMap(newDocumentType => { | ||||||
|       this.documentTypeService.listAll().pipe(first()).subscribe(documentTypes => { |       return this.documentTypeService.listAll().pipe(map(documentTypes => ({newDocumentType, documentTypes}))) | ||||||
|  |     })) | ||||||
|  |     .pipe(takeUntil(this.unsubscribeNotifier)) | ||||||
|  |     .subscribe(({newDocumentType, documentTypes}) => { | ||||||
|       this.documentTypes = documentTypes.results |       this.documentTypes = documentTypes.results | ||||||
|       this.documentForm.get('document_type').setValue(newDocumentType.id) |       this.documentForm.get('document_type').setValue(newDocumentType.id) | ||||||
|     }) |     }) | ||||||
|     }) |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   createCorrespondent(newName: string) { |   createCorrespondent(newName: string) { | ||||||
|     var modal = this.modalService.open(CorrespondentEditDialogComponent, {backdrop: 'static'}) |     var modal = this.modalService.open(CorrespondentEditDialogComponent, {backdrop: 'static'}) | ||||||
|     modal.componentInstance.dialogMode = 'create' |     modal.componentInstance.dialogMode = 'create' | ||||||
|     if (newName) modal.componentInstance.object = { name: newName } |     if (newName) modal.componentInstance.object = { name: newName } | ||||||
|     modal.componentInstance.success.pipe(first()).subscribe(newCorrespondent => { |     modal.componentInstance.success.pipe(switchMap(newCorrespondent => { | ||||||
|       this.correspondentService.listAll().pipe(first()).subscribe(correspondents => { |       return this.correspondentService.listAll().pipe(map(correspondents => ({newCorrespondent, correspondents}))) | ||||||
|  |     })) | ||||||
|  |     .pipe(takeUntil(this.unsubscribeNotifier)) | ||||||
|  |     .subscribe(({newCorrespondent, correspondents}) => { | ||||||
|       this.correspondents = correspondents.results |       this.correspondents = correspondents.results | ||||||
|       this.documentForm.get('correspondent').setValue(newCorrespondent.id) |       this.documentForm.get('correspondent').setValue(newCorrespondent.id) | ||||||
|     }) |     }) | ||||||
|     }) |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   discard() { |   discard() { | ||||||
| @@ -214,21 +222,18 @@ export class DocumentDetailComponent implements OnInit, OnDestroy, DirtyComponen | |||||||
|   saveEditNext() { |   saveEditNext() { | ||||||
|     this.networkActive = true |     this.networkActive = true | ||||||
|     this.store.next(this.documentForm.value) |     this.store.next(this.documentForm.value) | ||||||
|     this.documentsService.update(this.document).pipe(first()).subscribe(result => { |     this.documentsService.update(this.document).pipe(switchMap(updateResult => { | ||||||
|       this.error = null |       this.error = null | ||||||
|       this.documentListViewService.getNext(this.documentId).pipe(first()).subscribe(nextDocId => { |       return this.documentListViewService.getNext(this.documentId).pipe(map(nextDocId => ({nextDocId, updateResult}))) | ||||||
|         this.networkActive = false |     })).pipe(switchMap(({nextDocId, updateResult}) => { | ||||||
|         if (nextDocId) { |       if (nextDocId) return this.openDocumentService.closeDocument(this.document, true).pipe(map(closeResult => ({updateResult, nextDocId, closeResult}))) | ||||||
|           this.openDocumentService.closeDocument(this.document, true).pipe(first()).subscribe(closed => { |     })) | ||||||
|             if (closed) { |     .pipe(takeUntil(this.unsubscribeNotifier)) | ||||||
|  |     .subscribe(({updateResult, nextDocId, closeResult}) => { | ||||||
|  |       if (closeResult) { | ||||||
|         this.router.navigate(['documents', nextDocId]) |         this.router.navigate(['documents', nextDocId]) | ||||||
|         this.titleInput.focus() |         this.titleInput.focus() | ||||||
|       } |       } | ||||||
|           }) |  | ||||||
|         } |  | ||||||
|       }, error => { |  | ||||||
|         this.networkActive = false |  | ||||||
|       }) |  | ||||||
|     }, error => { |     }, error => { | ||||||
|       this.networkActive = false |       this.networkActive = false | ||||||
|       this.error = error.error |       this.error = error.error | ||||||
| @@ -253,17 +258,18 @@ export class DocumentDetailComponent implements OnInit, OnDestroy, DirtyComponen | |||||||
|     modal.componentInstance.message = $localize`The files for this document will be deleted permanently. This operation cannot be undone.` |     modal.componentInstance.message = $localize`The files for this document will be deleted permanently. This operation cannot be undone.` | ||||||
|     modal.componentInstance.btnClass = "btn-danger" |     modal.componentInstance.btnClass = "btn-danger" | ||||||
|     modal.componentInstance.btnCaption = $localize`Delete document` |     modal.componentInstance.btnCaption = $localize`Delete document` | ||||||
|     modal.componentInstance.confirmClicked.pipe(first()).subscribe(() => { |     modal.componentInstance.confirmClicked.pipe(switchMap(() => { | ||||||
|       modal.componentInstance.buttonsEnabled = false |       modal.componentInstance.buttonsEnabled = false | ||||||
|       this.documentsService.delete(this.document).pipe(first()).subscribe(() => { |       return this.documentsService.delete(this.document) | ||||||
|  |     })) | ||||||
|  |     .pipe(takeUntil(this.unsubscribeNotifier)) | ||||||
|  |     .subscribe(() => { | ||||||
|       modal.close() |       modal.close() | ||||||
|       this.close() |       this.close() | ||||||
|     }, error => { |     }, error => { | ||||||
|       this.toastService.showError($localize`Error deleting document: ${JSON.stringify(error)}`) |       this.toastService.showError($localize`Error deleting document: ${JSON.stringify(error)}`) | ||||||
|       modal.componentInstance.buttonsEnabled = true |       modal.componentInstance.buttonsEnabled = true | ||||||
|     }) |     }) | ||||||
|     }) |  | ||||||
|  |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   moreLike() { |   moreLike() { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Michael Shamoon
					Michael Shamoon