Stub out new design for Form.init with some Debug.todo calls.

This commit is contained in:
Dillon Kearns 2022-07-19 18:36:08 +02:00
parent 642b9a6f9b
commit 379ffc55ba
4 changed files with 153 additions and 64 deletions

View File

@ -7,7 +7,7 @@ import ErrorPage exposing (ErrorPage)
import Form
import Form.Field as Field
import Form.FieldView
import Form.Validation as Validation
import Form.Validation as Validation exposing (Validation)
import Form.Value
import Head
import Head.Seo as Seo
@ -63,80 +63,86 @@ defaultUser =
}
form : Form.HtmlForm String User User Msg
form : Form.HtmlFormNew String User User Msg
form =
Form.init
(\first last username email dob check ->
Validation.succeed User
|> Validation.andMap first
|> Validation.andMap last
|> Validation.andMap username
|> Validation.andMap email
|> Validation.andMap dob
|> Validation.andMap check
)
(\formState firstName lastName username email dob check ->
let
errors field =
formState.errors
|> Form.errorsForField field
Form.init2
(\firstName lastName username email dob check ->
{ combine =
Validation.succeed User
|> Validation.andMap firstName
|> Validation.andMap lastName
|> Validation.andMap username
|> Validation.andMap email
|> Validation.andMap dob
|> Validation.andMap check
, view =
\formState ->
let
errors field =
formState.errors
|> Form.errorsForField2 field
errorsView field =
case ( formState.submitAttempted, field |> errors ) of
( _, first :: rest ) ->
errorsView field =
case
( formState.submitAttempted
, errors field
)
of
( _, first :: rest ) ->
Html.div []
[ Html.ul
[ Attr.style "border" "solid red"
]
(List.map
(\error ->
Html.li []
[ Html.text error
]
)
(first :: rest)
)
]
_ ->
Html.div [] []
fieldView label field =
Html.div []
[ Html.ul
[ Attr.style "border" "solid red"
[ Html.label []
[ Html.text (label ++ " ")
, field |> Form.FieldView.input2 []
]
(List.map
(\error ->
Html.li []
[ Html.text error
]
)
(first :: rest)
)
, errorsView field
]
in
[ fieldView "First" firstName
, fieldView "Last" lastName
, fieldView "Price" username
, fieldView "Image" email
, fieldView "Image" dob
, Html.button []
[ Html.text
(if formState.isTransitioning then
"Updating..."
_ ->
Html.div [] []
fieldView label field =
Html.div []
[ Html.label []
[ Html.text (label ++ " ")
, field |> Form.FieldView.input []
]
, errorsView field
else
"Update"
)
]
in
[ fieldView "First" firstName
, fieldView "Last" lastName
, fieldView "Price" username
, fieldView "Image" email
, fieldView "Image" dob
, Html.button []
[ Html.text
(if formState.isTransitioning then
"Updating..."
else
"Update"
)
]
]
]
}
)
|> Form.field "first"
|> Form.field2 "first"
(Field.text
|> Field.required "Required"
|> Field.withInitialValue (.first >> Form.Value.string)
)
|> Form.field "last"
|> Form.field2 "last"
(Field.text
|> Field.required "Required"
|> Field.withInitialValue (.last >> Form.Value.string)
)
|> Form.field "username"
|> Form.field2 "username"
(Field.text
|> Field.required "Required"
|> Field.withInitialValue (.username >> Form.Value.string)
@ -149,12 +155,12 @@ form =
-- DataSource.succeed []
-- )
)
|> Form.field "email"
|> Form.field2 "email"
(Field.text
|> Field.required "Required"
|> Field.withInitialValue (.email >> Form.Value.string)
)
|> Form.field "dob"
|> Form.field2 "dob"
(Field.date
{ invalid = \_ -> "Invalid date"
}
@ -163,7 +169,7 @@ form =
--|> Field.withMin (Date.fromCalendarDate 1900 Time.Jan 1 |> Form.Value.date)
--|> Field.withMax (Date.fromCalendarDate 2022 Time.Jan 1 |> Form.Value.date)
)
|> Form.field "checkbox" Field.checkbox
|> Form.field2 "checkbox" Field.checkbox
route : StatelessRoute RouteParams Data ActionData
@ -190,7 +196,8 @@ data routeParams =
action : RouteParams -> Parser (DataSource (Server.Response.Response ActionData ErrorPage))
action routeParams =
Request.formData [ form ]
--Request.formData [ form ]
Debug.todo ""
|> Request.map
(\userResultData ->
userResultData
@ -268,6 +275,7 @@ view maybeUrl sharedModel app =
[]
[ Html.text <| "Edit profile " ++ user.first ++ " " ++ user.last ]
, form
|> Debug.todo ""
|> Form.toDynamicTransition "test1"
|> Form.renderHtml
[ Attr.style "display" "flex"

View File

@ -12,7 +12,13 @@ module Form exposing
, runOneOfServerSideWithServerValidations
, AppContext
, FieldDefinition(..)
-- subGroup
, FormNew(..)
, HtmlFormNew
-- subGroup
, errorsForField2
, field2
, init2
)
{-|
@ -139,6 +145,19 @@ init fn viewFn =
(\_ -> [])
{-| -}
init2 : parsedAndView -> FormNew String parsedAndView data
init2 parsedAndView =
FormNew []
(\_ _ ->
{ result = Dict.empty
, parsedAndView = parsedAndView
, serverValidations = DataSource.succeed []
}
)
(\_ -> [])
{-| -}
dynamic :
(decider -> Form error (Validation error parsed named) data (Context error data -> subView))
@ -395,6 +414,16 @@ field name (Field fieldParser kind) (Form definitions parseFn toInitialValues) =
)
{-| -}
field2 :
String
-> Field error parsed data kind constraints
-> FormNew error (Validation error parsed named -> parsedAndView) data
-> FormNew error parsedAndView data
field2 name (Field fieldParser kind) (FormNew definitions parseFn toInitialValues) =
Debug.todo ""
{-| -}
hiddenField :
String
@ -568,6 +597,14 @@ errorsForField viewField (Errors errorsDict) =
|> Maybe.withDefault []
{-| -}
errorsForField2 : Validation error parsed { field : kind } -> Errors error -> List error
errorsForField2 field_ (Errors errorsDict) =
errorsDict
|> Dict.get (Validation.fieldName field_)
|> Maybe.withDefault []
{-| -}
type alias FieldErrors error =
Dict String (List error)
@ -1240,6 +1277,16 @@ type alias HtmlForm error parsed data msg =
(Context error data -> List (Html (Pages.Msg.Msg msg)))
{-| -}
type alias HtmlFormNew error parsed data msg =
FormNew
error
{ combine : Validation error parsed Never
, view : Context error parsed -> List (Html (Pages.Msg.Msg msg))
}
data
{-| -}
type alias HtmlSubForm error parsed data msg =
Form
@ -1277,6 +1324,21 @@ type Form error parsed data view
(data -> List ( String, String ))
{-| -}
type FormNew error parsedAndView data
= FormNew
(List ( String, FieldDefinition ))
(Maybe data
-> Form.FormState
->
{ result : Dict String (List error)
, parsedAndView : parsedAndView
, serverValidations : DataSource (List ( String, List error ))
}
)
(data -> List ( String, String ))
type alias RenderOptions =
{ submitStrategy : SubmitStrategy
, method : Method

View File

@ -1,6 +1,7 @@
module Form.FieldView exposing
( Input(..), InputType(..), Options(..), input, inputTypeToString, radio, select, toHtmlProperties
, radioStyled, inputStyled
, input2
)
{-|
@ -19,6 +20,7 @@ import Html.Attributes as Attr
import Html.Styled
import Html.Styled.Attributes as StyledAttr
import Json.Encode as Encode
import Pages.Internal.Form exposing (Validation)
{-| -}
@ -95,6 +97,16 @@ type Options a
= Options (String -> Maybe a) (List String)
{-| -}
input2 :
List (Html.Attribute msg)
-> Validation error parsed { field : Input }
-> Html msg
input2 attrs rawField =
-- TODO include `{ value : Maybe String, , kind : ( Input, List ( String, Encode.Value ) ) }` in Validation
Html.text "TODO"
{-| -}
input :
List (Html.Attribute msg)

View File

@ -1,6 +1,7 @@
module Form.Validation exposing
( Validation, andMap, andThen, fail, fromMaybe, fromResult, map, map2, parseWithError, succeed, withError, withErrorIf, withFallback
, value
, fieldName
)
{-|
@ -20,6 +21,12 @@ type alias Validation error parsed named =
Pages.Internal.Form.Validation error parsed named
fieldName : Validation error parsed { field : kind } -> String
fieldName (Validation name ( maybeParsed, errors )) =
name
|> Maybe.withDefault ""
{-| -}
succeed : parsed -> Validation error parsed Never
succeed parsed =