diff --git a/src/app/services/projects.service.ts b/src/app/services/projects.service.ts index 809dc03..2ec0122 100644 --- a/src/app/services/projects.service.ts +++ b/src/app/services/projects.service.ts @@ -1,8 +1,8 @@ import { Injectable } from '@angular/core'; import { HttpClient, HttpResponse } from '@angular/common/http'; -import { IndexerService } from './indexer.service'; import { catchError } from 'rxjs/operators'; import { of, Observable } from 'rxjs'; +import { IndexerService } from './indexer.service'; export interface Project { founderKey: string; @@ -36,71 +36,38 @@ export class ProjectsService { constructor( private http: HttpClient, private indexerService: IndexerService + ) {} - private async ensureTotalProjectsFetched(): Promise { - if (!this.totalProjectsFetched) { - await this.getTotalProjectsCount(); - this.totalProjectsFetched = true; - this.offset = Math.max(this.totalProjects - this.limit, 0); + async fetchProjects(): Promise { + if (this.loading || this.noMoreProjects) { + return []; } - } - private async getTotalProjectsCount(): Promise { + this.loading = true; const indexerUrl = this.indexerService.getPrimaryIndexer('testnet'); - if (!indexerUrl) { - throw new Error('No primary indexer found for testnet'); - } + const url = this.totalProjectsFetched + ? `${indexerUrl}api/query/Angor/projects?offset=${this.offset}&limit=${this.limit}` + : `${indexerUrl}api/query/Angor/projects?limit=${this.limit}`; - const url = `${indexerUrl}api/query/Angor/projects?offset=0&limit=1`; + console.log(`Fetching projects from URL: ${url}`); try { const response = await this.http .get(url, { observe: 'response' }) .toPromise(); - if (response && response.headers) { + + if (!this.totalProjectsFetched && response && response.headers) { const paginationTotal = response.headers.get('pagination-total'); this.totalProjects = paginationTotal ? +paginationTotal : 0; console.log(`Total projects: ${this.totalProjects}`); - } - } catch (error) { - console.error('Error fetching total project count:', error); - } - } - - async fetchProjects(): Promise { - await this.ensureTotalProjectsFetched(); - - if (this.loading || this.noMoreProjects) { - return []; - } + this.totalProjectsFetched = true; - this.loading = true; - const indexerUrl = this.indexerService.getPrimaryIndexer('testnet'); - if (!indexerUrl) { - this.loading = false; - throw new Error('No primary indexer found for testnet'); - } - - if (this.projects.length >= this.totalProjects || this.offset < 0) { - this.noMoreProjects = true; - this.loading = false; - return []; - } - - const url = `${indexerUrl}api/query/Angor/projects?offset=${this.offset}&limit=${this.limit}`; - console.log(`Fetching projects from URL: ${url}`); + this.offset = Math.max(this.totalProjects - this.limit, 0); + } - try { - const newProjects = await this.http - .get(url) - .pipe( - catchError((error) => { - console.error('Error fetching projects:', error); - return of([]); - }) - ) - .toPromise(); + const newProjects = response?.body || []; + console.log('New projects received:', newProjects); if (!newProjects || newProjects.length === 0) { this.noMoreProjects = true; @@ -110,13 +77,14 @@ export class ProjectsService { (newProject) => !this.projects.some( (existingProject) => - existingProject.projectIdentifier === - newProject.projectIdentifier + existingProject.projectIdentifier === newProject.projectIdentifier ) ); if (uniqueNewProjects.length > 0) { this.projects = [...this.projects, ...uniqueNewProjects]; + console.log(`${uniqueNewProjects.length} new projects added`); + this.offset = Math.max(this.offset - this.limit, 0); return uniqueNewProjects; } else { @@ -132,13 +100,9 @@ export class ProjectsService { } } + fetchProjectStats(projectIdentifier: string): Observable { const indexerUrl = this.indexerService.getPrimaryIndexer('testnet'); - if (!indexerUrl) { - console.error('No primary indexer found for testnet'); - return of({} as ProjectStats); - } - const url = `${indexerUrl}api/query/Angor/projects/${projectIdentifier}/stats`; console.log(`Fetching project stats from URL: ${url}`); @@ -155,11 +119,6 @@ export class ProjectsService { fetchProjectDetails(projectIdentifier: string): Observable { const indexerUrl = this.indexerService.getPrimaryIndexer('testnet'); - if (!indexerUrl) { - console.error('No primary indexer found for testnet'); - return of({} as Project); - } - const url = `${indexerUrl}api/query/Angor/projects/${projectIdentifier}`; console.log(`Fetching project details from URL: ${url}`); @@ -178,11 +137,10 @@ export class ProjectsService { return this.projects; } - async resetProjects(): Promise { - await this.ensureTotalProjectsFetched(); - + resetProjects(): void { this.projects = []; this.noMoreProjects = false; - this.offset = Math.max(this.totalProjects - this.limit, 0); + this.offset = 0; + this.totalProjectsFetched = false; } }