mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-11-22 21:51:25 +03:00
Add helpers for JSON Encoder for fields in Scaffold module.
This commit is contained in:
parent
1e66901d93
commit
15edcd502e
@ -4,7 +4,8 @@
|
||||
"packages": {
|
||||
"elm/core": "1.0.5",
|
||||
"elm/html": "1.0.0",
|
||||
"rtfeldman/elm-css": "18.0.0"
|
||||
"rtfeldman/elm-css": "18.0.0",
|
||||
"elm/json": "1.1.3"
|
||||
},
|
||||
"local": [
|
||||
"../../src/",
|
||||
|
@ -11,12 +11,12 @@ import Elm.Declare
|
||||
import Elm.Let
|
||||
import Elm.Op
|
||||
import Gen.BackendTask
|
||||
import Gen.Debug
|
||||
import Gen.Effect as Effect
|
||||
import Gen.Form as Form
|
||||
import Gen.Form.FieldView as FieldView
|
||||
import Gen.Html as Html
|
||||
import Gen.Html.Attributes as Attr
|
||||
import Gen.Json.Encode
|
||||
import Gen.List
|
||||
import Gen.Pages.Script
|
||||
import Gen.Platform.Sub
|
||||
@ -138,8 +138,8 @@ createFile { moduleName, fields } =
|
||||
{ err =
|
||||
( "error"
|
||||
, \error ->
|
||||
Gen.Debug.toString error
|
||||
|> Gen.Pages.Script.call_.log
|
||||
"Form validations did not succeed!"
|
||||
|> Gen.Pages.Script.log
|
||||
|> Gen.BackendTask.call_.map
|
||||
(Elm.fn ( "_", Nothing )
|
||||
(\_ ->
|
||||
@ -152,9 +152,10 @@ createFile { moduleName, fields } =
|
||||
)
|
||||
)
|
||||
, ok =
|
||||
( "okForm"
|
||||
, \okForm ->
|
||||
Gen.Debug.toString okForm
|
||||
( "validatedForm"
|
||||
, \validatedForm ->
|
||||
Scaffold.Form.recordEncoder validatedForm fields
|
||||
|> Gen.Json.Encode.encode 2
|
||||
|> Gen.Pages.Script.call_.log
|
||||
|> Gen.BackendTask.call_.map
|
||||
(Elm.fn ( "_", Nothing )
|
||||
|
@ -1,6 +1,7 @@
|
||||
module Scaffold.Form exposing
|
||||
( Kind(..), provide, restArgsParser
|
||||
, Context
|
||||
, fieldEncoder, recordEncoder
|
||||
)
|
||||
|
||||
{-|
|
||||
@ -9,6 +10,8 @@ module Scaffold.Form exposing
|
||||
|
||||
@docs Context
|
||||
|
||||
@docs fieldEncoder, recordEncoder
|
||||
|
||||
-}
|
||||
|
||||
import Cli.Option
|
||||
@ -482,3 +485,76 @@ initCombined initCombinedArg initCombinedArg0 =
|
||||
}
|
||||
)
|
||||
[ initCombinedArg, initCombinedArg0 ]
|
||||
|
||||
|
||||
{-| Generate a JSON Encoder for the form fields. This can be helpful for sending the validated form data through a
|
||||
BackendTask.Custom or to an external API from your scaffolded Route Module code.
|
||||
-}
|
||||
recordEncoder : Elm.Expression -> List ( String, Kind ) -> Elm.Expression
|
||||
recordEncoder record fields =
|
||||
fields
|
||||
|> List.map
|
||||
(\( field, kind ) ->
|
||||
Elm.tuple
|
||||
(Elm.string field)
|
||||
(fieldEncoder record field kind)
|
||||
)
|
||||
|> Elm.list
|
||||
|> List.singleton
|
||||
|> Elm.apply
|
||||
(Elm.value
|
||||
{ importFrom = [ "Json", "Encode" ]
|
||||
, name = "object"
|
||||
, annotation =
|
||||
Just
|
||||
(Type.function
|
||||
[ Type.list
|
||||
(Type.tuple
|
||||
Type.string
|
||||
(Type.namedWith [ "Json", "Encode" ] "Value" [])
|
||||
)
|
||||
]
|
||||
(Type.namedWith [ "Json", "Encode" ] "Value" [])
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
{-| -}
|
||||
fieldEncoder : Elm.Expression -> String -> Kind -> Elm.Expression
|
||||
fieldEncoder record name kind =
|
||||
Elm.apply
|
||||
(case kind of
|
||||
FieldInt ->
|
||||
encoder "int"
|
||||
|
||||
FieldText ->
|
||||
encoder "string"
|
||||
|
||||
FieldTextarea ->
|
||||
encoder "string"
|
||||
|
||||
FieldFloat ->
|
||||
encoder "float"
|
||||
|
||||
FieldTime ->
|
||||
-- TODO fix time encoder
|
||||
encoder "int"
|
||||
|
||||
FieldDate ->
|
||||
-- TODO fix date encoder
|
||||
encoder "int"
|
||||
|
||||
FieldCheckbox ->
|
||||
encoder "bool"
|
||||
)
|
||||
[ Elm.get name record ]
|
||||
|
||||
|
||||
encoder : String -> Elm.Expression
|
||||
encoder name =
|
||||
Elm.value
|
||||
{ importFrom = [ "Json", "Encode" ]
|
||||
, name = name
|
||||
, annotation = Nothing
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user