Add visitor settings, rework logged-in dropdown (#2514)
[oweals/peertube.git] / client / src / app / modal / quick-settings-modal.component.ts
1 import { Component, ViewChild, OnInit } from '@angular/core'
2 import { AuthService, AuthStatus } from '@app/core'
3 import { FormReactive, FormValidatorService, UserService, User } from '@app/shared'
4 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
5 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
6 import { ReplaySubject } from 'rxjs'
7 import { LocalStorageService } from '@app/shared/misc/storage.service'
8 import { filter } from 'rxjs/operators'
9
10 @Component({
11   selector: 'my-quick-settings',
12   templateUrl: './quick-settings-modal.component.html',
13   styleUrls: [ './quick-settings-modal.component.scss' ]
14 })
15 export class QuickSettingsModalComponent extends FormReactive implements OnInit {
16   @ViewChild('modal', { static: true }) modal: NgbModal
17
18   user: User
19   userInformationLoaded = new ReplaySubject<boolean>(1)
20
21   private openedModal: NgbModalRef
22
23   constructor (
24     protected formValidatorService: FormValidatorService,
25     private modalService: NgbModal,
26     private userService: UserService,
27     private authService: AuthService,
28     private localStorageService: LocalStorageService
29   ) {
30     super()
31   }
32
33   ngOnInit () {
34     this.user = this.userService.getAnonymousUser()
35     this.localStorageService.watch().subscribe(
36       () => this.user = this.userService.getAnonymousUser()
37     )
38     this.userInformationLoaded.next(true)
39
40     this.authService.loginChangedSource
41       .pipe(filter(status => status !== AuthStatus.LoggedIn))
42       .subscribe(
43         () => {
44           this.user = this.userService.getAnonymousUser()
45           this.userInformationLoaded.next(true)
46         }
47       )
48   }
49
50   isUserLoggedIn () {
51     return this.authService.isLoggedIn()
52   }
53
54   show () {
55     this.openedModal = this.modalService.open(this.modal, { centered: true })
56   }
57
58   hide () {
59     this.openedModal.close()
60     this.form.reset()
61   }
62 }