mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-11-30 03:11:30 +03:00
Move some helpers outside of form helper to make it more customizable.
This commit is contained in:
parent
5aeade0daf
commit
df7f275566
@ -65,12 +65,13 @@ program =
|
||||
createFile : List String -> List ( String, AddFormHelp.Kind ) -> Elm.File
|
||||
createFile moduleName fields =
|
||||
let
|
||||
formHelp :
|
||||
{ formHandlers : { declaration : Elm.Declaration, value : Elm.Expression }
|
||||
, renderForm : Elm.Expression -> Elm.Expression
|
||||
, declarations : List Elm.Declaration
|
||||
}
|
||||
formHelp =
|
||||
formHelpers :
|
||||
Maybe
|
||||
{ formHandlers : { declaration : Elm.Declaration, value : Elm.Expression }
|
||||
, renderForm : Elm.Expression -> Elm.Expression
|
||||
, declarations : List Elm.Declaration
|
||||
}
|
||||
formHelpers =
|
||||
AddFormHelp.provide
|
||||
{ fields = fields
|
||||
, view =
|
||||
@ -119,31 +120,49 @@ createFile moduleName fields =
|
||||
, action =
|
||||
( Alias
|
||||
(Elm.Annotation.record
|
||||
[ ( "errors", Elm.Annotation.namedWith [ "Form" ] "Response" [ Elm.Annotation.string ] )
|
||||
]
|
||||
(case formHelpers of
|
||||
Just _ ->
|
||||
[ ( "errors", Elm.Annotation.namedWith [ "Form" ] "Response" [ Elm.Annotation.string ] )
|
||||
]
|
||||
|
||||
Nothing ->
|
||||
[]
|
||||
)
|
||||
)
|
||||
, \routeParams ->
|
||||
Gen.Server.Request.formData formHelp.formHandlers.value
|
||||
|> Gen.Server.Request.call_.map
|
||||
(Elm.fn ( "formData", Nothing )
|
||||
(\formData ->
|
||||
Elm.Case.tuple formData
|
||||
"response"
|
||||
"parsedForm"
|
||||
(\response parsedForm ->
|
||||
Gen.Debug.toString parsedForm
|
||||
|> Gen.Pages.Script.call_.log
|
||||
|> Gen.BackendTask.call_.map
|
||||
(Elm.fn ( "_", Nothing )
|
||||
(\_ ->
|
||||
Gen.Server.Response.render
|
||||
(Elm.record
|
||||
[ ( "errors", response )
|
||||
]
|
||||
formHelpers
|
||||
|> Maybe.map
|
||||
(\justFormHelp ->
|
||||
Gen.Server.Request.formData justFormHelp.formHandlers.value
|
||||
|> Gen.Server.Request.call_.map
|
||||
(Elm.fn ( "formData", Nothing )
|
||||
(\formData ->
|
||||
Elm.Case.tuple formData
|
||||
"response"
|
||||
"parsedForm"
|
||||
(\response parsedForm ->
|
||||
Gen.Debug.toString parsedForm
|
||||
|> Gen.Pages.Script.call_.log
|
||||
|> Gen.BackendTask.call_.map
|
||||
(Elm.fn ( "_", Nothing )
|
||||
(\_ ->
|
||||
Gen.Server.Response.render
|
||||
(Elm.record
|
||||
[ ( "errors", response )
|
||||
]
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|> Maybe.withDefault
|
||||
(Gen.Server.Request.succeed
|
||||
(Gen.BackendTask.succeed
|
||||
(Gen.Server.Response.render
|
||||
(Elm.record [])
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
@ -159,10 +178,12 @@ createFile moduleName fields =
|
||||
)
|
||||
, head = \app -> Elm.list []
|
||||
}
|
||||
|> Pages.Generate.addDeclarations formHelp.declarations
|
||||
|> Pages.Generate.addDeclarations
|
||||
[ errorsView.declaration
|
||||
]
|
||||
(formHelpers
|
||||
|> Maybe.map .declarations
|
||||
|> Maybe.map ((::) errorsView.declaration)
|
||||
|> Maybe.withDefault []
|
||||
)
|
||||
|> Pages.Generate.buildWithLocalState
|
||||
{ view =
|
||||
\{ maybeUrl, sharedModel, model, app } ->
|
||||
@ -170,9 +191,16 @@ createFile moduleName fields =
|
||||
{ title = moduleName |> String.join "." |> Elm.string
|
||||
, body =
|
||||
Elm.list
|
||||
[ Html.h2 [] [ Html.text "Form" ]
|
||||
, formHelp.renderForm app -- TODO customize argument with `(Elm.get "errors" >> Elm.just)` and `Elm.unit`?
|
||||
]
|
||||
(case formHelpers of
|
||||
Just justFormHelp ->
|
||||
[ Html.h2 [] [ Html.text "Form" ]
|
||||
, justFormHelp.renderForm app -- TODO customize argument with `(Elm.get "errors" >> Elm.just)` and `Elm.unit`?
|
||||
]
|
||||
|
||||
Nothing ->
|
||||
[ Html.h2 [] [ Html.text "New Page" ]
|
||||
]
|
||||
)
|
||||
}
|
||||
, update =
|
||||
\{ pageUrl, sharedModel, app, msg, model } ->
|
||||
|
@ -165,74 +165,80 @@ provide :
|
||||
-> Elm.Expression
|
||||
}
|
||||
->
|
||||
{ formHandlers : { declaration : Elm.Declaration, value : Elm.Expression }
|
||||
, renderForm : Elm.Expression -> Elm.Expression
|
||||
, declarations : List Elm.Declaration
|
||||
}
|
||||
Maybe
|
||||
{ formHandlers : { declaration : Elm.Declaration, value : Elm.Expression }
|
||||
, renderForm : Elm.Expression -> Elm.Expression
|
||||
, declarations : List Elm.Declaration
|
||||
}
|
||||
provide { fields, view } =
|
||||
let
|
||||
form : { declaration : Elm.Declaration, call : List Elm.Expression -> Elm.Expression, callFrom : List String -> List Elm.Expression -> Elm.Expression }
|
||||
form =
|
||||
formWithFields fields view
|
||||
in
|
||||
{ formHandlers =
|
||||
{ declaration =
|
||||
Elm.declaration "formHandlers"
|
||||
(Gen.Form.call_.initCombined (Elm.val "Action") (form.call [])
|
||||
|> Elm.withType
|
||||
(Elm.Annotation.namedWith [ "Form" ]
|
||||
"ServerForms"
|
||||
[ Elm.Annotation.string
|
||||
, Elm.Annotation.named [] "Action"
|
||||
]
|
||||
if List.isEmpty fields then
|
||||
Nothing
|
||||
|
||||
else
|
||||
Just
|
||||
{ formHandlers =
|
||||
{ declaration =
|
||||
Elm.declaration "formHandlers"
|
||||
(Gen.Form.call_.initCombined (Elm.val "Action") (form.call [])
|
||||
|> Elm.withType
|
||||
(Elm.Annotation.namedWith [ "Form" ]
|
||||
"ServerForms"
|
||||
[ Elm.Annotation.string
|
||||
, Elm.Annotation.named [] "Action"
|
||||
]
|
||||
)
|
||||
)
|
||||
)
|
||||
, value = Elm.val "formHandlers"
|
||||
}
|
||||
, renderForm =
|
||||
\app ->
|
||||
form.call []
|
||||
|> Gen.Form.toDynamicTransition "form"
|
||||
|> Gen.Form.renderHtml [] (Elm.get "errors" >> Elm.just) app Elm.unit
|
||||
, declarations =
|
||||
[ formWithFields fields view |> .declaration
|
||||
, Elm.customType "Action"
|
||||
[ Elm.variantWith "Action" [ Elm.Annotation.named [] "ParsedForm" ]
|
||||
]
|
||||
, value = Elm.val "formHandlers"
|
||||
}
|
||||
, renderForm =
|
||||
\app ->
|
||||
form.call []
|
||||
|> Gen.Form.toDynamicTransition "form"
|
||||
|> Gen.Form.renderHtml [] (Elm.get "errors" >> Elm.just) app Elm.unit
|
||||
, declarations =
|
||||
[ formWithFields fields view |> .declaration
|
||||
, Elm.customType "Action"
|
||||
[ Elm.variantWith "Action" [ Elm.Annotation.named [] "ParsedForm" ]
|
||||
]
|
||||
|
||||
-- TODO customize formHandlers name?
|
||||
, Elm.declaration "formHandlers" (Gen.Form.call_.initCombined (Elm.val "Action") (form.call []))
|
||||
-- TODO customize formHandlers name?
|
||||
, Elm.declaration "formHandlers" (Gen.Form.call_.initCombined (Elm.val "Action") (form.call []))
|
||||
|
||||
-- TODO customize ParsedForm name?
|
||||
, Elm.alias "ParsedForm"
|
||||
(fields
|
||||
|> List.map
|
||||
(\( fieldName, kind ) ->
|
||||
( fieldName
|
||||
, case kind of
|
||||
FieldString ->
|
||||
Elm.Annotation.string
|
||||
-- TODO customize ParsedForm name?
|
||||
, Elm.alias "ParsedForm"
|
||||
(fields
|
||||
|> List.map
|
||||
(\( fieldName, kind ) ->
|
||||
( fieldName
|
||||
, case kind of
|
||||
FieldString ->
|
||||
Elm.Annotation.string
|
||||
|
||||
FieldInt ->
|
||||
Elm.Annotation.int
|
||||
FieldInt ->
|
||||
Elm.Annotation.int
|
||||
|
||||
FieldText ->
|
||||
Elm.Annotation.string
|
||||
FieldText ->
|
||||
Elm.Annotation.string
|
||||
|
||||
FieldFloat ->
|
||||
Elm.Annotation.float
|
||||
FieldFloat ->
|
||||
Elm.Annotation.float
|
||||
|
||||
FieldTime ->
|
||||
Elm.Annotation.named [ "Form", "Field" ] "TimeOfDay"
|
||||
FieldTime ->
|
||||
Elm.Annotation.named [ "Form", "Field" ] "TimeOfDay"
|
||||
|
||||
FieldDate ->
|
||||
Elm.Annotation.named [ "Date" ] "Date"
|
||||
FieldDate ->
|
||||
Elm.Annotation.named [ "Date" ] "Date"
|
||||
|
||||
FieldBool ->
|
||||
Elm.Annotation.bool
|
||||
)
|
||||
FieldBool ->
|
||||
Elm.Annotation.bool
|
||||
)
|
||||
)
|
||||
|> Elm.Annotation.record
|
||||
)
|
||||
|> Elm.Annotation.record
|
||||
)
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user