tg is reactive

This commit is contained in:
Daniel Ponte 2024-12-27 11:30:02 -05:00
parent c64f9a19af
commit 7198371563
6 changed files with 73 additions and 43 deletions

View file

@ -5,7 +5,7 @@ import { MatToolbarModule } from '@angular/material/toolbar';
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { AuthService } from './login/auth.service'; import { AuthService } from './login/auth.service';
import { NavigationComponent } from './navigation/navigation.component'; import { NavigationComponent } from './navigation/navigation.component';
import { Subject } from 'rxjs'; import { Subject, Subscription } from 'rxjs';
import { Title } from '@angular/platform-browser'; import { Title } from '@angular/platform-browser';
import { UpdateNagComponent } from './version/update-nag/update-nag.component'; import { UpdateNagComponent } from './version/update-nag/update-nag.component';
import { import {
@ -34,6 +34,7 @@ export class AppComponent {
title = 'stillbox'; title = 'stillbox';
opened!: boolean; opened!: boolean;
toggleNavSubject: Subject<void> = new Subject<void>(); toggleNavSubject: Subject<void> = new Subject<void>();
titleSub!: Subscription;
constructor( constructor(
private router: Router, private router: Router,
@ -45,7 +46,7 @@ export class AppComponent {
} }
ngOnInit() { ngOnInit() {
this.router.events this.titleSub = this.router.events
.pipe( .pipe(
filter((event) => event instanceof NavigationEnd), filter((event) => event instanceof NavigationEnd),
map(() => { map(() => {
@ -68,6 +69,7 @@ export class AppComponent {
} }
logout() { logout() {
this.titleSub.unsubscribe();
this.auth.logout(); this.auth.logout();
} }
} }

View file

@ -7,16 +7,16 @@ export const AuthGuard: CanActivateFn = (route, state) => {
const authSvc: AuthService = inject(AuthService); const authSvc: AuthService = inject(AuthService);
if (localStorage.getItem('jwt') == null) { if (localStorage.getItem('jwt') == null) {
let success = false; let success = false;
authSvc.refresh().subscribe( authSvc.refresh().subscribe({
(event) => { next: (event) => {
if (event?.status == 200) { if (event?.status == 200) {
success = true; success = true;
} }
}, },
(err) => { error: (err) => {
router.navigate(['/login']); router.navigate(['/login']);
}, },
); });
return success; return success;
} else { } else {
return true; return true;

View file

@ -1,7 +1,7 @@
import { Component, inject } from '@angular/core'; import { Component, inject } from '@angular/core';
import { FormsModule } from '@angular/forms'; import { FormsModule } from '@angular/forms';
import { AuthService } from '../login/auth.service'; import { AuthService } from '../login/auth.service';
import { catchError, of } from 'rxjs'; import { catchError, of, Subscription } from 'rxjs';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { MatInputModule } from '@angular/material/input'; import { MatInputModule } from '@angular/material/input';
import { MatFormFieldModule } from '@angular/material/form-field'; import { MatFormFieldModule } from '@angular/material/form-field';
@ -18,9 +18,11 @@ export class LoginComponent {
username: string = ''; username: string = '';
password: string = ''; password: string = '';
failed: boolean = false; failed: boolean = false;
private subscriptions = new Subscription();
onSubmit() { onSubmit() {
this.failed = false; this.failed = false;
this.subscriptions.add(
this.apiService this.apiService
.login(this.username, this.password) .login(this.username, this.password)
.pipe( .pipe(
@ -35,6 +37,11 @@ export class LoginComponent {
} else { } else {
this.failed = true; this.failed = true;
} }
}); }),
);
}
ngOnDestroy() {
this.subscriptions.unsubscribe();
} }
} }

View file

@ -1,5 +1,5 @@
import { Injectable, inject } from '@angular/core'; import { Injectable, inject } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs'; import { BehaviorSubject, Observable, Subscription } from 'rxjs';
import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout'; import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout';
import { map, shareReplay } from 'rxjs/operators'; import { map, shareReplay } from 'rxjs/operators';
@ -11,6 +11,7 @@ export class ToolbarContextService {
filterBtn: BehaviorSubject<boolean>; filterBtn: BehaviorSubject<boolean>;
isHandset$: Observable<boolean>; isHandset$: Observable<boolean>;
filterPanel: BehaviorSubject<boolean> = new BehaviorSubject(false); filterPanel: BehaviorSubject<boolean> = new BehaviorSubject(false);
subscriptions = new Subscription();
showFilterButton() { showFilterButton() {
this.filterBtn.next(true); this.filterBtn.next(true);
@ -30,12 +31,16 @@ export class ToolbarContextService {
console.log('hide'); console.log('hide');
} }
ngOnDestroy() {
this.subscriptions.unsubscribe();
}
constructor() { constructor() {
this.filterBtn = new BehaviorSubject(false); this.filterBtn = new BehaviorSubject(false);
this.isHandset$ = this.breakpointObserver.observe(Breakpoints.Handset).pipe( this.isHandset$ = this.breakpointObserver.observe(Breakpoints.Handset).pipe(
map((result) => result.matches), map((result) => result.matches),
shareReplay(), shareReplay(),
); );
this.isHandset$.subscribe(this.filterPanel); this.subscriptions.add(this.isHandset$.subscribe(this.filterPanel));
} }
} }

View file

@ -17,7 +17,7 @@ import {
MatAutocompleteActivatedEvent, MatAutocompleteActivatedEvent,
} from '@angular/material/autocomplete'; } from '@angular/material/autocomplete';
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { catchError, of } from 'rxjs'; import { BehaviorSubject, catchError, of } from 'rxjs';
import { shareReplay } from 'rxjs/operators'; import { shareReplay } from 'rxjs/operators';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { import {

View file

@ -8,7 +8,7 @@ import { TalkgroupTableComponent } from './talkgroup-table/talkgroup-table.compo
import { PageEvent } from '@angular/material/paginator'; import { PageEvent } from '@angular/material/paginator';
import { MatToolbarModule } from '@angular/material/toolbar'; import { MatToolbarModule } from '@angular/material/toolbar';
import { PrefsService } from '../prefs/prefs.service'; import { PrefsService } from '../prefs/prefs.service';
import { Subject } from 'rxjs'; import { BehaviorSubject, Subject, Subscription } from 'rxjs';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner'; import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { MatInputModule } from '@angular/material/input'; import { MatInputModule } from '@angular/material/input';
import { MatFormFieldModule } from '@angular/material/form-field'; import { MatFormFieldModule } from '@angular/material/form-field';
@ -43,7 +43,9 @@ export class TalkgroupsComponent {
perPage = 25; perPage = 25;
filter = new FormControl(''); filter = new FormControl('');
curPage = <PageEvent>{ pageIndex: 0, pageSize: this.perPage }; curPage = <PageEvent>{ pageIndex: 0, pageSize: this.perPage };
private getPage = new BehaviorSubject<PageEvent>(this.curPage);
constructor(private route: ActivatedRoute) {} constructor(private route: ActivatedRoute) {}
subs = new Subscription();
pageReset: Subject<void> = new Subject<void>(); pageReset: Subject<void> = new Subject<void>();
@ -53,30 +55,44 @@ export class TalkgroupsComponent {
switchPage(p: PageEvent) { switchPage(p: PageEvent) {
this.curPage = p; this.curPage = p;
this.route.paramMap
.pipe( if (p.pageSize != this.perPage) {
switchMap((params) => { this.perPage = p.pageSize;
this.perPage = p!.pageSize; this.prefsService.set('tgsPerPage', this.perPage);
this.selectedSys = Number(params.get('sys')); }
this.selectedId = Number(params.get('tg')); this.getPage.next(p);
return this.tgService.getTalkgroupsPag({ }
page: p!.pageIndex + 1,
perPage: p!.pageSize, ngOnDestroy() {
filter: this.filter.value == '' ? null : this.filter.value, this.subs.unsubscribe();
});
}),
)
.subscribe((ev) => {
this.tgs = ev;
});
this.prefsService.set('tgsPerPage', p!.pageSize);
} }
ngOnInit() { ngOnInit() {
this.prefsService.get('tgsPerPage').subscribe((tgpp) => { this.prefsService.get('tgsPerPage').subscribe((tgpp) => {
this.perPage = tgpp; this.perPage = tgpp;
this.switchPage(this.curPage);
}); });
this.subs.add(
this.getPage
.pipe(
switchMap((p) =>
this.route.paramMap.pipe(
switchMap((params) => {
this.selectedSys = Number(params.get('sys'));
this.selectedId = Number(params.get('tg'));
return this.tgService.getTalkgroupsPag({
page: p.pageIndex + 1,
perPage: p.pageSize,
filter: this.filter.value == '' ? null : this.filter.value,
});
}),
),
),
)
.subscribe((ev) => {
this.tgs = ev;
}),
);
this.switchPage(this.curPage);
} }
ngAfterViewInit() { ngAfterViewInit() {