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 )
-> ServerForms error parsed
-> ( 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
firstParser :: remainingParsers ->
let
@ -918,15 +928,23 @@ runOneOfServerSide rawFormData (ServerForms parsers) =
)
in
case thing of
( Just parsed, [] ) ->
( Just parsed, Dict.empty )
( Just parsed, errors ) ->
( Just parsed, errors |> Dict.fromList )
_ ->
runOneOfServerSide rawFormData (ServerForms remainingParsers)
( Nothing, errors ) ->
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
( Nothing, Dict.empty )
( Nothing, firstFoundErrors |> Maybe.withDefault [] |> Dict.fromList )
{-| -}

View File

@ -70,29 +70,31 @@ all =
)
, describe "oneOf" <|
let
oneOfParsers : Form.ServerForms String Action
oneOfParsers =
Form.init
(\_ ->
{ combine = Validation.succeed ()
, view = \_ -> Div
}
Form.initCombined SetQuantity
(Form.init
(\_ uuid quantity ->
{ 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.initCombined (\() -> Signout)
|> Form.combine SetQuantity
|> Form.combine (\() -> Signout)
(Form.init
(\_ uuid quantity ->
{ combine =
Validation.succeed Tuple.pair
|> Validation.andMap (uuid |> Validation.map Uuid)
|> Validation.andMap quantity
, view =
\_ -> Div
(\_ ->
{ combine = Validation.succeed ()
, 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")
)
in
[ test "first branch" <|