From 78fcdeeca53c21f36cd443a741b3560105b7ac96 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Mon, 25 Nov 2024 18:45:46 +0000 Subject: [PATCH] Create row helper function --- src/database/entities/app/UserEffect.ts | 6 ++--- src/helpers/EffectHelper.ts | 35 ++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/database/entities/app/UserEffect.ts b/src/database/entities/app/UserEffect.ts index f5754bd..72fae5f 100644 --- a/src/database/entities/app/UserEffect.ts +++ b/src/database/entities/app/UserEffect.ts @@ -58,9 +58,7 @@ export default class UserEffect extends AppBaseEntity { return single; } - public static async FetchAllByUserIdPaginated(userId: string, page: number = 0): Promise { - const itemsPerPage = 10; - + public static async FetchAllByUserIdPaginated(userId: string, page: number = 0, itemsPerPage: number = 10): Promise<[UserEffect[], number]> { const repository = AppDataSource.getRepository(UserEffect); const query = await repository.createQueryBuilder("effect") @@ -69,7 +67,7 @@ export default class UserEffect extends AppBaseEntity { .orderBy("effect.Name", "ASC") .skip(page * itemsPerPage) .take(itemsPerPage) - .getMany(); + .getManyAndCount(); return query; } diff --git a/src/helpers/EffectHelper.ts b/src/helpers/EffectHelper.ts index 7f16f84..275e8e6 100644 --- a/src/helpers/EffectHelper.ts +++ b/src/helpers/EffectHelper.ts @@ -1,4 +1,4 @@ -import {EmbedBuilder} from "discord.js"; +import {ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder} from "discord.js"; import UserEffect from "../database/entities/app/UserEffect"; import EmbedColours from "../constants/EmbedColours"; @@ -49,8 +49,18 @@ export default class EffectHelper { return true; } - public static async GenerateEffectEmbed(userId: string, page: number): Promise { - const effects = await UserEffect.FetchAllByUserIdPaginated(userId, page - 1); + public static async GenerateEffectEmbed(userId: string, page: number): Promise<{ + embed: EmbedBuilder, + row: ActionRowBuilder, + }> { + const itemsPerPage = 10; + + const query = await UserEffect.FetchAllByUserIdPaginated(userId, page - 1, itemsPerPage); + + const effects = query[0]; + const count = query[1]; + + const isLastPage = Math.ceil(count / itemsPerPage) - 1 == page; let description = "*none*"; @@ -63,6 +73,23 @@ export default class EffectHelper { .setDescription(description) .setColor(EmbedColours.Ok); - return embed; + const row = new ActionRowBuilder() + .addComponents( + new ButtonBuilder() + .setCustomId(`effects list ${page - 1}`) + .setLabel("Previous") + .setStyle(ButtonStyle.Primary) + .setDisabled(page == 0), + new ButtonBuilder() + .setCustomId(`effects list ${page + 1}`) + .setLabel("Next") + .setStyle(ButtonStyle.Primary) + .setDisabled(isLastPage), + ); + + return { + embed, + row, + }; } }