Files
SimpleCloudNotifier/webapp/src/app/features/senders/sender-list/sender-list.component.ts

92 lines
2.5 KiB
TypeScript

import { Component, inject, signal, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { NzTableModule } from 'ng-zorro-antd/table';
import { NzButtonModule } from 'ng-zorro-antd/button';
import { NzIconModule } from 'ng-zorro-antd/icon';
import { NzEmptyModule } from 'ng-zorro-antd/empty';
import { NzCardModule } from 'ng-zorro-antd/card';
import { NzToolTipModule } from 'ng-zorro-antd/tooltip';
import { NzTabsModule } from 'ng-zorro-antd/tabs';
import { ApiService } from '../../../core/services/api.service';
import { AuthService } from '../../../core/services/auth.service';
import { SenderNameStatistics } from '../../../core/models';
import { RelativeTimePipe } from '../../../shared/pipes/relative-time.pipe';
@Component({
selector: 'app-sender-list',
standalone: true,
imports: [
CommonModule,
NzTableModule,
NzButtonModule,
NzIconModule,
NzEmptyModule,
NzCardModule,
NzToolTipModule,
NzTabsModule,
RelativeTimePipe,
],
templateUrl: './sender-list.component.html',
styleUrl: './sender-list.component.scss'
})
export class SenderListComponent implements OnInit {
private apiService = inject(ApiService);
private authService = inject(AuthService);
mySenders = signal<SenderNameStatistics[]>([]);
allSenders = signal<SenderNameStatistics[]>([]);
loadingMy = signal(false);
loadingAll = signal(false);
activeTab = signal(0);
ngOnInit(): void {
this.loadMySenders();
}
onTabChange(index: number): void {
this.activeTab.set(index);
if (index === 0 && this.mySenders().length === 0) {
this.loadMySenders();
} else if (index === 1 && this.allSenders().length === 0) {
this.loadAllSenders();
}
}
loadMySenders(): void {
const userId = this.authService.getUserId();
if (!userId) return;
this.loadingMy.set(true);
this.apiService.getUserSenderNames(userId).subscribe({
next: (response) => {
this.mySenders.set(response.sender_names);
this.loadingMy.set(false);
},
error: () => {
this.loadingMy.set(false);
}
});
}
loadAllSenders(): void {
this.loadingAll.set(true);
this.apiService.getSenderNames().subscribe({
next: (response) => {
this.allSenders.set(response.sender_names);
this.loadingAll.set(false);
},
error: () => {
this.loadingAll.set(false);
}
});
}
refresh(): void {
if (this.activeTab() === 0) {
this.loadMySenders();
} else {
this.loadAllSenders();
}
}
}