Add render option for submit strategy.

This commit is contained in:
Dillon Kearns 2022-06-22 14:34:05 -07:00
parent 1bc2b2a315
commit 2a530b48e6
7 changed files with 53 additions and 37 deletions

View File

@ -124,7 +124,7 @@ view :
view maybeUrl sharedModel model app =
{ title = "Create Group"
, body =
[ Form.renderHtml app () postForm
[ Form.renderHtml { submitStrategy = Form.TransitionStrategy } app () postForm
]
}

View File

@ -1,7 +1,6 @@
module Route.DependentForm exposing (ActionData, Data, Model, Msg, route)
import DataSource exposing (DataSource)
import Dict
import Effect exposing (Effect)
import ErrorPage exposing (ErrorPage)
import Head
@ -128,7 +127,7 @@ view maybeUrl sharedModel model app =
{ title = "Dependent Form Example"
, body =
[ Html.h2 [] [ Html.text "Example" ]
, Form.renderHtml app () dependentParser
, Form.renderHtml { submitStrategy = Form.TransitionStrategy } app () dependentParser
]
}

View File

@ -267,7 +267,7 @@ view maybeUrl sharedModel model app =
]
, Html.p []
[ Html.text <| "Welcome " ++ app.data.user.name ++ "!"
, Form.renderHtml app () signoutForm
, Form.renderHtml { submitStrategy = Form.FetcherStrategy } app () signoutForm
]
, cartView totals
, app.data.smoothies
@ -317,9 +317,9 @@ productView app cart item =
]
, Html.div
[]
[ Form.renderHtml app ( quantityInCart, Decrement, item ) setQuantityForm
[ Form.renderHtml { submitStrategy = Form.FetcherStrategy } app ( quantityInCart, Decrement, item ) setQuantityForm
, Html.p [] [ quantityInCart |> String.fromInt |> Html.text ]
, Form.renderHtml app ( quantityInCart, Increment, item ) setQuantityForm
, Form.renderHtml { submitStrategy = Form.FetcherStrategy } app ( quantityInCart, Increment, item ) setQuantityForm
]
, Html.div []
[ Html.img

View File

@ -8,7 +8,6 @@ import Dict.Extra
import Effect exposing (Effect)
import ErrorPage exposing (ErrorPage)
import Head
import Head.Seo as Seo
import Html exposing (Html)
import Html.Attributes as Attr
import MySession
@ -18,7 +17,6 @@ import Pages.Form as Form
import Pages.FormState
import Pages.Msg
import Pages.PageUrl exposing (PageUrl)
import Pages.Url
import Path exposing (Path)
import Request.Hasura
import Route
@ -216,7 +214,7 @@ view maybeUrl sharedModel model app =
{ title = "New Item"
, body =
[ Html.h2 [] [ Html.text "New item" ]
, Form.renderHtml app app.data form
, Form.renderHtml { submitStrategy = Form.TransitionStrategy } app app.data form
, pendingCreation
|> Debug.log "pendingCreation"
|> Result.toMaybe

View File

@ -247,6 +247,6 @@ view maybeUrl sharedModel model app =
, body =
[ Html.p []
[ Html.text <| "Welcome " ++ app.data.user.name ++ "!" ]
, Form.renderHtml app app.data formParser
, Form.renderHtml { submitStrategy = Form.TransitionStrategy } app app.data formParser
]
}

View File

@ -354,11 +354,11 @@ view maybeUrl sharedModel model app =
{ title = "Update Item"
, body =
[ Html.h2 [] [ Html.text "Update item" ]
, Form.renderHtml app app.data form
, Form.renderHtml { submitStrategy = Form.TransitionStrategy } app app.data form
, pendingCreation
|> Maybe.map pendingView
|> Maybe.withDefault (Html.div [] [])
, Form.renderHtml app app.data deleteForm
, Form.renderHtml { submitStrategy = Form.TransitionStrategy } app app.data deleteForm
]
}

View File

@ -10,6 +10,7 @@ module Pages.Form exposing
, parse, runOneOfServerSide, runServerSide
, dynamic, HtmlSubForm
, FieldDefinition(..)
, SubmitStrategy(..)
)
{-|
@ -604,7 +605,8 @@ runOneOfServerSide rawFormData parsers =
{-| -}
renderHtml :
AppContext app
RenderOptions
-> AppContext app
-> data
->
Form
@ -615,8 +617,8 @@ renderHtml :
-> ( List (Html.Attribute (Pages.Msg.Msg msg)), List (Html (Pages.Msg.Msg msg)) )
)
-> Html (Pages.Msg.Msg msg)
renderHtml app data combinedParser =
Html.Lazy.lazy3 renderHelper app data combinedParser
renderHtml options app data combinedParser =
Html.Lazy.lazy4 renderHelper options app data combinedParser
{-| -}
@ -637,11 +639,12 @@ renderStyledHtml app data combinedParser =
renderHelper :
AppContext app
RenderOptions
-> AppContext app
-> data
-> Form error ( Maybe parsed, FieldErrors error ) data (Context error -> ( List (Html.Attribute (Pages.Msg.Msg msg)), List (Html (Pages.Msg.Msg msg)) ))
-> Html (Pages.Msg.Msg msg)
renderHelper formState data (Form fieldDefinitions parser toInitialValues) =
renderHelper options formState data (Form fieldDefinitions parser toInitialValues) =
-- TODO Get transition context from `app` so you can check if the current form is being submitted
-- TODO either as a transition or a fetcher? Should be easy enough to check for the `id` on either of those?
let
@ -739,33 +742,39 @@ renderHelper formState data (Form fieldDefinitions parser toInitialValues) =
-- TODO need to make an option to choose `Pages.Msg.fetcherOnSubmit`
-- TODO `Pages.Msg.fetcherOnSubmit` needs to accept an `isValid` param, too
, --Pages.Msg.fetcherOnSubmit
Pages.Msg.submitIfValid
(\fields ->
case
{ initFormState
| fields =
fields
|> List.map (Tuple.mapSecond (\value -> { value = value, status = Form.NotVisited }))
|> Dict.fromList
}
|> parser (Just data)
-- TODO use mergedResults here
|> .result
|> toResult
of
Ok _ ->
True
, case options.submitStrategy of
FetcherStrategy ->
Pages.Msg.fetcherOnSubmit
Err _ ->
False
)
TransitionStrategy ->
Pages.Msg.submitIfValid (isValid parser data)
]
++ formAttributes
)
(hiddenInputs ++ children)
isValid : (Maybe data -> Form.FormState -> { a | result : ( Maybe parsed, FieldErrors error ) }) -> data -> List ( String, String ) -> Bool
isValid parser data fields =
case
{ initFormState
| fields =
fields
|> List.map (Tuple.mapSecond (\value -> { value = value, status = Form.NotVisited }))
|> Dict.fromList
}
|> parser (Just data)
-- TODO use mergedResults here
|> .result
|> toResult
of
Ok _ ->
True
Err _ ->
False
renderStyledHelper :
AppContext app
-> data
@ -964,6 +973,16 @@ type Form error parsed data view
(data -> List ( String, String ))
type alias RenderOptions =
{ submitStrategy : SubmitStrategy
}
type SubmitStrategy
= FetcherStrategy
| TransitionStrategy
{-| -}
type FieldDefinition
= RegularField