]>
Commit | Line | Data |
---|---|---|
1 | import { ReplaySubject } from 'rxjs' | |
2 | import { filter } from 'rxjs/operators' | |
3 | import { Component, OnInit, ViewChild } from '@angular/core' | |
4 | import { ActivatedRoute, Router } from '@angular/router' | |
5 | import { AuthService, AuthStatus, LocalStorageService, User, UserService } from '@app/core' | |
6 | import { NgbModal } from '@ng-bootstrap/ng-bootstrap' | |
7 | import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref' | |
8 | ||
9 | @Component({ | |
10 | selector: 'my-quick-settings', | |
11 | templateUrl: './quick-settings-modal.component.html' | |
12 | }) | |
13 | export class QuickSettingsModalComponent implements OnInit { | |
14 | private static readonly QUERY_MODAL_NAME = 'quick-settings' | |
15 | ||
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 | private modalService: NgbModal, | |
25 | private userService: UserService, | |
26 | private authService: AuthService, | |
27 | private localStorageService: LocalStorageService, | |
28 | private route: ActivatedRoute, | |
29 | private router: Router | |
30 | ) { | |
31 | } | |
32 | ||
33 | ngOnInit () { | |
34 | this.user = this.userService.getAnonymousUser() | |
35 | this.localStorageService.watch() | |
36 | .subscribe({ | |
37 | next: () => this.user = this.userService.getAnonymousUser() | |
38 | }) | |
39 | ||
40 | this.userInformationLoaded.next(true) | |
41 | ||
42 | this.authService.loginChangedSource | |
43 | .pipe(filter(status => status !== AuthStatus.LoggedIn)) | |
44 | .subscribe({ | |
45 | next: () => { | |
46 | this.user = this.userService.getAnonymousUser() | |
47 | this.userInformationLoaded.next(true) | |
48 | } | |
49 | }) | |
50 | ||
51 | this.route.queryParams.subscribe(params => { | |
52 | if (params['modal'] === QuickSettingsModalComponent.QUERY_MODAL_NAME) { | |
53 | this.openedModal = this.modalService.open(this.modal, { centered: true }) | |
54 | ||
55 | this.openedModal.hidden.subscribe(() => this.setModalQuery('remove')) | |
56 | } | |
57 | }) | |
58 | } | |
59 | ||
60 | isUserLoggedIn () { | |
61 | return this.authService.isLoggedIn() | |
62 | } | |
63 | ||
64 | show () { | |
65 | this.setModalQuery('add') | |
66 | } | |
67 | ||
68 | private setModalQuery (type: 'add' | 'remove') { | |
69 | const modal = type === 'add' | |
70 | ? QuickSettingsModalComponent.QUERY_MODAL_NAME | |
71 | : null | |
72 | ||
73 | this.router.navigate([], { queryParams: { modal }, queryParamsHandling: 'merge' }) | |
74 | } | |
75 | } |