Dynamic sub-form parsing renders views properly.

This commit is contained in:
Dillon Kearns 2022-06-20 15:13:27 -07:00
parent 58c7a84038
commit b194671b9e
2 changed files with 125 additions and 52 deletions

View File

@ -119,13 +119,17 @@ type PostAction
| ParsedPost { title : String, body : Maybe String }
linkForm : Form.HtmlForm String PostAction data Msg
linkForm : Form.HtmlSubForm String PostAction data Msg
linkForm =
Form.init
(\url ->
Form.ok (ParsedLink url.value)
)
(\fieldErrors url -> ( [], [] ))
(\fieldErrors url ->
[ Html.text "Create a link"
, url |> Pages.FieldRenderer.input []
]
)
|> Form.field "url"
(Field.text
|> Field.required "Required"
@ -133,7 +137,7 @@ linkForm =
)
postForm : Form.HtmlForm String PostAction data Msg
postForm : Form.HtmlSubForm String PostAction data Msg
postForm =
Form.init
(\title body ->
@ -144,7 +148,12 @@ postForm =
}
)
)
(\fieldErrors title body -> ( [], [] ))
(\fieldErrors title body ->
[ Html.text "Create a link"
, title |> Pages.FieldRenderer.input []
, body |> Pages.FieldRenderer.input []
]
)
|> Form.field "title" (Field.text |> Field.required "Required")
|> Form.field "body" Field.text
@ -154,12 +163,33 @@ dependentParser =
Form.init
(\kind postForm_ ->
postForm_ kind.value
|> Form.andThen identity
)
(\formState kind postForm_ ->
let
parsedKind : Maybe PostKind
parsedKind =
-- TODO don't manually parse, this should be provided as a record field (`parsed : Maybe parsed`)
case kind.value |> Debug.log "@@@kind.value" of
Just "link" ->
Just Link
Just "post" ->
Just Post
_ ->
Nothing
something : List (Html (Pages.Msg.Msg Msg))
something =
-- TODO do I need to have `Maybe parsed` available in view fields?
postForm_ Nothing
-- TODO show "please choose an option" if `Maybe parsed` is `Nothing`
case parsedKind of
Just justKind ->
postForm_ justKind
Nothing ->
[ Html.text "Please select a post kind" ]
errors field =
formState.errors
@ -187,7 +217,22 @@ dependentParser =
]
in
( []
, [--postForm_ Nothing
, [ Pages.FieldRenderer.radio []
(\enum toRadio ->
Html.label []
[ toRadio []
, Html.text
(case enum of
Link ->
"Link"
Post ->
"Post"
)
]
)
kind
, Html.div [] something
]
)
)

View File

@ -8,7 +8,7 @@ module Pages.Form exposing
, renderHtml, renderStyledHtml
, parse, runOneOfServerSide, runServerSide
, FieldDefinition(..)
, andThen, dynamic
, HtmlSubForm, andThen, dynamic
)
{-|
@ -137,7 +137,7 @@ init fn viewFn =
dynamic :
(decider -> Form error parsed data (Context String -> viewFn))
(decider -> Form error parsed data (Context error -> subView))
->
Form
error
@ -146,15 +146,15 @@ dynamic :
-- -> combined
--)
--dontKnowYet
((decider -> parsed) -> combined)
((decider -> ( Maybe parsed, FieldErrors error )) -> combined)
data
(Context error -> ((Maybe decider -> ViewField ()) -> combinedView))
(Context error -> ((decider -> subView) -> combinedView))
-> Form error combined data (Context error -> combinedView)
dynamic forms formBuilder =
Form []
(\maybeData formState ->
let
toParser : decider -> { result : ( Maybe parsed, FieldErrors error ), view : Context String -> viewFn }
toParser : decider -> { result : ( Maybe parsed, FieldErrors error ), view : Context error -> subView }
toParser decider =
case forms decider of
Form definitions parseFn toInitialValues ->
@ -212,42 +212,42 @@ dynamic forms formBuilder =
}
myFn =
let
--decider =
-- Debug.todo ""
--( fieldThings, errorsSoFar ) =
-- toParser decider |> .result
--combineFn : parsed -> Maybe combined
--combineFn =
-- Debug.todo ""
--deciderToParsed : ( Maybe parsed, FieldErrors error )
deciderToParsed : decider -> ( Maybe parsed, FieldErrors error )
deciderToParsed decider =
case
decider
|> toParser
|> .result
of
( Just okParsed, _ ) ->
okParsed
( Nothing, _ ) ->
Debug.todo "TODO - don't call parser at all in this case"
--case
decider
|> toParser
|> .result
--of
-- ( Just okParsed, _ ) ->
-- okParsed
--
-- ( Nothing, _ ) ->
-- Debug.todo "TODO - don't call parser at all in this case"
newThing :
{ result :
( Maybe
((decider -> parsed) -> combined)
((decider -> ( Maybe parsed, FieldErrors error )) -> combined)
, Dict String (List error)
)
, view : Context error -> (Maybe decider -> ViewField ()) -> combinedView
, view : Context error -> (decider -> subView) -> combinedView
}
newThing =
case formBuilder of
Form definitions parseFn toInitialValues ->
parseFn maybeData formState
--whatsThis : Maybe ((decider -> parsed) -> combined)
--whatsThis =
-- newThing.result |> Tuple.first
anotherThing : Maybe combined
anotherThing =
Maybe.map2 (|>) (Just deciderToParsed) (newThing.result |> Tuple.first)
Maybe.map2
(\thing1 thing2 -> thing1 |> thing2)
(Just deciderToParsed)
(newThing.result |> Tuple.first)
in
{ result =
( --case fieldThings of
@ -266,13 +266,19 @@ dynamic forms formBuilder =
, view =
\fieldErrors ->
let
something2 : Maybe decider -> ViewField ()
something2 maybeDecider =
{ name = ""
, value = Nothing
, status = Form.NotVisited
, kind = ( (), [] )
}
something2 : decider -> subView
something2 decider =
fieldErrors
|> (decider
|> toParser
|> .view
)
--{ name = ""
--, value = Nothing
--, status = Form.NotVisited
--, kind = ( (), [] )
--}
in
newThing.view fieldErrors something2
@ -304,10 +310,18 @@ dynamic forms formBuilder =
andThen : (parsed -> ( Maybe combined, FieldErrors error )) -> ( Maybe parsed, FieldErrors error ) -> ( Maybe combined, FieldErrors error )
andThen andThenFn ( maybe, fieldErrors ) =
Debug.todo ""
case maybe of
Just justValue ->
andThenFn justValue
|> Tuple.mapSecond (mergeErrors fieldErrors)
Nothing ->
( Nothing, fieldErrors )
--( maybe andThenFn, fieldErrors )
--Debug.todo ""
{-
Form
error
@ -602,25 +616,30 @@ mergeResults parsed =
case parsed.result of
( Just ( parsedThing, combineErrors ), individualFieldErrors ) ->
( parsedThing
, Dict.merge
(\key entries soFar ->
soFar |> insertIfNonempty key entries
)
(\key entries1 entries2 soFar ->
soFar |> insertIfNonempty key (entries1 ++ entries2)
)
(\key entries soFar ->
soFar |> insertIfNonempty key entries
)
combineErrors
individualFieldErrors
Dict.empty
, mergeErrors combineErrors individualFieldErrors
)
( Nothing, individualFieldErrors ) ->
( Nothing, individualFieldErrors )
mergeErrors : Dict comparable (List value) -> Dict comparable (List value) -> Dict comparable (List value)
mergeErrors errors1 errors2 =
Dict.merge
(\key entries soFar ->
soFar |> insertIfNonempty key entries
)
(\key entries1 entries2 soFar ->
soFar |> insertIfNonempty key (entries1 ++ entries2)
)
(\key entries soFar ->
soFar |> insertIfNonempty key entries
)
errors1
errors2
Dict.empty
{-| -}
parse :
AppContext app
@ -1042,6 +1061,15 @@ type alias HtmlForm error parsed data msg =
(Context error -> ( List (Html.Attribute (Pages.Msg.Msg msg)), List (Html (Pages.Msg.Msg msg)) ))
{-| -}
type alias HtmlSubForm error parsed data msg =
Form
error
( Maybe parsed, FieldErrors error )
data
(Context error -> List (Html (Pages.Msg.Msg msg)))
{-| -}
type alias StyledHtmlForm error parsed data msg =
Form