From fc153e7f481bef43fedbadae4082209c04135b05 Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Wed, 20 Jul 2022 11:36:50 +0200 Subject: [PATCH] Remove some hardcoding. --- examples/pokedex/app/Route/Form.elm | 5 +- src/Form.elm | 148 +++++++++++++++++++++++++++- 2 files changed, 149 insertions(+), 4 deletions(-) diff --git a/examples/pokedex/app/Route/Form.elm b/examples/pokedex/app/Route/Form.elm index 44c3ae14..47b77326 100644 --- a/examples/pokedex/app/Route/Form.elm +++ b/examples/pokedex/app/Route/Form.elm @@ -197,7 +197,7 @@ data routeParams = action : RouteParams -> Parser (DataSource (Server.Response.Response ActionData ErrorPage)) action routeParams = --Request.formData [ form ] - Debug.todo "" + Request.skip "todo" |> Request.map (\userResultData -> userResultData @@ -275,8 +275,7 @@ view maybeUrl sharedModel app = [] [ Html.text <| "Edit profile " ++ user.first ++ " " ++ user.last ] , form - |> Debug.todo "" - |> Form.toDynamicTransition "test1" + |> Form.toDynamicTransitionNew "test1" |> Form.renderHtml [ Attr.style "display" "flex" , Attr.style "flex-direction" "column" diff --git a/src/Form.elm b/src/Form.elm index 3f076b6e..017ff56f 100644 --- a/src/Form.elm +++ b/src/Form.elm @@ -18,6 +18,7 @@ module Form exposing , errorsForField2 , field2 , init2 + , toDynamicTransitionNew ) @@ -421,7 +422,82 @@ field2 : -> FormNew error (Validation error parsed named -> parsedAndView) data -> FormNew error parsedAndView data field2 name (Field fieldParser kind) (FormNew definitions parseFn toInitialValues) = - Debug.todo "" + FormNew + (( name, RegularField ) + :: definitions + ) + (\maybeData formState -> + let + ( maybeParsed, errors ) = + fieldParser.decode rawFieldValue + + ( rawFieldValue, fieldStatus ) = + case formState.fields |> Dict.get name of + Just info -> + ( Just info.value, info.status ) + + Nothing -> + ( Maybe.map2 (|>) maybeData fieldParser.initialValue, Form.NotVisited ) + + parsedField : ParsedField error parsed + parsedField = + Pages.Internal.Form.Validation (Just name) ( maybeParsed, Dict.empty ) + + rawField : ViewField error parsed kind + rawField = + { name = name + , value = rawFieldValue + , status = fieldStatus + , kind = ( kind, fieldParser.properties ) + , parsed = maybeParsed + , errors = errors + } + + myFn : + { result : Dict String (List error) + , parsedAndView : Validation error parsed named -> parsedAndView + , serverValidations : DataSource (List ( String, List error )) + } + -> + { result : Dict String (List error) + , parsedAndView : parsedAndView + , serverValidations : DataSource (List ( String, List error )) + } + myFn soFar = + let + serverValidationsForField : DataSource ( String, List error ) + serverValidationsForField = + fieldParser.serverValidation rawFieldValue + |> DataSource.map (Tuple.pair name) + + validationField : Validation error parsed named + validationField = + Pages.Internal.Form.Validation (Just name) ( maybeParsed, Dict.empty ) + in + { result = + soFar.result + |> addErrorsInternal name errors + , parsedAndView = + soFar.parsedAndView validationField + , serverValidations = + DataSource.map2 (::) + serverValidationsForField + soFar.serverValidations + } + in + formState + |> parseFn maybeData + |> myFn + ) + (\data -> + case fieldParser.initialValue of + Just toInitialValue -> + ( name, toInitialValue data ) + :: toInitialValues data + + Nothing -> + toInitialValues data + ) {-| -} @@ -979,6 +1055,76 @@ toDynamicTransition name (Form a b c) = FinalForm options a b c +{-| -} +toDynamicTransitionNew : + String + -> + FormNew + error + { combine : Validation error parsed field + , view : Context error data -> List (Html (Pages.Msg.Msg msg)) + } + data + -> + FinalForm + error + (Validation error parsed field) + data + (Context error data -> List (Html (Pages.Msg.Msg msg))) +toDynamicTransitionNew name (FormNew a b c) = + let + options = + { submitStrategy = TransitionStrategy + , method = Post + , name = Just name + } + + transformB : + (Maybe data + -> Form.FormState + -> + { result : Dict String (List error) + , parsedAndView : + { combine : Validation error parsed field + , view : Context error data -> List (Html (Pages.Msg.Msg msg)) + } + , serverValidations : DataSource (List ( String, List error )) + } + ) + -> + (Maybe data + -> Form.FormState + -> + { result : + ( Validation error parsed field + , Dict String (List error) + ) + , view : Context error data -> List (Html (Pages.Msg.Msg msg)) + , serverValidations : DataSource (List ( String, List error )) + } + ) + transformB rawB = + \maybeData formState -> + let + foo : + { result : Dict String (List error) + , parsedAndView : + { combine : Validation error parsed field + , view : Context error data -> List (Html (Pages.Msg.Msg msg)) + } + , serverValidations : DataSource (List ( String, List error )) + } + foo = + rawB maybeData formState + in + { result = ( foo.parsedAndView.combine, foo.result ) + , view = foo.parsedAndView.view + , serverValidations = foo.serverValidations + } + in + FinalForm options a (transformB b) c + + {-| -} withGetMethod : FinalForm error parsed data view -> FinalForm error parsed data view withGetMethod (FinalForm options a b c) =