aboutsummaryrefslogblamecommitdiffhomepage
path: root/client/src/app/modal/quick-settings-modal.component.ts
blob: 7a53179cb47ac0df3eb578ac468d2f0833fb51ef (plain) (tree)
1
2
3
4
5
6
7
8
9
10
                                    
                                       




                                                                                           


                                
                                                      




















                                                                                 
                                    


                                                                   
 



                                                             

                     


                                                         
        














                                                                             
import { ReplaySubject } from 'rxjs'
import { filter } from 'rxjs/operators'
import { Component, OnInit, ViewChild } from '@angular/core'
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'

@Component({
  selector: 'my-quick-settings',
  templateUrl: './quick-settings-modal.component.html'
})
export class QuickSettingsModalComponent extends FormReactive implements OnInit {
  @ViewChild('modal', { static: true }) modal: NgbModal

  user: User
  userInformationLoaded = new ReplaySubject<boolean>(1)

  private openedModal: NgbModalRef

  constructor (
    protected formValidatorService: FormValidatorService,
    private modalService: NgbModal,
    private userService: UserService,
    private authService: AuthService,
    private localStorageService: LocalStorageService
  ) {
    super()
  }

  ngOnInit () {
    this.user = this.userService.getAnonymousUser()
    this.localStorageService.watch()
      .subscribe({
        next: () => this.user = this.userService.getAnonymousUser()
      })

    this.userInformationLoaded.next(true)

    this.authService.loginChangedSource
      .pipe(filter(status => status !== AuthStatus.LoggedIn))
      .subscribe({
        next: () => {
          this.user = this.userService.getAnonymousUser()
          this.userInformationLoaded.next(true)
        }
      })
  }

  isUserLoggedIn () {
    return this.authService.isLoggedIn()
  }

  show () {
    this.openedModal = this.modalService.open(this.modal, { centered: true })
  }

  hide () {
    this.openedModal.close()
    this.form.reset()
  }
}