mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-11-27 21:29:55 +03:00
Migrate another page to new Form type.
This commit is contained in:
parent
0529b0a9d5
commit
fbe4b28a12
@ -124,44 +124,47 @@ type Action
|
||||
| SetQuantity Uuid Int
|
||||
|
||||
|
||||
signoutForm : Form.HtmlForm String Action input Msg
|
||||
signoutForm : Form.HtmlFormNew String Action input Msg
|
||||
signoutForm =
|
||||
Form.init
|
||||
(Validation.succeed Signout)
|
||||
(\formState ->
|
||||
[ Html.button [] [ Html.text "Sign out" ]
|
||||
]
|
||||
)
|
||||
|> Form.hiddenKind ( "kind", "signout" ) "Expected signout"
|
||||
|
||||
|
||||
setQuantityForm : Form.HtmlForm String Action ( Int, QuantityChange, Smoothie ) Msg
|
||||
setQuantityForm =
|
||||
Form.init
|
||||
(\uuid quantity ->
|
||||
Validation.succeed SetQuantity
|
||||
|> Validation.andMap (uuid |> Validation.map Uuid)
|
||||
|> Validation.andMap quantity
|
||||
)
|
||||
(\formState ->
|
||||
[ Html.button []
|
||||
[ Html.text <|
|
||||
case formState.data of
|
||||
( _, Decrement, _ ) ->
|
||||
"-"
|
||||
|
||||
( _, Increment, _ ) ->
|
||||
"+"
|
||||
Form.init2
|
||||
{ combine = Validation.succeed Signout
|
||||
, view =
|
||||
\formState ->
|
||||
[ Html.button [] [ Html.text "Sign out" ]
|
||||
]
|
||||
]
|
||||
}
|
||||
|> Form.hiddenKind2 ( "kind", "signout" ) "Expected signout"
|
||||
|
||||
|
||||
setQuantityForm : Form.HtmlFormNew String Action ( Int, QuantityChange, Smoothie ) Msg
|
||||
setQuantityForm =
|
||||
Form.init2
|
||||
(\uuid quantity ->
|
||||
{ combine =
|
||||
Validation.succeed SetQuantity
|
||||
|> Validation.andMap (uuid |> Validation.map Uuid)
|
||||
|> Validation.andMap quantity
|
||||
, view =
|
||||
\formState ->
|
||||
[ Html.button []
|
||||
[ Html.text <|
|
||||
case formState.data of
|
||||
( _, Decrement, _ ) ->
|
||||
"-"
|
||||
|
||||
( _, Increment, _ ) ->
|
||||
"+"
|
||||
]
|
||||
]
|
||||
}
|
||||
)
|
||||
|> Form.hiddenKind ( "kind", "setQuantity" ) "Expected setQuantity"
|
||||
|> Form.hiddenField "itemId"
|
||||
|> Form.hiddenKind2 ( "kind", "setQuantity" ) "Expected setQuantity"
|
||||
|> Form.hiddenField2 "itemId"
|
||||
(Field.text
|
||||
|> Field.required "Required"
|
||||
|> Field.withInitialValue (\( _, _, item ) -> Form.Value.string (uuidToString item.id))
|
||||
)
|
||||
|> Form.hiddenField "quantity"
|
||||
|> Form.hiddenField2 "quantity"
|
||||
(Field.int { invalid = \_ -> "Expected int" }
|
||||
|> Field.required "Required"
|
||||
|> Field.withInitialValue
|
||||
@ -182,14 +185,14 @@ toQuantity quantityChange =
|
||||
-1
|
||||
|
||||
|
||||
oneOfParsers : List (Form.HtmlForm String Action ( Int, QuantityChange, Smoothie ) Msg)
|
||||
oneOfParsers : List (Form.HtmlFormNew String Action ( Int, QuantityChange, Smoothie ) Msg)
|
||||
oneOfParsers =
|
||||
[ signoutForm, setQuantityForm ]
|
||||
|
||||
|
||||
action : RouteParams -> Request.Parser (DataSource (Response ActionData ErrorPage))
|
||||
action routeParams =
|
||||
Request.formDataWithoutServerValidation oneOfParsers
|
||||
Request.formDataWithoutServerValidation2 oneOfParsers
|
||||
|> MySession.expectSessionDataOrRedirect (Session.get "userId" >> Maybe.map Uuid)
|
||||
(\userId parsedAction session ->
|
||||
case parsedAction of
|
||||
@ -228,7 +231,7 @@ view maybeUrl sharedModel model app =
|
||||
app.fetchers
|
||||
|> List.filterMap
|
||||
(\pending ->
|
||||
case Form.runOneOfServerSide pending.payload.fields oneOfParsers of
|
||||
case Form.runOneOfServerSide2 pending.payload.fields oneOfParsers of
|
||||
( Just (SetQuantity itemId addAmount), _ ) ->
|
||||
Just ( uuidToString itemId, addAmount )
|
||||
|
||||
@ -268,7 +271,7 @@ view maybeUrl sharedModel model app =
|
||||
, Html.p []
|
||||
[ Html.text <| "Welcome " ++ app.data.user.name ++ "!"
|
||||
, signoutForm
|
||||
|> Form.toDynamicFetcher "signout"
|
||||
|> Form.toDynamicFetcherNew "signout"
|
||||
|> Form.renderHtml [] Nothing app ()
|
||||
]
|
||||
, cartView totals
|
||||
@ -321,11 +324,11 @@ productView app cart item =
|
||||
[]
|
||||
[ setQuantityForm
|
||||
-- TODO should this be toStaticFetcher (don't need the formId here because there is no client-side state, only hidden form fields
|
||||
|> Form.toDynamicFetcher "increment-quantity"
|
||||
|> Form.toDynamicFetcherNew "increment-quantity"
|
||||
|> Form.renderHtml [] Nothing app ( quantityInCart, Decrement, item )
|
||||
, Html.p [] [ quantityInCart |> String.fromInt |> Html.text ]
|
||||
, setQuantityForm
|
||||
|> Form.toDynamicFetcher "decrement-quantity"
|
||||
|> Form.toDynamicFetcherNew "decrement-quantity"
|
||||
|> Form.renderHtml [] Nothing app ( quantityInCart, Increment, item )
|
||||
]
|
||||
, Html.div []
|
||||
|
143
src/Form.elm
143
src/Form.elm
@ -19,11 +19,13 @@ module Form exposing
|
||||
, errorsForField2
|
||||
, field2
|
||||
, hiddenField2
|
||||
, hiddenKind2
|
||||
, init2
|
||||
, runOneOfServerSide2
|
||||
, runOneOfServerSideWithServerValidations2
|
||||
, runServerSide3
|
||||
, runServerSide4
|
||||
, toDynamicFetcherNew
|
||||
, toDynamicTransitionNew
|
||||
|
||||
)
|
||||
@ -681,6 +683,77 @@ hiddenField name (Field fieldParser _) (Form definitions parseFn toInitialValues
|
||||
)
|
||||
|
||||
|
||||
{-| -}
|
||||
hiddenKind2 :
|
||||
( String, String )
|
||||
-> error
|
||||
-> FormNew error parsedAndView data
|
||||
-> FormNew error parsedAndView data
|
||||
hiddenKind2 ( name, value ) error_ (FormNew definitions parseFn toInitialValues) =
|
||||
let
|
||||
(Field fieldParser _) =
|
||||
Field.exactValue value error_
|
||||
in
|
||||
FormNew
|
||||
(( name, HiddenField )
|
||||
:: definitions
|
||||
)
|
||||
(\maybeData formState ->
|
||||
let
|
||||
( _, errors ) =
|
||||
fieldParser.decode rawFieldValue
|
||||
|
||||
rawFieldValue : Maybe String
|
||||
rawFieldValue =
|
||||
case formState.fields |> Dict.get name of
|
||||
Just info ->
|
||||
Just info.value
|
||||
|
||||
Nothing ->
|
||||
Maybe.map2 (|>) maybeData fieldParser.initialValue
|
||||
|
||||
myFn :
|
||||
{ result : Dict String (List error)
|
||||
, parsedAndView : 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)
|
||||
in
|
||||
{ result =
|
||||
soFar.result
|
||||
|> addErrorsInternal name errors
|
||||
, parsedAndView = soFar.parsedAndView
|
||||
, 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
|
||||
)
|
||||
|
||||
|
||||
{-| -}
|
||||
hiddenKind :
|
||||
( String, String )
|
||||
@ -1277,6 +1350,76 @@ toDynamicFetcher name (Form a b c) =
|
||||
FinalForm options a b c
|
||||
|
||||
|
||||
{-| -}
|
||||
toDynamicFetcherNew :
|
||||
String
|
||||
->
|
||||
FormNew
|
||||
error
|
||||
{ combine : Validation error parsed field
|
||||
, view : Context error data -> view
|
||||
}
|
||||
data
|
||||
->
|
||||
FinalForm
|
||||
error
|
||||
(Validation error parsed field)
|
||||
data
|
||||
(Context error data -> view)
|
||||
toDynamicFetcherNew name (FormNew a b c) =
|
||||
let
|
||||
options =
|
||||
{ submitStrategy = FetcherStrategy
|
||||
, 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 -> view
|
||||
}
|
||||
, serverValidations : DataSource (List ( String, List error ))
|
||||
}
|
||||
)
|
||||
->
|
||||
(Maybe data
|
||||
-> Form.FormState
|
||||
->
|
||||
{ result :
|
||||
( Validation error parsed field
|
||||
, Dict String (List error)
|
||||
)
|
||||
, view : Context error data -> view
|
||||
, 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 -> view
|
||||
}
|
||||
, 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
|
||||
|
||||
|
||||
{-| -}
|
||||
toDynamicTransition : String -> Form error parsed data view -> FinalForm error parsed data view
|
||||
toDynamicTransition name (Form a b c) =
|
||||
|
Loading…
Reference in New Issue
Block a user