Browse Source

Fix contributor search showing an empty page and wrong paging (#1380)

Co-authored-by: claude[bot] <claude[bot]@users.noreply.github.com>
pull/1382/merge
github-actions[bot] 1 week ago
committed by GitHub
parent
commit
e919c66746
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 11
      frontend/src/app/features/teams/state/team-contributors.state.spec.ts
  2. 17
      frontend/src/app/features/teams/state/team-contributors.state.ts
  3. 13
      frontend/src/app/shared/state/contributors.state.spec.ts
  4. 17
      frontend/src/app/shared/state/contributors.state.ts

11
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', () => {

17
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<string> {
@ -27,6 +27,7 @@ interface Snapshot extends ListState<string> {
@Service({ autoProvided: false })
export class TeamContributorsState extends State<Snapshot> {
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<Snapshot> {
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<Snapshot> {
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<Snapshot> {
}
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<any> {
@ -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<ContributorDto>; 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 } };
}

13
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', () => {

17
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<string> {
@Service()
export class ContributorsState extends State<Snapshot> {
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<Snapshot> {
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<Snapshot> {
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<Snapshot> {
}
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<any> {
@ -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<ContributorDto>; 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 } };
}

Loading…
Cancel
Save