]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/modal/quick-settings-modal.component.ts
Prevent invalid end watch section warnings
[github/Chocobozzz/PeerTube.git] / client / src / app / modal / quick-settings-modal.component.ts
index 7a53179cb47ac0df3eb578ac468d2f0833fb51ef..8ba58a23afa76dd12adcd29e1d7f9c7a5b08cdfd 100644 (file)
@@ -1,8 +1,8 @@
-import { ReplaySubject } from 'rxjs'
+import { ReplaySubject, Subscription } from 'rxjs'
 import { filter } from 'rxjs/operators'
-import { Component, OnInit, ViewChild } from '@angular/core'
+import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
+import { ActivatedRoute, Router } from '@angular/router'
 import { AuthService, AuthStatus, LocalStorageService, User, UserService } from '@app/core'
-import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
 
@@ -10,7 +10,9 @@ import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
   selector: 'my-quick-settings',
   templateUrl: './quick-settings-modal.component.html'
 })
-export class QuickSettingsModalComponent extends FormReactive implements OnInit {
+export class QuickSettingsModalComponent implements OnInit, OnDestroy {
+  private static readonly QUERY_MODAL_NAME = 'quick-settings'
+
   @ViewChild('modal', { static: true }) modal: NgbModal
 
   user: User
@@ -18,26 +20,31 @@ export class QuickSettingsModalComponent extends FormReactive implements OnInit
 
   private openedModal: NgbModalRef
 
+  private routeSub: Subscription
+  private loginSub: Subscription
+  private localStorageSub: Subscription
+
   constructor (
-    protected formValidatorService: FormValidatorService,
     private modalService: NgbModal,
     private userService: UserService,
     private authService: AuthService,
-    private localStorageService: LocalStorageService
+    private localStorageService: LocalStorageService,
+    private route: ActivatedRoute,
+    private router: Router
   ) {
-    super()
   }
 
   ngOnInit () {
     this.user = this.userService.getAnonymousUser()
-    this.localStorageService.watch()
+
+    this.localStorageSub = this.localStorageService.watch()
       .subscribe({
         next: () => this.user = this.userService.getAnonymousUser()
       })
 
     this.userInformationLoaded.next(true)
 
-    this.authService.loginChangedSource
+    this.loginSub = this.authService.loginChangedSource
       .pipe(filter(status => status !== AuthStatus.LoggedIn))
       .subscribe({
         next: () => {
@@ -45,6 +52,20 @@ export class QuickSettingsModalComponent extends FormReactive implements OnInit
           this.userInformationLoaded.next(true)
         }
       })
+
+    this.routeSub = this.route.queryParams.subscribe(params => {
+      if (params['modal'] === QuickSettingsModalComponent.QUERY_MODAL_NAME) {
+        this.openedModal = this.modalService.open(this.modal, { centered: true })
+
+        this.openedModal.hidden.subscribe(() => this.setModalQuery('remove'))
+      }
+    })
+  }
+
+  ngOnDestroy () {
+    if (this.routeSub) this.routeSub.unsubscribe()
+    if (this.loginSub) this.loginSub.unsubscribe()
+    if (this.localStorageSub) this.localStorageSub.unsubscribe()
   }
 
   isUserLoggedIn () {
@@ -52,11 +73,14 @@ export class QuickSettingsModalComponent extends FormReactive implements OnInit
   }
 
   show () {
-    this.openedModal = this.modalService.open(this.modal, { centered: true })
+    this.setModalQuery('add')
   }
 
-  hide () {
-    this.openedModal.close()
-    this.form.reset()
+  private setModalQuery (type: 'add' | 'remove') {
+    const modal = type === 'add'
+      ? QuickSettingsModalComponent.QUERY_MODAL_NAME
+      : null
+
+    this.router.navigate([], { queryParams: { modal }, queryParamsHandling: 'merge' })
   }
 }