Skip to content

Commit

Permalink
fix pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
Seania committed Mar 3, 2023
1 parent 80880da commit 087f520
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
39 changes: 34 additions & 5 deletions src/lib/community/board/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ export class Board {
return await cursor.all();
}

private static paginate(current: number, max: number, range = 5) {
// Calculate the minimum and maximum page numbers for the given range
const minPage = Math.max(current - Math.floor(range / 2), 1);
const maxPage = Math.min(minPage + range - 1, max);
// Return a tuple with the min and max page numbers
return [minPage, maxPage];
}

async render(pageRequest: ListBoardRequest, type: BoardType, locals: App.Locals, query?: string) {
let articleFilter: string[] | undefined;

Expand All @@ -44,16 +52,21 @@ export class Board {
await pageRequest.getListRecents(locals?.user?.uid ?? null, articleFilter) as any[]
: await pageRequest.getBestListRecents(locals?.user?.uid ?? null, articleFilter) as any[];

const maxPage = type === 'default' ?
await pageRequest.getMaxPage(articleFilter)
: await pageRequest.getBestMaxPage(articleFilter);
let maxPage = type === 'default' ?
await pageRequest.getMaxPage(locals?.user?.uid ?? null, articleFilter)
: await pageRequest.getBestMaxPage(locals?.user?.uid ?? null, articleFilter);

const page = pageRequest.page;

if (page > maxPage) {
throw error(HttpStatus.NOT_FOUND, 'Not found');
}

// todo: global setting for pagination range
const paginated = Board.paginate(page, maxPage, 5);
const minPage = paginated[0];
maxPage = paginated[1];

const {user, sessionId} = locals;

const announcements = await this.getAnnouncements(page, user ? user.uid : sessionId!);
Expand Down Expand Up @@ -84,6 +97,7 @@ export class Board {
user,
name: await this.name,
currentPage: page,
minPage,
maxPage,
announcements,
session: locals,
Expand All @@ -103,7 +117,7 @@ export class Board {
return await cursor.next();
}

async getMaxPage(amount = 30, requireLikes: number | null = null, articleFilter?: string[]): Promise<number> {
async getMaxPage(reader: string | null, amount = 30, requireLikes: number | null = null, articleFilter?: string[]): Promise<number> {
const cursor = await db.query(aql`
let articleFilter = ${articleFilter ?? null}
let skipFilter = articleFilter == null
Expand All @@ -120,8 +134,22 @@ export class Board {
let likeCount = length(for tn in savedTags filter tn == "_like" return tn)
let dislikeCount = length(for tn in savedTags filter tn == "_dislike" return tn)
filter is_number(minLike) ? likeCount - dislikeCount >= minLike : true
let reader = ${reader}
let blockedTags = is_string(reader) ? flatten(
for user in users
filter user._key == reader
return is_array(user.blockedTags) ? user.blockedTags : []
) : []
let blockedUsers = is_string(reader) ? flatten(
for user in users
filter user._key == reader && has(user, "blockedUsers")
return (for blockedUser in user.blockedUsers return blockedUser.key)
) : []
filter blockedTags none in savedTags
filter article.author not in blockedUsers
return article)
return article)
return max([1, ceil(count / ${amount})])`);
return await cursor.next();
}
Expand Down Expand Up @@ -360,6 +388,7 @@ export interface IArticleList {
user?: IUserSession,
name: string,
currentPage: number,
minPage: number,
maxPage: number;
announcements: ArticleItemDto[],
session: App.Locals,
Expand Down
8 changes: 7 additions & 1 deletion src/routes/community/[id=integer]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
declare var name: string;
$: name = data.name;
$: currentPage = data.currentPage;
declare var minPage: typeof data.maxPage;
$: minPage = data.minPage;
declare var maxPage: typeof data.maxPage;
$: maxPage = data.maxPage;
declare var listType: 'list' | 'gallery'
Expand Down Expand Up @@ -416,7 +418,11 @@
{/if}

<div class="pb-8 space-y-2">
<Pagination base="/community/{$page.params.id}" pageKey="page" current="{currentPage}" max="{maxPage}"/>
<Pagination base="/community/{$page.params.id}"
pageKey="page"
current="{currentPage}"
min="{minPage}"
max="{maxPage}"/>
</div>
</div>

Expand Down
8 changes: 4 additions & 4 deletions src/routes/community/[id=integer]/api/list/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ export class ListBoardRequest {
}
}

getMaxPage(articleFilter?: string[]) {
return this.board.getMaxPage(this.amount, null, articleFilter);
getMaxPage(reader: string | null, articleFilter?: string[]) {
return this.board.getMaxPage(reader, this.amount, null, articleFilter);
}

getBestMaxPage(articleFilter?: string[]) {
return this.board.getMaxPage(this.amount, 1, articleFilter);
getBestMaxPage(reader: string | null, articleFilter?: string[]) {
return this.board.getMaxPage(reader, this.amount, 1, articleFilter);
}

getListRecents(reader: string | null, articleFilter?: string[]): Promise<ArticleDto[]> {
Expand Down

0 comments on commit 087f520

Please sign in to comment.