loading data from api asynchronously and dynamically setting page number. loading onInit

This commit is contained in:
Marcel-Anker
2025-11-19 11:07:47 +01:00
parent 0aca7ace80
commit 8ca5317795

View File

@@ -1,13 +1,11 @@
import {Component, inject} from '@angular/core'; import {Component, inject} from '@angular/core';
import {HttpService} from '../http.service'; import {HttpService} from '../http.service';
import { import {
Eintrittskarte,
Film,
StatisticsFilm, StatisticsFilm,
StatisticsFilmReduced, StatisticsVorstellung,
StatisticsReduced, StatisticsVorstellungReduced,
Vorstellung
} from '@infinimotion/model-frontend'; } from '@infinimotion/model-frontend';
import {LoadingService} from '../loading.service';
import {firstValueFrom, forkJoin} from 'rxjs';
@Component({ @Component({
selector: 'app-statistics', selector: 'app-statistics',
@@ -17,22 +15,37 @@ import {
}) })
export class StatisticsComponent { export class StatisticsComponent {
private http = inject(HttpService); private http = inject(HttpService);
private movies: StatisticsFilmReduced[] = [] protected movies: StatisticsFilm[] = [];
private shows: StatisticsVorstellungReduced[] = [] protected shows: StatisticsVorstellung[] = [];
protected moviesDisplayedColumns: string[] = ['id', 'title', 'earnings', 'tickets'];
protected showsDisplayedColumns: string[] = ['id', 'hall', 'movie_title', 'date', 'earnings', 'tickets'];
protected movieResultsLength: number = 0;
protected showsResultLength: number = 0;
private loading = inject(LoadingService);
ngOnInit(): void { ngOnInit(): void {
this.loadData(); this.loading.show()
this.loadData().then();
} }
loadData() { async loadData() {
this.http.getStatisticsList().subscribe({ let movieRequest = this.http.getMovieStatistics();
next: (response) => { let showRequest = this.http.getShowStatistics();
this.movies = response.movies; let movieResponse = await firstValueFrom(movieRequest);
this.shows = response.shows; let showResponse = await firstValueFrom(showRequest);
console.log(this.movies) this.movies = movieResponse
console.log(this.shows) this.shows = showResponse
}, if (this.movies.length / 30 < 1) {
error: (err) => console.error('Fehler beim Laden der Statistiken', err), this.movieResultsLength = 1;
}); } else {
this.movieResultsLength = Math.ceil(this.movies.length / 30);
}
if (this.shows.length / 30 < 1) {
this.showsResultLength = 1;
} else {
this.showsResultLength = Math.ceil(this.shows.length / 30);
}
this.loading.hide();
} }
} }