agocorona / MFlow

(Haskell) Web application server with stateful, type safe user interactions and widget combinators
http://mflowdemo.herokuapp.com
Other
100 stars 12 forks source link

Monad Formlet & Factoring Out Button #68

Open nickgeoca opened 8 years ago

nickgeoca commented 8 years ago

Description: After the 1st number is entered in the First field, the Second field is populated with "submit". As seen in picture below. The code sample is below too. Note that changing "(Just n1)" to "Nothing" does not change the behavior. image

Line: n2 <- "Second " ++> getInt (Just n1) <++ br

Version

Code

{-# LANGUAGE OverloadedStrings #-}
module Main
where
import MFlow.Wai.Blaze.Html.All hiding(main)

main = runNavigation "" . step . page . pageFlow "s" $ 
  do  n1 <- "First "  ++> getInt Nothing   <++ br
      n2 <- "Second " ++> getInt (Just n1) <++ br
      p  << (n1 + n2) ++> noWidget

  <** br ++> submitButton "submit"
agocorona commented 8 years ago

Nick, for this purposes is the "pageFlow" modifier.

This bad effect is unavoidable in monadic flows when there are alternative branches and the page is not fully rendered, since the generator of field identifiers assign them sequentially and the values are filled from the previous interactions. `pageFlow´ add a prefix that prevent the new fields to be populated with values from other branches.

when the second getInt is rendered for the first time, it receives the identifier "p1" but this "p1" was in the previously the identifier of the submitButton field, that had "submit" as value, so the generator wrongly assign this to the second sum field. But this is unavoidable unless submitButton is preceded with apageFlow` prefix, so that the identifier generated for this field do not clash with the ones of the sum:

main = runNavigation "" . step . page . pageFlow "s" $
  do  n1 <- "First "  ++> getInt Nothing   <++ br
      n2 <- "Second " ++> getInt (Just n1) <++ br
      p  << (n1 + n2) ++> noWidget

  <** br ++> pageFlow "button" (submitButton "submit")

In this example, the submintButton field receive the identifier "submitp0" the first time that it is rendered, so there is no clash with the monadic expression above. the zero is because pageFlowalso restart the field counter for the branch.