Skip to content

Commit

Permalink
Adding "remove/add" buttons in resume step
Browse files Browse the repository at this point in the history
- Adding remove and add button to resume form step
- Removing pre_finish_step from interate callback
- Adding pre_finish_step to crud methods
- Changing pre_finish_step to be async
  • Loading branch information
rukasudev committed Jan 9, 2025
1 parent 4d74a52 commit 0b354f2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
4 changes: 2 additions & 2 deletions app/components/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ async def callback(self, interaction: discord.Interaction):

self.view.clear_items()

view = RemoveItem(self.view.command_key, self.view.cogs, self.locale, self.after_callback)
view = RemoveItem(self.view.command_key, self.view.cogs or self.view._parse_responses_to_cog(), self.locale, self.after_callback)
embed = interaction.message.embeds[0]

await interaction.response.edit_message(embed=embed, view=view)
Expand All @@ -239,7 +239,7 @@ async def callback(self, interaction: discord.Interaction):

self.view.clear_items()

view = Form(self.view.command_key, self.locale, cogs=self.view.cogs)
view = Form(self.view.command_key, self.locale, cogs=self.view.cogs or self.view._parse_responses_to_cog())
view.filter_steps(constants.COMMAND_KEY_TO_COMPOSITION_KEY[self.view.command_key])
view._set_after_callback(self.after_callback)

Expand Down
21 changes: 18 additions & 3 deletions app/views/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

from app import logger
from app.components.buttons import (
AddItemButton,
CancelButton,
ConfirmButton,
EditButton,
PreviewButton,
RemoveItemButton,
)
from app.components.embed import parse_form_dict_to_embed
from app.components.modals import CustomModal
Expand Down Expand Up @@ -68,7 +70,6 @@ async def update_counter(self, args):
try:
self._step = next(self.steps)
except StopIteration:
self.pre_finish_step(args)
return await self._after_callback(args)

self.step_embed = parse_form_dict_to_embed(self._step, self.locale)
Expand Down Expand Up @@ -262,13 +263,27 @@ async def show_resume(self, interaction: discord.Interaction):

self.add_item(EditButton(after_callback=self.update_resume, locale=self.locale))

if self.command_key in commandconstants.COMPOSITION_COMMANDS_LIST:
if len(self.responses[0]['value']) < commandconstants.COMPOSITION_MAX_LENGTH[self.command_key]:
self.add_item(AddItemButton(self.add_item_callback, locale=self.locale))
if len(self.responses[0]['value']) > 1:
self.add_item(RemoveItemButton(self.remove_item_callback, locale=self.locale))

if self._get_step_item("preview"):
self.add_item(PreviewButton(custom_callback=send_welcome_message_preview, locale=self.locale, command_key=self.command_key))

self.add_item(ConfirmButton(callback=self._finish, locale=self.locale))
self.add_item(CancelButton(locale=self.locale))
await interaction.response.edit_message(embed=embed, view=self)

async def add_item_callback(self, interaction: discord.Interaction):
self.responses[0]['value'].extend(self.form_view.responses[0]['value'])
await self.show_resume(interaction)

async def remove_item_callback(self, interaction: discord.Interaction, item_removed: Dict[str, Any], new_cogs: Dict[str, Any]):
self.responses[0]['value'] = new_cogs[commandconstants.COMMAND_KEY_TO_COMPOSITION_KEY[self.command_key]]["values"]
await self.show_resume(interaction)

async def show_buttons(self, interaction: discord.Interaction):
self.clear_items()

Expand Down Expand Up @@ -296,7 +311,7 @@ async def _finish(self, interaction: discord.Interaction):

interaction.locale = parse_valid_locale(interaction.locale)
cog_param = self._parse_responses_to_cog()
self.pre_finish_step(interaction)
await self.pre_finish_step(interaction)

update_moderations_by_guild(
guild_id=interaction.guild_id, key=self.command_key, value=True
Expand Down Expand Up @@ -335,7 +350,7 @@ async def _finish(self, interaction: discord.Interaction):

await interaction.followup.send(embed=embed, view=self)

def pre_finish_step(self, interaction: discord.Interaction):
async def pre_finish_step(self, interaction: discord.Interaction):
if self.command_key == commandconstants.NOTIFICATIONS_TWITCH_KEY:
from app.services.notifications_twitch import subscribe_streamer
self.handle_streamer_change(interaction)
Expand Down
4 changes: 4 additions & 0 deletions app/views/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def __init__(
async def update_command(self, interaction: discord.Interaction):
await interaction.response.defer(thinking=True, ephemeral=True)

await self.edited_form_view.pre_finish_step(interaction)

data = self.edited_form_view._parse_responses_to_cog()

update_cog_by_guild(interaction.guild_id, self.command_key, data)
Expand Down Expand Up @@ -247,6 +249,8 @@ def handle_add_item_button(self) -> None:
async def add_item_callback(self, interaction: discord.Interaction):
await interaction.response.defer(thinking=True, ephemeral=True)

await self.form_view.pre_finish_step(interaction)

response = self.form_view._parse_responses_to_cog()[constants.COMMAND_KEY_TO_COMPOSITION_KEY[self.command_key]].get("values")[0]
self.cogs[constants.COMMAND_KEY_TO_COMPOSITION_KEY[self.command_key]]["values"].append(response)

Expand Down

0 comments on commit 0b354f2

Please sign in to comment.