Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does it worth thinking about tracking transitioned states? #77

Open
paulofabiano opened this issue May 24, 2021 · 1 comment
Open

Does it worth thinking about tracking transitioned states? #77

paulofabiano opened this issue May 24, 2021 · 1 comment

Comments

@paulofabiano
Copy link

paulofabiano commented May 24, 2021

Hey, 馃憢

I'm working in a complex sequential UI pattern where one component is shown after the other according to actions done by the user. Something like this:

image

Let麓s suppose the first interaction opportunity for the user is chosen one of the options from the first level (A, B, C, and D). So, the user chooses D and another UI component, relative to the D option is presented to the user and now the user must choose one of the following options: S, T, U, V, and X.

So, the user chooses T and another UI component is presented to the user with the following options: 1 to 9.

Machinery allows me to build this easily with some transitions but I have a problem: When the second UI is presented, the first one is yet visible. When the third UI is presented, the first and second are yet visible.

So, I was able to manage that by:

  • My state field is actually a map like this %{state: "ready", path: ["ready"]}
  • When I transit from one state to other, I include the new state in state.path.

In my LiveView, I have all UI components within an if statement, like:

<% if Enum.member?(state.path, "") do %> ... <% end %> (Actually I'm using Surface UI so it is a bit different from that but you got the idea).

It works but I'm not sure if this is an elegant solution and also I started to wonder if worth adding this as a default machinery feature: a path field that returns all the transitioned states: ["ready", "D", "T"]. If the transition allows returning to a previous state, we could just cut the right elements from this same element in the list.

It would not be the complete transition history but more like the absolute path from the initial state until the current one.

Best,
Paulo

@paulofabiano
Copy link
Author

I tried this implementation with persist/2 to define the path before updating the state. This way, in case I transit to a previously transitioned state, I just rewind the path to the first occurrence:

def persist(struct, next_state) do
    path =
      case Enum.member?(struct.path, next_state) do
        true ->
          path =
            Enum.split_while(
              struct.path,
              fn state ->
                !String.equivalent?(state, next_state)
              end
            )
            |> elem(0)

          path ++ [next_state]

        false ->
          struct.path ++ [next_state]
      end

    %{struct | state: next_state, path: path}
  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant