Wire in onSubmit for TransitionStrategy.

This commit is contained in:
Dillon Kearns 2023-02-28 15:59:55 -08:00
parent c53c09aa31
commit 4ae64cb5bd
4 changed files with 28 additions and 11 deletions

View File

@ -166,7 +166,7 @@ view maybeUrl sharedModel model static =
-- TODO should there be a helper function to easily invoke a form submission to a different route?
[ Attr.method "post"
, Attr.action "/logout"
, Pages.Msg.onSubmit |> Attr.fromUnstyled
, PagesMsg.onSubmit |> Attr.fromUnstyled
]
[ Html.button [] [ Html.text "Logout" ] ]
]

View File

@ -1232,8 +1232,7 @@ renderHelper attrs accessResponse options formState data form =
Pages.Internal.Msg.fetcherOnSubmit options.onSubmit formId (\_ -> isValid)
TransitionStrategy ->
-- TODO pass in options.onSubmit here
Pages.Internal.Msg.submitIfValid formId (\_ -> isValid)
Pages.Internal.Msg.submitIfValid options.onSubmit formId (\_ -> isValid)
]
++ attrs
)
@ -1271,7 +1270,7 @@ renderStyledHelper attrs accessResponse options formState data form =
TransitionStrategy ->
StyledAttr.fromUnstyled <|
Pages.Internal.Msg.submitIfValid formId (\_ -> isValid)
Pages.Internal.Msg.submitIfValid options.onSubmit formId (\_ -> isValid)
]
++ attrs
)

View File

@ -17,7 +17,7 @@ import Json.Decode
type Msg userMsg
= UserMsg userMsg
| Submit FormData
| SubmitIfValid String FormData Bool
| SubmitIfValid String FormData Bool (Maybe userMsg)
| SubmitFetcher String FormData Bool (Maybe userMsg)
| FormFieldEvent Json.Decode.Value
| NoOp
@ -31,10 +31,21 @@ onSubmit =
{-| -}
submitIfValid : String -> (List ( String, String ) -> Bool) -> Attribute (Msg userMsg)
submitIfValid formId isValid =
submitIfValid : Maybe ({ fields : List ( String, String ) } -> userMsg) -> String -> (List ( String, String ) -> Bool) -> Attribute (Msg userMsg)
submitIfValid userMsg formId isValid =
FormDecoder.formDataOnSubmit
|> Attr.map (\formData -> SubmitIfValid formId formData (isValid formData.fields))
|> Attr.map
(\formData ->
SubmitIfValid formId
formData
(isValid formData.fields)
(userMsg
|> Maybe.map
(\toUserMsg ->
toUserMsg { fields = formData.fields }
)
)
)
{-| -}
@ -65,8 +76,8 @@ map mapFn msg =
Submit info ->
Submit info
SubmitIfValid formId info isValid ->
SubmitIfValid formId info isValid
SubmitIfValid formId info isValid toUserMsg ->
SubmitIfValid formId info isValid (Maybe.map mapFn toUserMsg)
SubmitFetcher formId info isValid toUserMsg ->
SubmitFetcher formId info isValid (Maybe.map mapFn toUserMsg)

View File

@ -508,7 +508,7 @@ update config appMsg model =
, Submit fields
)
Pages.Internal.Msg.SubmitIfValid formId fields isValid ->
Pages.Internal.Msg.SubmitIfValid formId fields isValid maybeUserMsg ->
if isValid then
( { model
-- TODO should I setSubmitAttempted here, too?
@ -521,6 +521,13 @@ update config appMsg model =
}
, Submit fields
)
|> (case maybeUserMsg of
Just justUserMsg ->
performUserMsg justUserMsg config
Nothing ->
identity
)
else
( { model