Skip to content

Commit

Permalink
Fix pagination when running releases command with period argument
Browse files Browse the repository at this point in the history
  • Loading branch information
danieltrolezi committed Sep 25, 2024
1 parent 6adf30e commit 9d2d363
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 23 deletions.
16 changes: 10 additions & 6 deletions app/Services/Discord/Commands/ReleasesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ public function exec(array $payload): array
public function callback(array $payload): array
{
$customId = $this->parseCustomId($payload);
$currentPage = $this->getCurrentPage($payload);
$args = $customId['args'];
$page = $args['page'] ?? 1;

return match ($customId['component']) {
self::COMPONENT_PREV_PAGE => $this->getGameReleases($payload, $currentPage - 1),
self::COMPONENT_NEXT_PAGE => $this->getGameReleases($payload, $currentPage + 1),
};
if (!empty($args['period'])) {
Arr::set($payload, 'data.options.0.value', $args['period']);
}

return $this->getGameReleases($payload, $page);
}

/**
Expand Down Expand Up @@ -115,7 +117,9 @@ private function makeResponse(PaginatedResponse $response, string $period): arra
'embeds' => $this->makeGameEmbeds($response),
'components' => [
$this->makeActionRow(
$this->makePaginationComponents($response)
$this->makePaginationComponents($response, [
'period' => $period
])
)
]
];
Expand Down
26 changes: 17 additions & 9 deletions app/Services/Discord/Utils/DiscordCallbackUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ trait DiscordCallbackUtils
* @param string $name
* @return string
*/
private function formatCustomId(string $commandName, string $componentName): string
{
if (strpos($commandName, '_') !== false) {
throw new \Exception('Command name can not contain underscores.');
private function formatCustomId(
string $commandName,
string $componentName,
array $args = []
): string {
if (strpos($commandName, '|') !== false) {
throw new \Exception('Command name can not contain pipelines.');
}

if (strpos($componentName, '_') !== false) {
throw new \Exception('Component name can not contain underscores.');
if (strpos($componentName, '|') !== false) {
throw new \Exception('Component name can not contain pipelines.');
}

return sprintf('%s_%s_%s', $commandName, $componentName, uniqid());
$args = http_build_query($args);

return sprintf('%s|%s|%s|%s', $commandName, $componentName, $args, uniqid());
}

/**
Expand All @@ -32,14 +37,17 @@ private function parseCustomId(
string $path = 'data.custom_id'
): array {
$customId = explode(
'_',
'|',
Arr::get($payload, $path)
);

parse_str($customId[2], $args);

return [
'command' => $customId[0],
'component' => $customId[1],
'uid' => $customId[2],
'args' => $args,
'uid' => $customId[3],
];
}
}
25 changes: 17 additions & 8 deletions app/Services/Discord/Utils/DiscordComponentUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@ trait DiscordComponentUtils
use DiscordCallbackUtils;

private const string COMPONENT_GAMES_FOUND = 'games-fround';
private const string COMPONENT_CURRENT_PAGE = 'current-page-';
private const string COMPONENT_CURRENT_PAGE = 'current-page';
private const string COMPONENT_NEXT_PAGE = 'next-page';
private const string COMPONENT_PREV_PAGE = 'previous-page';

private function makeCustomId(string $componentName): string
private function makeCustomId(string $componentName, array $args = []): string
{
if (!$this instanceof CallbackCommandInterface) {
throw new \Exception('Command does not implements CallbackCommandInterface.');
}

return $this->formatCustomId($this->getCommandName(), $componentName);
return $this->formatCustomId(
$this->getCommandName(),
$componentName,
$args
);
}

/**
Expand Down Expand Up @@ -99,11 +103,12 @@ private function makeButtonComponent(
string $name,
ButtonStyle $style = ButtonStyle::Primary,
bool $disabled = false,
string $emoji = ''
string $emoji = '',
array $args = []
): array {
$button = [
'type' => ComponentType::Button,
'custom_id' => $this->makeCustomId($name),
'custom_id' => $this->makeCustomId($name, $args),
'label' => strtoupper($label),
'style' => $style,
'disabled' => $disabled
Expand All @@ -122,8 +127,10 @@ private function makeButtonComponent(
* @param PaginatedResponse $response
* @return array
*/
private function makePaginationComponents(PaginatedResponse $response): array
{
private function makePaginationComponents(
PaginatedResponse $response,
array $args = []
): array {
return [
$this->makeButtonComponent(
name: self::COMPONENT_GAMES_FOUND,
Expand All @@ -133,7 +140,7 @@ private function makePaginationComponents(PaginatedResponse $response): array
emoji: '🔥'
),
$this->makeButtonComponent(
name: self::COMPONENT_CURRENT_PAGE . $response->currentPage,
name: self::COMPONENT_CURRENT_PAGE,
label: sprintf('Page %s of %s', $response->currentPage, $response->lastPage),
style: ButtonStyle::Secundary,
disabled: true,
Expand All @@ -143,11 +150,13 @@ private function makePaginationComponents(PaginatedResponse $response): array
name: self::COMPONENT_PREV_PAGE,
label: 'Previous Page',
disabled: $response->currentPage === 1,
args: array_merge($args, ['page' => $response->currentPage - 1])
),
$this->makeButtonComponent(
name: self::COMPONENT_NEXT_PAGE,
label: 'Next Page',
disabled: $response->currentPage === $response->lastPage,
args: array_merge($args, ['page' => $response->currentPage + 1])
)
];
}
Expand Down

0 comments on commit 9d2d363

Please sign in to comment.