diff --git a/frontend/src/app/features/teams/state/team-contributors.state.spec.ts b/frontend/src/app/features/teams/state/team-contributors.state.spec.ts index c79999a63..904f082a8 100644 --- a/frontend/src/app/features/teams/state/team-contributors.state.spec.ts +++ b/frontend/src/app/features/teams/state/team-contributors.state.spec.ts @@ -89,6 +89,17 @@ describe('TeamContributorsState', () => { expect(contributorsState.contributorsFiltered()).toEqual(oldContributors.items.slice(10, 20)); expect(contributorsState.snapshot.page).toEqual(1); expect(contributorsState.snapshot.pageSize).toEqual(10); + expect(contributorsState.paging()).toEqual({ page: 1, pageSize: 10, total: 20, count: 10 }); + }); + + it('should reset page and calculate paging from filtered contributors if searching', () => { + contributorsState.load().subscribe(); + contributorsState.page({ page: 1, pageSize: 10 }); + contributorsState.search('4'); + + expect(contributorsState.contributorsFiltered()).toEqual(createContributors(4, 14).items); + expect(contributorsState.snapshot.page).toEqual(0); + expect(contributorsState.paging()).toEqual({ page: 0, pageSize: 10, total: 2, count: 2 }); }); it('should show filtered contributors if searching', () => { diff --git a/frontend/src/app/features/teams/state/team-contributors.state.ts b/frontend/src/app/features/teams/state/team-contributors.state.ts index 7773682f8..b01f9ec3f 100644 --- a/frontend/src/app/features/teams/state/team-contributors.state.ts +++ b/frontend/src/app/features/teams/state/team-contributors.state.ts @@ -8,7 +8,7 @@ import { computed, inject, Service } from '@angular/core'; import { EMPTY, Observable, throwError } from 'rxjs'; import { catchError, finalize, tap } from 'rxjs/operators'; -import { AssignContributorDto, ContributorDto, ContributorsDto, debug, DialogService, ErrorDto, getPagingInfo, ListState, shareMapSubscribed, shareSubscribed, State, StringHelper, TeamsState, Types, VersionTag } from '@app/shared'; +import { AssignContributorDto, ContributorDto, ContributorsDto, debug, DialogService, ErrorDto, ListState, PagingInfo, shareMapSubscribed, shareSubscribed, State, StringHelper, TeamsState, Types, VersionTag } from '@app/shared'; import { TeamContributorsService } from '../internal'; interface Snapshot extends ListState { @@ -27,6 +27,7 @@ interface Snapshot extends ListState { @Service({ autoProvided: false }) export class TeamContributorsState extends State { + private readonly contributorsPage = this.project(getContributorsPage); private readonly contributorsService = inject(TeamContributorsService); private readonly dialogs = inject(DialogService); private readonly teamsState = inject(TeamsState); @@ -35,7 +36,7 @@ export class TeamContributorsState extends State { this.project(x => x.contributors); public readonly paging = - this.project(x => getPagingInfo(x, x.contributors.length)); + computed(() => this.contributorsPage().paging); public readonly query = this.project(x => x.query); @@ -56,7 +57,7 @@ export class TeamContributorsState extends State { this.project(x => x.canCreate === true); public readonly contributorsFiltered = - this.project(getFilteredContributors); + computed(() => this.contributorsPage().items); public get teamId() { return this.teamsState.teamId; @@ -113,7 +114,8 @@ export class TeamContributorsState extends State { } public search(query: string) { - this.next({ query }, 'Results Filtered'); + // The current page may not exist anymore for the filtered results. + this.next({ query, page: 0 }, 'Results Filtered'); } public revoke(contributor: ContributorDto): Observable { @@ -162,7 +164,7 @@ function getRegex(query?: string): RegExp | undefined { return query ? StringHelper.searchRegex(query) : undefined; } -function getFilteredContributors(snapshot: Snapshot) { +function getContributorsPage(snapshot: Snapshot): { items: ReadonlyArray; paging: PagingInfo } { const { contributors, query, page, pageSize } = snapshot; let filtered = contributors; @@ -173,5 +175,8 @@ function getFilteredContributors(snapshot: Snapshot) { filtered = filtered.filter(x => regex.test(x.contributorName)); } - return filtered.slice(page * pageSize, (page + 1) * pageSize); + const items = filtered.slice(page * pageSize, (page + 1) * pageSize); + + // The paging must be calculated from the filtered contributors, not from all contributors. + return { items, paging: { page, pageSize, total: filtered.length, count: items.length } }; } diff --git a/frontend/src/app/shared/state/contributors.state.spec.ts b/frontend/src/app/shared/state/contributors.state.spec.ts index e1d416595..f04b3ed9e 100644 --- a/frontend/src/app/shared/state/contributors.state.spec.ts +++ b/frontend/src/app/shared/state/contributors.state.spec.ts @@ -92,6 +92,19 @@ describe('ContributorsState', () => { expect(contributors!).toEqual(oldContributors.items.slice(10, 20)); expect(contributorsState.snapshot.page).toEqual(1); expect(contributorsState.snapshot.pageSize).toEqual(10); + expect(contributorsState.paging()).toEqual({ page: 1, pageSize: 10, total: 20, count: 10 }); + }); + + it('should reset page and calculate paging from filtered contributors if searching', () => { + contributorsState.load().subscribe(); + contributorsState.page({ page: 1, pageSize: 10 }); + contributorsState.search('4'); + + const contributors = contributorsState.contributorsFiltered(); + + expect(contributors!).toEqual(createContributors(4, 14).items); + expect(contributorsState.snapshot.page).toEqual(0); + expect(contributorsState.paging()).toEqual({ page: 0, pageSize: 10, total: 2, count: 2 }); }); it('should show filtered contributors if searching', () => { diff --git a/frontend/src/app/shared/state/contributors.state.ts b/frontend/src/app/shared/state/contributors.state.ts index 542d9d7e8..833af4961 100644 --- a/frontend/src/app/shared/state/contributors.state.ts +++ b/frontend/src/app/shared/state/contributors.state.ts @@ -8,7 +8,7 @@ import { computed, inject, Service } from '@angular/core'; import { EMPTY, Observable, throwError } from 'rxjs'; import { catchError, finalize, tap } from 'rxjs/operators'; -import { debug, DialogService, ErrorDto, getPagingInfo, ListState, shareMapSubscribed, shareSubscribed, State, StringHelper, Types, VersionTag } from '@app/framework'; +import { debug, DialogService, ErrorDto, ListState, PagingInfo, shareMapSubscribed, shareSubscribed, State, StringHelper, Types, VersionTag } from '@app/framework'; import { AssignContributorDto, ContributorDto, ContributorsDto } from '../model'; import { ContributorsService } from '../services/contributors.service'; import { AppsState } from './apps.state'; @@ -30,6 +30,7 @@ interface Snapshot extends ListState { @Service() export class ContributorsState extends State { private readonly appsState = inject(AppsState); + private readonly contributorsPage = this.project(getContributorsPage); private readonly contributorsService = inject(ContributorsService); private readonly dialogs = inject(DialogService); @@ -37,7 +38,7 @@ export class ContributorsState extends State { this.project(x => x.contributors); public readonly paging = - this.project(x => getPagingInfo(x, x.contributors.length)); + computed(() => this.contributorsPage().paging); public readonly query = this.project(x => x.query); @@ -58,7 +59,7 @@ export class ContributorsState extends State { this.project(x => x.canCreate === true); public readonly contributorsFiltered = - this.project(getFilteredContributors); + computed(() => this.contributorsPage().items); public get appId() { return this.appsState.appId; @@ -119,7 +120,8 @@ export class ContributorsState extends State { } public search(query: string) { - this.next({ query }, 'Results Filtered'); + // The current page may not exist anymore for the filtered results. + this.next({ query, page: 0 }, 'Results Filtered'); } public revoke(contributor: ContributorDto): Observable { @@ -166,7 +168,7 @@ function getRegex(query?: string): RegExp | undefined { return query ? StringHelper.searchRegex(query) : undefined; } -function getFilteredContributors(snapshot: Snapshot) { +function getContributorsPage(snapshot: Snapshot): { items: ReadonlyArray; paging: PagingInfo } { const { contributors, query, page, pageSize } = snapshot; let filtered = contributors; @@ -177,5 +179,8 @@ function getFilteredContributors(snapshot: Snapshot) { filtered = filtered.filter(x => regex.test(x.contributorName)); } - return filtered.slice(page * pageSize, (page + 1) * pageSize); + const items = filtered.slice(page * pageSize, (page + 1) * pageSize); + + // The paging must be calculated from the filtered contributors, not from all contributors. + return { items, paging: { page, pageSize, total: filtered.length, count: items.length } }; }