Used resoler + service for API keys and TTS login page.

This commit is contained in:
Tom
2025-01-15 20:19:44 +00:00
parent d1eae32e4c
commit 6ee99466f8
7 changed files with 101 additions and 27 deletions

View File

@ -1,4 +1,4 @@
import { Component, OnDestroy, OnInit } from '@angular/core';
import { Component, inject, OnDestroy, OnInit } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { MatInputModule } from '@angular/material/input';
import { MatSelectModule } from '@angular/material/select';
@ -6,11 +6,11 @@ import { MatFormFieldModule } from '@angular/material/form-field';
import { MatButtonModule } from '@angular/material/button';
import EventService from '../../shared/services/EventService';
import { HttpClient } from '@angular/common/http';
import { Router } from '@angular/router';
import { Subscription } from 'rxjs';
import { environment } from '../../../environments/environment';
import { ActivatedRoute, Router } from '@angular/router';
import { first, Subscription, timeout } from 'rxjs';
import { HermesClientService } from '../../hermes-client.service';
import { MatCard, MatCardModule } from '@angular/material/card';
import { MatCardModule } from '@angular/material/card';
import { ApiKeyService } from '../../shared/services/api/api-key.service';
@Component({
selector: 'tts-login',
@ -20,43 +20,39 @@ import { MatCard, MatCardModule } from '@angular/material/card';
styleUrl: './tts-login.component.scss'
})
export class TtsLoginComponent implements OnInit, OnDestroy {
api_keys: { id: string, label: string }[];
keyService = inject(ApiKeyService);
route = inject(ActivatedRoute);
api_keys: { id: string, label: string }[] = [];
selected_api_key: string | undefined;
private subscription: Subscription | undefined;
private subscriptions: Subscription[] = [];
constructor(private hermes: HermesClientService, private events: EventService, private http: HttpClient, private router: Router) {
this.api_keys = [];
}
ngOnInit(): void {
this.http.get(environment.API_HOST + '/keys', {
headers: {
'Authorization': 'Bearer ' + localStorage.getItem('jwt')
}
}).subscribe((data: any) => this.api_keys = data);
this.route.data.subscribe(d => this.api_keys = d['keys']);
this.subscription = this.events.listen('tts_login_ack', async _ => {
this.subscriptions.push(this.events.listen('tts_login_ack', async _ => {
await this.router.navigate(['policies'])
});
this.events.listen('tts_logoff', async _ => {
}));
this.subscriptions.push(this.events.listen('tts_logoff', async _ => {
this.selected_api_key = undefined;
await this.router.navigate(['tts-login'])
});
this.events.listen('impersonation', _ => {
}));
this.subscriptions.push(this.events.listen('impersonation', _ => {
this.selected_api_key = undefined;
this.http.get(environment.API_HOST + '/keys', {
headers: {
'Authorization': 'Bearer ' + localStorage.getItem('jwt')
}
}).subscribe((data: any) => this.api_keys = data);
});
this.keyService.fetch(true)
.pipe(timeout(3000), first())
.subscribe(d => this.api_keys = d);
}));
}
ngOnDestroy(): void {
if (this.subscription)
this.subscription.unsubscribe();
if (!this.hermes.logged_in)
this.subscriptions.forEach(s => s.unsubscribe());
}
login() {