1module AddRoute exposing (run)
4import Cli.Option as Option
5import Cli.OptionsParser as OptionsParser
6import Cli.Program as Program
8import Elm.Annotation as Type
14import Gen.Effect as Effect
16import Gen.Form as Form
17import Gen.Form.FieldView as FieldView
18import Gen.Html as Html
19import Gen.Html.Attributes as Attr
23import Gen.Pages.Form as PagesForm
24import Gen.Pages.Script
25import Gen.Server.Request as Request
26import Gen.Server.Response as Response
28import Pages.Script as Script exposing (Script)
30import Scaffold.Route exposing (Type(..))
33type alias CliOptions =
34 { moduleName : List String
35 , fields : List ( String, Scaffold.Form.Kind )
41 Script.withCliOptions program
46 |> BackendTask.allowFatal
50program : Program.Config CliOptions
54 (OptionsParser.build CliOptions
55 |> OptionsParser.with (Option.requiredPositionalArg "module" |> Scaffold.Route.moduleNameCliArg)
56 |> OptionsParser.withRestArgs Scaffold.Form.restArgsParser
60createFile : CliOptions -> { path : String, body : String }
61createFile { moduleName, fields } =
65 { formHandlers : Elm.Expression
66 , form : Elm.Expression
67 , declarations : List Elm.Declaration
74 \{ formState, params } ->
80 (\{ name, kind, param } ->
81 fieldView (Elm.string name) param
84 ++ [ Elm.ifThen formState.submitting
88 [ Html.text "Submitting..."
98 |> Elm.Let.fn2 "fieldView"
99 ( "label", Type.string |> Just )
104 [ Html.call_.text (Elm.Op.append label (Elm.string " "))
105 , field |> FieldView.input []
106 , errorsView.call formState.errors field
110 |> Elm.Let.toExpression
113 Scaffold.Route.serverRender
114 { moduleName = moduleName
120 [ ( "errors", Type.namedWith [ "Form" ] "ServerResponse" [ Type.string ] )
127 , \routeParams request ->
131 Request.formData justFormHelp.formHandlers request
132 |> Gen.Maybe.call_.map
133 (Elm.fn ( "formData", Nothing )
135 Elm.Case.tuple formData
138 (\response parsedForm ->
139 Elm.Case.custom parsedForm
141 [ Elm.Case.branch1 "Form.Valid"
142 ( "validatedForm", Type.int )
144 Elm.Case.custom validatedForm
146 [ Elm.Case.branch1 "Action"
147 ( "parsed", Type.int )
149 Scaffold.Form.recordEncoder parsed fields
150 |> Gen.Json.Encode.encode 2
151 |> Gen.Pages.Script.call_.log
152 |> Gen.BackendTask.call_.map
153 (Elm.fn ( "_", Nothing )
157 [ ( "errors", response )
165 , Elm.Case.branch2 "Form.Invalid"
166 ( "parsed", Type.int )
167 ( "error", Type.int )
169 "Form validations did not succeed!"
170 |> Gen.Pages.Script.log
171 |> Gen.BackendTask.call_.map
172 (Elm.fn ( "_", Nothing )
176 [ ( "errors", response )
186 |> Gen.Maybe.withDefault
187 (Gen.BackendTask.fail
188 (Gen.FatalError.fromString "Expected form post")
192 (Gen.BackendTask.succeed
199 ( Alias (Type.record [])
200 , \routeParams request ->
201 Gen.BackendTask.succeed
206 , head = \app -> Elm.list []
208 |> Scaffold.Route.addDeclarations
210 |> Maybe.map .declarations
211 |> Maybe.map ((::) errorsView.declaration)
212 |> Maybe.withDefault []
214 |> Scaffold.Route.buildWithLocalState
216 \{ shared, model, app } ->
218 { title = moduleName |> String.join "." |> Elm.string
223 [ Html.h2 [] [ Html.text "Form" ]
225 |> PagesForm.call_.renderHtml
228 |> Form.withServerResponse
231 |> Gen.Maybe.map (Elm.get "errors")
238 [ Html.h2 [] [ Html.text "New Page" ]
243 \{ shared, app, msg, model } ->
245 (Type.named [] "Msg")
246 [ Elm.Case.branch0 "NoOp"
253 Elm.tuple (Elm.record []) Effect.none
255 \{ routeParams, path, shared, model } ->
258 Alias (Type.record [])
260 Custom [ Elm.variant "NoOp" ]
265 { declaration : Elm.Declaration
266 , call : Elm.Expression -> Elm.Expression -> Elm.Expression
267 , callFrom : List String -> Elm.Expression -> Elm.Expression -> Elm.Expression
268 , value : List String -> Elm.Expression
271 Elm.Declare.fn2 "errorsView"
272 ( "errors", Type.namedWith [ "Form" ] "Errors" [ Type.string ] |> Just )
274 , Type.namedWith [ "Form", "Validation" ]
284 (Gen.List.call_.isEmpty (Form.errorsForField field errors))
288 [ Html.call_.ul (Elm.list [])
290 (Elm.fn ( "error", Nothing )
293 [ Attr.style "color" "red"
295 [ Html.call_.text error
299 (Form.errorsForField field errors)
304 (Type.namedWith [ "Html" ]
309 [ Type.named [] "Msg" ]
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list