Include a view function in form parser.

This commit is contained in:
Dillon Kearns 2022-06-08 08:37:36 -07:00
parent 4f5bbce442
commit 5e9275ec4c
2 changed files with 124 additions and 61 deletions

View File

@ -70,11 +70,13 @@ requiredString error =
)
andThenNew : a -> CombinedParser String a
andThenNew fn =
andThenNew : combined -> viewFn -> CombinedParser String combined viewFn
andThenNew fn viewFn =
CombinedParser []
(\formState ->
( Just fn, Dict.empty )
{ result = ( Just fn, Dict.empty )
, view = viewFn
}
)
@ -94,8 +96,8 @@ andThenNew fn =
field :
String
-> FieldThing error parsed
-> CombinedParser error (ParsedField error parsed -> a)
-> CombinedParser error a
-> CombinedParser error (ParsedField error parsed -> combined) (RawField -> combinedView)
-> CombinedParser error combined combinedView
field name (FieldThing fieldParser) (CombinedParser definitions parseFn) =
CombinedParser
(( name, FieldDefinition )
@ -118,12 +120,35 @@ field name (FieldThing fieldParser) (CombinedParser definitions parseFn) =
}
)
rawField : RawField
rawField =
{ name = name
, value = formState |> Dict.get name |> Maybe.map .value
}
--{ result :
-- ( Maybe parsed
-- , Dict String (List error)
-- )
--, view : view
--}
myFn :
( Maybe (ParsedField error parsed -> a)
{ result :
( Maybe (ParsedField error parsed -> combined)
, Dict String (List error)
)
-> ( Maybe a, Dict String (List error) )
myFn ( fieldThings, errorsSoFar ) =
, view : RawField -> combinedView
}
->
{ result : ( Maybe combined, Dict String (List error) )
, view : combinedView
}
myFn soFar =
let
( fieldThings, errorsSoFar ) =
soFar.result
in
{ result =
( case fieldThings of
Just fieldPipelineFn ->
parsedField
@ -134,6 +159,8 @@ field name (FieldThing fieldParser) (CombinedParser definitions parseFn) =
, errorsSoFar
|> addErrors name errors
)
, view = soFar.view rawField
}
in
formState
|> parseFn
@ -188,14 +215,30 @@ input attrs fieldThing =
[]
runNew : Form.FormState -> CombinedParser error parsed -> ( Maybe parsed, Dict String (List error) )
runNew :
Form.FormState
-> CombinedParser error parsed view
->
{ result : ( Maybe parsed, Dict String (List error) )
, view : view
}
runNew formState (CombinedParser fieldDefinitions parser) =
--Debug.todo ""
parser formState
type CombinedParser error parsed
= CombinedParser (List ( String, FieldDefinition )) (Form.FormState -> ( Maybe parsed, Dict String (List error) ))
type CombinedParser error parsed view
= CombinedParser
(List ( String, FieldDefinition ))
(Form.FormState
->
{ result :
( Maybe parsed
, Dict String (List error)
)
, view : view
}
)
@ -253,6 +296,12 @@ type alias ParsedField error parsed =
}
type alias RawField =
{ name : String
, value : Maybe String
}
value : FullFieldThing error parsed -> parsed
value =
Debug.todo ""

View File

@ -51,34 +51,34 @@ all =
-- [ ( "password-confirmation", [ "Must match password" ] )
-- ]
-- )
test "non-dependent field error" <|
\() ->
FormParser.runNew
(fields
[ ( "password", "mypassword" )
, ( "password-confirmation", "" )
]
)
(FormParser.andThenNew
(\password passwordConfirmation ->
if password.value == passwordConfirmation.value then
--passwordConfirmation |> FormParser.withError "Must match password"
Debug.todo ""
else
FormParser.ok { password = password }
)
|> FormParser.field "password" (FormParser.requiredString "Password is required")
|> FormParser.field "password-confirmation" (FormParser.requiredString "Password confirmation is required")
)
|> Expect.equal
( Nothing
, Dict.fromList
[ ( "password", [] )
, ( "password-confirmation", [ "Password confirmation is required" ] )
]
)
, test "new design idea 3" <|
--test "non-dependent field error" <|
-- \() ->
-- FormParser.runNew
-- (fields
-- [ ( "password", "mypassword" )
-- , ( "password-confirmation", "" )
-- ]
-- )
-- (FormParser.andThenNew
-- (\password passwordConfirmation ->
-- if password.value == passwordConfirmation.value then
-- --passwordConfirmation |> FormParser.withError "Must match password"
-- Debug.todo ""
--
-- else
-- FormParser.ok { password = password }
-- )
-- |> FormParser.field "password" (FormParser.requiredString "Password is required")
-- |> FormParser.field "password-confirmation" (FormParser.requiredString "Password confirmation is required")
-- )
-- |> Expect.equal
-- ( Nothing
-- , Dict.fromList
-- [ ( "password", [] )
-- , ( "password-confirmation", [ "Password confirmation is required" ] )
-- ]
-- ),
test "new design idea 3" <|
\() ->
FormParser.runNew
(fields
@ -88,16 +88,15 @@ all =
)
(FormParser.andThenNew
(\password passwordConfirmation ->
--{
--dependentErrors =
if password.value /= passwordConfirmation.value then
Debug.todo ""
--passwordConfirmation |> FormParser.withError "Must match password"
else
{ password = password.value }
--FormParser.ok
--, view =
FormParser.ok { password = password.value }
)
(\password passwordConfirmation ->
Div
--Html.form []
-- [ password |> FormParser.input []
-- , passwordConfirmation |> FormParser.input []
@ -107,8 +106,23 @@ all =
|> FormParser.field "password" (FormParser.string "Password is required")
|> FormParser.field "password-confirmation" (FormParser.string "Password confirmation is required")
)
|> expectNoErrors { password = "mypassword" }
|> Expect.equal
{ result =
( Just { password = "mypassword" }
, Dict.fromList
[ ( "password", [] )
, ( "password-confirmation", [] )
]
)
, view = Div
}
--|> expectNoErrors { password = "mypassword" }
]
type MyView
= Div
expectNoErrors : parsed -> ( Maybe parsed, Dict String (List error) ) -> Expect.Expectation