Use builder pattern for rendering forms.

This commit is contained in:
Dillon Kearns 2022-07-08 10:57:22 -07:00
parent e2505843e0
commit 70b1423bd2
5 changed files with 44 additions and 55 deletions

View File

@ -262,13 +262,8 @@ view maybeUrl sharedModel static =
, Html.h1 , Html.h1
[] []
[ Html.text <| "Edit profile " ++ user.first ++ " " ++ user.last ] [ Html.text <| "Edit profile " ++ user.first ++ " " ++ user.last ]
, Form.renderHtml , form
{ method = Form.Post |> Form.toDynamicTransition "test1"
, submitStrategy = Form.TransitionStrategy |> Form.renderHtml static defaultUser
, name = Just "test1"
}
static
defaultUser
form
] ]
} }

View File

@ -195,7 +195,10 @@ view maybeUrl sharedModel model static =
{ title = "Search" { title = "Search"
, body = , body =
[ Html.h2 [] [ Html.text "Search" ] [ Html.h2 [] [ Html.text "Search" ]
, Form.renderHtml { method = Form.Get, submitStrategy = Form.TransitionStrategy, name = Just "test1" } static () form , form
|> Form.toDynamicTransition "test1"
|> Form.withGetMethod
|> Form.renderHtml static ()
, static.data.results , static.data.results
|> Maybe.map resultsView |> Maybe.map resultsView
|> Maybe.withDefault (Html.div [] []) |> Maybe.withDefault (Html.div [] [])

View File

@ -264,7 +264,9 @@ view maybeUrl sharedModel model static =
Html.text "" Html.text ""
] ]
, flashView static.data.flashMessage , flashView static.data.flashMessage
, Form.renderHtml { method = Form.Post, submitStrategy = Form.TransitionStrategy, name = Just "test1" } static () form , form
|> Form.toDynamicTransition "test1"
|> Form.renderHtml static ()
] ]
} }

View File

@ -340,14 +340,9 @@ view maybeUrl sharedModel model static =
[ Html.text item.description [ Html.text item.description
-- TODO should the (List Html.Attribute) be passed in to renderHtml instead of the `( List Html, List Attr)` in Form.init? -- TODO should the (List Html.Attribute) be passed in to renderHtml instead of the `( List Html, List Attr)` in Form.init?
, Form.renderHtml , deleteForm
{ submitStrategy = Form.TransitionStrategy |> Form.toDynamicTransition "test1"
, method = Form.Post |> Form.renderHtml static item.id
, name = Just "test1"
}
static
item.id
deleteForm
] ]
) )
) )
@ -364,13 +359,8 @@ view maybeUrl sharedModel model static =
] ]
) )
) )
, Form.renderHtml , createForm
{ submitStrategy = Form.TransitionStrategy |> Form.toDynamicTransition "test2"
, method = Form.Post |> Form.renderHtml static ()
, name = Just "test2"
}
static
()
createForm
] ]
} }

View File

@ -5,7 +5,7 @@ module Pages.Form exposing
, andThen , andThen
, Context, ViewField , Context, ViewField
, renderHtml, renderStyledHtml , renderHtml, renderStyledHtml
, Method(..), SubmitStrategy(..) , FinalForm, withGetMethod, toDynamicTransition, toDynamicFetcher
, parse, runOneOfServerSide, runServerSide , parse, runOneOfServerSide, runServerSide
, dynamic, HtmlSubForm , dynamic, HtmlSubForm
, runOneOfServerSideWithServerValidations , runOneOfServerSideWithServerValidations
@ -13,8 +13,6 @@ module Pages.Form exposing
, AppContext , AppContext
, RenderOptions , RenderOptions
-- subGroup -- subGroup
, toDynamicFetcher
, toDynamicTransition
) )
@ -47,10 +45,7 @@ module Pages.Form exposing
@docs renderHtml, renderStyledHtml @docs renderHtml, renderStyledHtml
@docs FinalForm, withGetMethod, toDynamicTransition, toDynamicFetcher
### Rendering Options
@docs Method, SubmitStrategy
## Running Parsers ## Running Parsers
@ -859,11 +854,10 @@ runOneOfServerSideWithServerValidations rawFormData parsers =
{-| -} {-| -}
renderHtml : renderHtml :
RenderOptions AppContext app
-> AppContext app
-> data -> data
-> ->
Form FinalForm
error error
(Validation error parsed) (Validation error parsed)
data data
@ -871,8 +865,8 @@ renderHtml :
-> ( List (Html.Attribute (Pages.Msg.Msg msg)), List (Html (Pages.Msg.Msg msg)) ) -> ( List (Html.Attribute (Pages.Msg.Msg msg)), List (Html (Pages.Msg.Msg msg)) )
) )
-> Html (Pages.Msg.Msg msg) -> Html (Pages.Msg.Msg msg)
renderHtml options app data combinedParser = renderHtml app data (FinalForm options a b c) =
Html.Lazy.lazy4 renderHelper options app data combinedParser Html.Lazy.lazy4 renderHelper options app data (Form a b c)
{-| -} {-| -}
@ -933,6 +927,11 @@ toDynamicTransition name (Form a b c) =
FinalForm options a b c FinalForm options a b c
withGetMethod : FinalForm error parsed data view -> FinalForm error parsed data view
withGetMethod (FinalForm options a b c) =
FinalForm { options | method = Get } a b c
{-| -} {-| -}
renderStyledHtml : renderStyledHtml :
List (Html.Styled.Attribute (Pages.Msg.Msg msg)) List (Html.Styled.Attribute (Pages.Msg.Msg msg))
@ -1118,27 +1117,27 @@ renderStyledHelperNew attrs maybe options formState data (Form fieldDefinitions
part2 : Dict String Form.FieldState part2 : Dict String Form.FieldState
part2 = part2 =
--formState.pageFormState
-- |> Dict.get formId
-- |> Maybe.withDefault
-- (maybe
-- |> Maybe.map
-- (\{ fields } ->
-- { fields =
-- fields
-- |> List.map (Tuple.mapSecond (\value -> { value = value, status = Form.NotVisited }))
-- |> Dict.fromList
-- , submitAttempted = True
-- }
-- )
-- |> Maybe.withDefault initFormState
-- )
-- |> .fields
formState.pageFormState formState.pageFormState
|> Dict.get formId |> Dict.get formId
|> Maybe.withDefault
(maybe
|> Maybe.map
(\{ fields } ->
{ fields =
fields
|> List.map (Tuple.mapSecond (\value -> { value = value, status = Form.NotVisited }))
|> Dict.fromList
, submitAttempted = True
}
)
|> Maybe.withDefault initFormState |> Maybe.withDefault initFormState
)
|> .fields |> .fields
--formState.pageFormState
-- |> Dict.get formId
-- |> Maybe.withDefault initFormState
-- |> .fields
fullFormState : Dict String Form.FieldState fullFormState : Dict String Form.FieldState
fullFormState = fullFormState =
initialValues initialValues