Giter Site home page Giter Site logo

Comments (4)

Tishka17 avatar Tishka17 commented on June 12, 2024

As I understood:

  1. You need to store items somewhere. It coud be database, FsmContext or manager.context (as you already did)
  2. You need to create window getter function. It will read your context or wherever your store your items. Here is an example: https://aiogram-dialog.readthedocs.io/en/latest/widgets.html#select
  3. To use items from getter should should just set items="Item" in your select.
    4 Your add_item function does not add anything. It replaces saved item in dialog context. May be it is the problem
  4. If you need to do some separate work with separate data, you should start new dialog. For example, in subdialog.py second dialog only inputs the name and does not know how it will be used. In your case should either create 2 dialogs or use manager.switch_to() instead of starting.

Also, welcome to https://t.me/aiogram_dialog

from aiogram_dialog.

yevhen-kalyna avatar yevhen-kalyna commented on June 12, 2024

Thanks for your response @Tishka17 , your recommendation helped, but not fully resolve the issue.

Look like I'm made storing of items in context:

async def get_item_list(dialog_manager: DialogManager, **kwargs):
    items = dialog_manager.current_context().dialog_data.get('items', [])
    if dialog_manager.current_context().dialog_data.get('item', None):
        item = dialog_manager.current_context().dialog_data['item']
        if len(items) < 5:
            items.append(item)
        else:
            pass
            # Notify user that he already have 5 items, and he need to remove something to add new one
    return {
        "items": items,
    }
...
dial_items_manager = Dialog(
    Window(
        'Your item list',
        Select(
            Format("{item}"),
            items="items",
            item_id_getter=operator.itemgetter(1),
            id="items",
            on_click=show_item,
        ),
        Row(
            Button(
                Const('Add item to list'),
                id="add_item",
                on_click=add_item_request,
            )
        ),
        Cancel(),
        state=ManageItemsSG.show_items,
        getter=get_item_list,
    ),
)

But look like something wrong with adding new items to the list of items, because when I adding some item, var 'item' and 'items' in get_item_list() are empty.
As you can see in My code: spoiler upper, I'm using add_item_request() to change Window to

    Window(
        Const("Write item which need to add"),
        MessageInput(add_item),
        state=ManageItemsSG.new_item,
        getter=get_item_list,
    ),

And when I calling add_item(), it registry my m.text, but with dialog_manager.current_context().dialog_data["item"] item not storing as I see. What the better workaround to handle items adding to the global list of items?
It how this look like, item list are empty
image

from aiogram_dialog.

Tishka17 avatar Tishka17 commented on June 12, 2024

I do not know, why your getter is modifying context data. It should only read it.

If you want add something to context, do it on your input/button click

from aiogram_dialog.

yevhen-kalyna avatar yevhen-kalyna commented on June 12, 2024

I used switch_to in my add_item() and its helped.

async def add_item(m: Message, dialog: Dialog, dialog_manager: DialogManager):
    dialog_manager.current_context().dialog_data["item"] = m.text
    await m.answer(f"Your item '{m.text}' added")
    await dialog_manager.switch_to(ManageItemsSG.show_items)

The issue can be closed.

from aiogram_dialog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.