transaction-logs updated
transaction-logs updated, removed extra service file and matched architecture with other componentsmazdak/UX-2025
parent
66c51a9e70
commit
8dd969fbf6
@ -1,71 +0,0 @@
|
|||||||
import { Injectable } from '@angular/core';
|
|
||||||
import { BehaviorSubject, map, Observable } from 'rxjs';
|
|
||||||
import { URIKey } from '../../utils/uri-enums';
|
|
||||||
import { HttpErrorResponse, HttpParams } from '@angular/common/http';
|
|
||||||
import { HttpURIService } from '../../app.http.uri.service';
|
|
||||||
import { TransactionLog } from '../../models/user';
|
|
||||||
|
|
||||||
@Injectable({
|
|
||||||
providedIn: 'root'
|
|
||||||
})
|
|
||||||
export class TransactionLogService {
|
|
||||||
|
|
||||||
private logsSubject = new BehaviorSubject<TransactionLog[]>([]);
|
|
||||||
private currentPageSubject = new BehaviorSubject<number>(1);
|
|
||||||
private totalCountSubject = new BehaviorSubject<number>(0);
|
|
||||||
private itemsPerPageSubject = new BehaviorSubject<number>(5);
|
|
||||||
|
|
||||||
logs$ = this.logsSubject.asObservable();
|
|
||||||
currentPage$ = this.currentPageSubject.asObservable();
|
|
||||||
totalCount$ = this.totalCountSubject.asObservable();
|
|
||||||
itemsPerPage$ = this.itemsPerPageSubject.asObservable();
|
|
||||||
|
|
||||||
constructor(private httpUriService: HttpURIService) {}
|
|
||||||
|
|
||||||
loadLogs(): void {
|
|
||||||
const params = new HttpParams();
|
|
||||||
this.httpUriService
|
|
||||||
.requestGET<any>(URIKey.TRANSACTION_LOGS, params)
|
|
||||||
.subscribe({
|
|
||||||
next: (res) => {
|
|
||||||
const logs = Array.isArray(res) ? res : res?.data;
|
|
||||||
this.logsSubject.next(logs ?? []);
|
|
||||||
this.totalCountSubject.next(logs.length);
|
|
||||||
},
|
|
||||||
error: (err) => console.error(err)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
setItemsPerPage(itemsPerPage: number): void {
|
|
||||||
this.itemsPerPageSubject.next(itemsPerPage);
|
|
||||||
this.currentPageSubject.next(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
nextPage(): void {
|
|
||||||
const totalPages = this.getTotalPages();
|
|
||||||
const currentPage = this.currentPageSubject.value;
|
|
||||||
if (currentPage < totalPages) {
|
|
||||||
this.currentPageSubject.next(currentPage + 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
previousPage(): void {
|
|
||||||
const currentPage = this.currentPageSubject.value;
|
|
||||||
if (currentPage > 1) {
|
|
||||||
this.currentPageSubject.next(currentPage - 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
goToPage(page: number): void {
|
|
||||||
const totalPages = this.getTotalPages();
|
|
||||||
if (page > 0 && page <= totalPages) {
|
|
||||||
this.currentPageSubject.next(page);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
getTotalPages(): number {
|
|
||||||
const totalCount = this.totalCountSubject.value;
|
|
||||||
const itemsPerPage = this.itemsPerPageSubject.value;
|
|
||||||
return Math.ceil(totalCount / itemsPerPage);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue