Create list effects command #412

Merged
Vylpes merged 15 commits from feature/379-list-effects into develop 2024-12-07 22:32:20 +00:00
2 changed files with 33 additions and 8 deletions
Showing only changes of commit 78fcdeeca5 - Show all commits

View file

@ -58,9 +58,7 @@ export default class UserEffect extends AppBaseEntity {
return single; return single;
} }
public static async FetchAllByUserIdPaginated(userId: string, page: number = 0): Promise<UserEffect[]> { public static async FetchAllByUserIdPaginated(userId: string, page: number = 0, itemsPerPage: number = 10): Promise<[UserEffect[], number]> {
const itemsPerPage = 10;
const repository = AppDataSource.getRepository(UserEffect); const repository = AppDataSource.getRepository(UserEffect);
const query = await repository.createQueryBuilder("effect") const query = await repository.createQueryBuilder("effect")
@ -69,7 +67,7 @@ export default class UserEffect extends AppBaseEntity {
.orderBy("effect.Name", "ASC") .orderBy("effect.Name", "ASC")
.skip(page * itemsPerPage) .skip(page * itemsPerPage)
.take(itemsPerPage) .take(itemsPerPage)
.getMany(); .getManyAndCount();
return query; return query;
} }

View file

@ -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 UserEffect from "../database/entities/app/UserEffect";
import EmbedColours from "../constants/EmbedColours"; import EmbedColours from "../constants/EmbedColours";
@ -49,8 +49,18 @@ export default class EffectHelper {
return true; return true;
} }
public static async GenerateEffectEmbed(userId: string, page: number): Promise<EmbedBuilder> { public static async GenerateEffectEmbed(userId: string, page: number): Promise<{
const effects = await UserEffect.FetchAllByUserIdPaginated(userId, page - 1); embed: EmbedBuilder,
row: ActionRowBuilder<ButtonBuilder>,
}> {
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*"; let description = "*none*";
@ -63,6 +73,23 @@ export default class EffectHelper {
.setDescription(description) .setDescription(description)
.setColor(EmbedColours.Ok); .setColor(EmbedColours.Ok);
return embed; const row = new ActionRowBuilder<ButtonBuilder>()
.addComponents(
new ButtonBuilder()
.setCustomId(`effects list ${page - 1}`)
.setLabel("Previous")
.setStyle(ButtonStyle.Primary)
.setDisabled(page == 0),
new ButtonBuilder()
Vylpes marked this conversation as resolved Outdated

The 'Previous' button should be disabled when 'page' is 1, not 0.

The 'Previous' button should be disabled when 'page' is 1, not 0.
.setCustomId(`effects list ${page + 1}`)
.setLabel("Next")
.setStyle(ButtonStyle.Primary)
.setDisabled(isLastPage),
);
return {
embed,
row,
};
} }
} }