From 79e218d00acb393f196282ffcca70f9e58c17a78 Mon Sep 17 00:00:00 2001
From: Michael Shamoon <4887959+shamoon@users.noreply.github.com>
Date: Tue, 17 May 2022 09:59:14 -0700
Subject: [PATCH 1/2] Fix title subscription fires even after doc change

---
 .../document-detail.component.ts              | 27 ++++++++++---------
 1 file changed, 15 insertions(+), 12 deletions(-)

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 4d66ea384..3d4289ad8 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
@@ -85,6 +85,7 @@ export class DocumentDetailComponent
   store: BehaviorSubject<any>
   isDirty$: Observable<boolean>
   unsubscribeNotifier: Subject<any> = new Subject()
+  docChangeNotifier: Subject<any> = new Subject()
 
   requiresPassword: boolean = false
   password: string
@@ -117,18 +118,7 @@ export class DocumentDetailComponent
     private toastService: ToastService,
     private settings: SettingsService,
     private queryParamsService: QueryParamsService
-  ) {
-    this.titleSubject
-      .pipe(
-        debounceTime(1000),
-        distinctUntilChanged(),
-        takeUntil(this.unsubscribeNotifier)
-      )
-      .subscribe((titleValue) => {
-        this.title = titleValue
-        this.documentForm.patchValue({ title: titleValue })
-      })
-  }
+  ) {}
 
   titleKeyUp(event) {
     this.titleSubject.next(event.target?.value)
@@ -184,6 +174,7 @@ export class DocumentDetailComponent
       .pipe(
         switchMap((paramMap) => {
           const documentId = +paramMap.get('id')
+          this.docChangeNotifier.next(documentId)
           return this.documentsService.get(documentId)
         })
       )
@@ -208,6 +199,18 @@ export class DocumentDetailComponent
             this.updateComponent(doc)
           }
 
+          this.titleSubject
+            .pipe(
+              debounceTime(1000),
+              distinctUntilChanged(),
+              takeUntil(this.docChangeNotifier),
+              takeUntil(this.unsubscribeNotifier)
+            )
+            .subscribe((titleValue) => {
+              this.title = titleValue
+              this.documentForm.patchValue({ title: titleValue })
+            })
+
           this.ogDate = new Date(normalizeDateStr(doc.created.toString()))
 
           // Initialize dirtyCheck

From a51893c849791648090108bfe11a8d216751c165 Mon Sep 17 00:00:00 2001
From: Michael Shamoon <4887959+shamoon@users.noreply.github.com>
Date: Tue, 17 May 2022 10:36:43 -0700
Subject: [PATCH 2/2] Manually dirty check title on doc change

---
 .../document-detail.component.ts              | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

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 3d4289ad8..f3515c974 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
@@ -172,6 +172,7 @@ export class DocumentDetailComponent
 
     this.route.paramMap
       .pipe(
+        takeUntil(this.unsubscribeNotifier),
         switchMap((paramMap) => {
           const documentId = +paramMap.get('id')
           this.docChangeNotifier.next(documentId)
@@ -206,9 +207,20 @@ export class DocumentDetailComponent
               takeUntil(this.docChangeNotifier),
               takeUntil(this.unsubscribeNotifier)
             )
-            .subscribe((titleValue) => {
-              this.title = titleValue
-              this.documentForm.patchValue({ title: titleValue })
+            .subscribe({
+              next: (titleValue) => {
+                this.title = titleValue
+                this.documentForm.patchValue({ title: titleValue })
+              },
+              complete: () => {
+                // doc changed so we manually check dirty in case title was changed
+                if (
+                  this.store.getValue().title !==
+                  this.documentForm.get('title').value
+                ) {
+                  this.openDocumentService.setDirty(doc.id, true)
+                }
+              },
             })
 
           this.ogDate = new Date(normalizeDateStr(doc.created.toString()))
@@ -238,7 +250,6 @@ export class DocumentDetailComponent
           return this.isDirty$.pipe(map((dirty) => ({ doc, dirty })))
         })
       )
-      .pipe(takeUntil(this.unsubscribeNotifier))
       .subscribe({
         next: ({ doc, dirty }) => {
           this.openDocumentService.setDirty(doc.id, dirty)