Update form parsing logic with new server form parsers.

This commit is contained in:
Dillon Kearns 2022-08-23 15:38:10 -07:00
parent a106496052
commit 255cd8c6a4
2 changed files with 44 additions and 24 deletions

View File

@ -903,7 +903,17 @@ runOneOfServerSide :
List ( String, String ) List ( String, String )
-> ServerForms error parsed -> ServerForms error parsed
-> ( Maybe parsed, Dict String (List error) ) -> ( Maybe parsed, Dict String (List error) )
runOneOfServerSide rawFormData (ServerForms parsers) = runOneOfServerSide rawFormData forms =
runOneOfServerSideHelp rawFormData Nothing forms
{-| -}
runOneOfServerSideHelp :
List ( String, String )
-> Maybe (List ( String, List error ))
-> ServerForms error parsed
-> ( Maybe parsed, Dict String (List error) )
runOneOfServerSideHelp rawFormData firstFoundErrors (ServerForms parsers) =
case parsers of case parsers of
firstParser :: remainingParsers -> firstParser :: remainingParsers ->
let let
@ -918,15 +928,23 @@ runOneOfServerSide rawFormData (ServerForms parsers) =
) )
in in
case thing of case thing of
( Just parsed, [] ) -> ( Just parsed, errors ) ->
( Just parsed, Dict.empty ) ( Just parsed, errors |> Dict.fromList )
_ -> ( Nothing, errors ) ->
runOneOfServerSide rawFormData (ServerForms remainingParsers) runOneOfServerSideHelp rawFormData
(firstFoundErrors
-- TODO is this logic what we want here? Might need to think through the semantics a bit more
-- of which errors to parse into - could be the first errors, the last, or some other way of
-- having higher precedence for deciding which form should be used
|> Maybe.withDefault errors
|> Just
)
(ServerForms remainingParsers)
[] -> [] ->
-- TODO need to pass errors -- TODO need to pass errors
( Nothing, Dict.empty ) ( Nothing, firstFoundErrors |> Maybe.withDefault [] |> Dict.fromList )
{-| -} {-| -}

View File

@ -70,29 +70,31 @@ all =
) )
, describe "oneOf" <| , describe "oneOf" <|
let let
oneOfParsers : Form.ServerForms String Action
oneOfParsers = oneOfParsers =
Form.init Form.initCombined SetQuantity
(\_ -> (Form.init
{ combine = Validation.succeed () (\_ uuid quantity ->
, view = \_ -> Div { combine =
} Validation.succeed Tuple.pair
|> Validation.andMap (uuid |> Validation.map Uuid)
|> Validation.andMap quantity
, view =
\_ -> Div
}
)
|> Form.hiddenField "kind" (Field.exactValue "setQuantity" "Expected setQuantity")
|> Form.hiddenField "uuid" (Field.text |> Field.required "Required")
|> Form.field "quantity" (Field.int { invalid = \_ -> "Expected int" } |> Field.required "Required")
) )
|> Form.hiddenField "kind" (Field.exactValue "signout" "Expected signout") |> Form.combine (\() -> Signout)
|> Form.initCombined (\() -> Signout)
|> Form.combine SetQuantity
(Form.init (Form.init
(\_ uuid quantity -> (\_ ->
{ combine = { combine = Validation.succeed ()
Validation.succeed Tuple.pair , view = \_ -> Div
|> Validation.andMap (uuid |> Validation.map Uuid)
|> Validation.andMap quantity
, view =
\_ -> Div
} }
) )
|> Form.hiddenField "kind" (Field.exactValue "setQuantity" "Expected setQuantity") |> Form.hiddenField "kind" (Field.exactValue "signout" "Expected signout")
|> Form.hiddenField "uuid" (Field.text |> Field.required "Required")
|> Form.field "quantity" (Field.int { invalid = \_ -> "Expected int" } |> Field.required "Required")
) )
in in
[ test "first branch" <| [ test "first branch" <|