docassemble

Recipe for "warning screen" + go back


I'm trying to implement a "that feature not yet supported" screen. I imagine there are many such "offramp" screens in a number of apps. I did look through the recipies section of the docs, but I didn't see anything.

Ideally, I want the user to be able to, from that screen (1) go back to the question that set variable leading them to the screen, or (2) exit the interview.

I would also like variable to be reset, so the question is re-asked.

Something like this, I would think?

---
id: problematic_question
# sets the value of var
---
mandatory: True
code: |
    if var == "x":
        undefine("var") # not sure if undefine() or forget_result_of('problematic_question') would be better here, or if they're perhaps equivalent for this use case [or if I'm completely off]
        force_ask("event_not_supported")
---
event: event_not_supported
question: (!!!) This option not supported
buttons:
  - Go back: refresh # if I understand correctly, undefining var, in conjunction with refresh, should take the user back to problematic_question
  - Exit: leave
---

I am running into a problem where I'm getting an infinite loop, where DA is attempting to define event_not_supported. I'm also not sure if my general approach would achieve the result / flow I want.

Help would be appreciated:

  1. Is there a standard way off approaching such "off ramps"
  2. Is there any reason, based on the example above, that an infinite loop should be occurring (presumably, DA is not actually undefining variable as expected, and thus continuously seeking the definition of event_not_supported?

Solution

  • force_ask() has some different behaviors that are only useful under special circumstances. My way of approaching off-ramps is to use the normal logic flow. Something like:

    ---
    id: problematic_question
    # sets the value of my_var
    ---
    mandatory: True
    code: |
      if my_var == "x":
        event_not_supported
    ---
    event: event_not_supported
    question: (!!!) This option not supported
    back button: True
    buttons:
      - Exit: exit
        url: https://example.com
    ---
    

    The back button attribute will make sure to show the usual back button. Hitting 'Back' will let the user redefine the value for my_var, so there won't be a reason to undefine() it. exit is a special button value (https://docassemble.org/docs/questions.html#special%20buttons).

    exit means that the user’s variable store will be erased and the user will be redirected either to the URL given by the associated url text, or if no url is defined, to the exit url defined in the metadata, or if that does not defined, then to the exitpage defined in the configuration. If the user tries to come back to the interview again, he will start the interview again, as though it had never been started. Original URL parameters will be lost.

    As for the infinite loop, the docs say

    Note also that no code that comes after force_ask() will ever be executed.

    The phrasing is a bit confusing in a case like this, but I believe that it also means nothing more will be run for this code - neither code before nor code after will get triggered anymore. That block, the logic flow that brought you to the code force_ask(), is done once you trigger force_ask().

    Sorry, my mistake about the infinite loop. The behavior I was remembering was old behavior. Is there other code that you haven't included here?