mirror of
https://github.com/flarum/core.git
synced 2025-08-06 08:27:42 +02:00
fix: paginated list limit hard to change (#3918)
* fix: paginated list limit hard to change * chore: use the default value * chore: apply to other list states * chore: remove debugging code * fix: typings
This commit is contained in:
@@ -48,7 +48,11 @@ export interface ApiPayloadPlural {
|
||||
next?: string;
|
||||
prev?: string;
|
||||
};
|
||||
meta?: MetaInformation;
|
||||
meta?: MetaInformation & {
|
||||
total?: number;
|
||||
page?: number;
|
||||
perPage?: number;
|
||||
};
|
||||
}
|
||||
|
||||
export type ApiPayload = ApiPayloadSingle | ApiPayloadPlural;
|
||||
|
@@ -25,8 +25,15 @@ export interface PaginatedListRequestParams extends Omit<ApiQueryParamsPlural, '
|
||||
}
|
||||
|
||||
export default abstract class PaginatedListState<T extends Model, P extends PaginatedListParams = PaginatedListParams> {
|
||||
/**
|
||||
* This value should not be relied upon when preloading an API document.
|
||||
* In those cases the pageSize should be taken from the meta information of the preloaded
|
||||
* document. Checkout `DiscussionListState.loadPage` for an example.
|
||||
*/
|
||||
public static DEFAULT_PAGE_SIZE = 20;
|
||||
|
||||
protected location!: PaginationLocation;
|
||||
protected pageSize: number;
|
||||
protected pageSize: number | null;
|
||||
|
||||
protected pages: Page<T>[] = [];
|
||||
protected params: P = {} as P;
|
||||
@@ -35,7 +42,7 @@ export default abstract class PaginatedListState<T extends Model, P extends Pagi
|
||||
protected loadingPrev: boolean = false;
|
||||
protected loadingNext: boolean = false;
|
||||
|
||||
protected constructor(params: P = {} as P, page: number = 1, pageSize: number = 20) {
|
||||
protected constructor(params: P = {} as P, page: number = 1, pageSize: number | null = null) {
|
||||
this.params = params;
|
||||
|
||||
this.location = { page };
|
||||
@@ -108,12 +115,23 @@ export default abstract class PaginatedListState<T extends Model, P extends Pagi
|
||||
...reqParams,
|
||||
page: {
|
||||
...reqParams.page,
|
||||
offset: this.pageSize * (page - 1),
|
||||
offset: (this.pageSize && this.pageSize * (page - 1)) || 0,
|
||||
limit: this.pageSize || undefined,
|
||||
},
|
||||
include,
|
||||
};
|
||||
|
||||
return app.store.find<T[]>(this.type, params);
|
||||
return app.store.find<T[]>(this.type, params).then((results) => {
|
||||
/*
|
||||
* If this state does not rely on a preloaded API document to know the page size,
|
||||
* then there is no initial list, and therefore the page size can be taken from subsequent requests.
|
||||
*/
|
||||
if (!this.pageSize) {
|
||||
this.pageSize = results.payload?.meta?.perPage || PaginatedListState.DEFAULT_PAGE_SIZE;
|
||||
}
|
||||
|
||||
return results;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -15,7 +15,7 @@ export default class DiscussionListState<P extends DiscussionListParams = Discus
|
||||
protected eventEmitter: EventEmitter;
|
||||
|
||||
constructor(params: P, page: number = 1) {
|
||||
super(params, page, 20);
|
||||
super(params, page, null);
|
||||
|
||||
this.eventEmitter = globalEventEmitter.on('discussion.deleted', this.deleteDiscussion.bind(this));
|
||||
}
|
||||
@@ -44,6 +44,7 @@ export default class DiscussionListState<P extends DiscussionListParams = Discus
|
||||
|
||||
if (preloadedDiscussions) {
|
||||
this.initialLoading = false;
|
||||
this.pageSize = preloadedDiscussions.payload.meta?.perPage || DiscussionListState.DEFAULT_PAGE_SIZE;
|
||||
|
||||
return Promise.resolve(preloadedDiscussions);
|
||||
}
|
||||
|
@@ -4,7 +4,7 @@ import Notification from '../../common/models/Notification';
|
||||
|
||||
export default class NotificationListState extends PaginatedListState<Notification> {
|
||||
constructor() {
|
||||
super({}, 1, 20);
|
||||
super({}, 1, null);
|
||||
}
|
||||
|
||||
get type(): string {
|
||||
|
Reference in New Issue
Block a user