elm-pages 3.0.0-beta.4 → 3.0.0-beta.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/README.md +10 -1
  2. package/adapter/netlify.js +207 -0
  3. package/codegen/{elm-pages-codegen.js → elm-pages-codegen.cjs} +2678 -2725
  4. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-DeadCodeEliminateData.elmi +0 -0
  5. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-DeadCodeEliminateData.elmo +0 -0
  6. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-DeadCodeEliminateDataTest.elmo +0 -0
  7. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/d.dat +0 -0
  8. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/i.dat +0 -0
  9. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/o.dat +0 -0
  10. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm.json +1 -1
  11. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/Reporter.elm.js +1447 -342
  12. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/Runner.elm.js +17004 -13817
  13. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/node_runner.js +1 -1
  14. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/node_supervisor.js +4 -4
  15. package/generator/dead-code-review/elm.json +9 -7
  16. package/generator/dead-code-review/src/Pages/Review/DeadCodeEliminateData.elm +59 -10
  17. package/generator/dead-code-review/tests/Pages/Review/DeadCodeEliminateDataTest.elm +52 -36
  18. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Internal-RoutePattern.elmi +0 -0
  19. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Internal-RoutePattern.elmo +0 -0
  20. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-NoContractViolations.elmi +0 -0
  21. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-NoContractViolations.elmo +0 -0
  22. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/d.dat +0 -0
  23. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/i.dat +0 -0
  24. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/o.dat +0 -0
  25. package/generator/review/elm-stuff/tests-0.19.1/elm.json +1 -1
  26. package/generator/review/elm-stuff/tests-0.19.1/js/Reporter.elm.js +1447 -342
  27. package/generator/review/elm-stuff/tests-0.19.1/js/Runner.elm.js +25025 -21739
  28. package/generator/review/elm-stuff/tests-0.19.1/js/node_runner.js +1 -1
  29. package/generator/review/elm-stuff/tests-0.19.1/js/node_supervisor.js +4 -4
  30. package/generator/review/elm.json +10 -10
  31. package/generator/src/RouteBuilder.elm +115 -109
  32. package/generator/src/SharedTemplate.elm +3 -2
  33. package/generator/src/SiteConfig.elm +3 -2
  34. package/generator/src/basepath-middleware.js +3 -3
  35. package/generator/src/build.js +209 -92
  36. package/generator/src/cli.js +292 -88
  37. package/generator/src/codegen.js +29 -27
  38. package/generator/src/compatibility-key.js +3 -0
  39. package/generator/src/compile-elm.js +43 -26
  40. package/generator/src/config.js +39 -0
  41. package/generator/src/copy-dir.js +2 -2
  42. package/generator/src/dev-server.js +176 -138
  43. package/generator/src/dir-helpers.js +9 -26
  44. package/generator/src/elm-codegen.js +5 -4
  45. package/generator/src/elm-file-constants.js +2 -3
  46. package/generator/src/error-formatter.js +12 -11
  47. package/generator/src/file-helpers.js +3 -4
  48. package/generator/src/generate-template-module-connector.js +23 -23
  49. package/generator/src/init.js +9 -8
  50. package/generator/src/pre-render-html.js +39 -28
  51. package/generator/src/render-test.js +109 -0
  52. package/generator/src/render-worker.js +25 -28
  53. package/generator/src/render.js +321 -142
  54. package/generator/src/request-cache.js +265 -162
  55. package/generator/src/resolve-elm-module.js +64 -0
  56. package/generator/src/rewrite-client-elm-json.js +6 -5
  57. package/generator/src/rewrite-elm-json-help.js +56 -0
  58. package/generator/src/rewrite-elm-json.js +17 -7
  59. package/generator/src/route-codegen-helpers.js +16 -31
  60. package/generator/src/seo-renderer.js +12 -7
  61. package/generator/src/vite-utils.js +77 -0
  62. package/generator/static-code/elm-pages.js +10 -0
  63. package/generator/static-code/hmr.js +79 -13
  64. package/generator/template/app/Api.elm +6 -5
  65. package/generator/template/app/Effect.elm +123 -0
  66. package/generator/template/app/ErrorPage.elm +37 -6
  67. package/generator/template/app/Route/Index.elm +17 -10
  68. package/generator/template/app/Shared.elm +24 -47
  69. package/generator/template/app/Site.elm +19 -6
  70. package/generator/template/app/View.elm +1 -8
  71. package/generator/template/elm-tooling.json +0 -3
  72. package/generator/template/elm.json +32 -24
  73. package/generator/template/package.json +10 -4
  74. package/package.json +30 -27
  75. package/src/ApiRoute.elm +199 -61
  76. package/src/BackendTask/Custom.elm +325 -0
  77. package/src/BackendTask/Env.elm +90 -0
  78. package/src/{DataSource → BackendTask}/File.elm +171 -56
  79. package/src/{DataSource → BackendTask}/Glob.elm +136 -125
  80. package/src/BackendTask/Http.elm +679 -0
  81. package/src/{DataSource → BackendTask}/Internal/Glob.elm +1 -1
  82. package/src/BackendTask/Internal/Request.elm +69 -0
  83. package/src/BackendTask/Random.elm +79 -0
  84. package/src/BackendTask/Time.elm +47 -0
  85. package/src/BackendTask.elm +537 -0
  86. package/src/FatalError.elm +90 -0
  87. package/src/Head.elm +237 -7
  88. package/src/HtmlPrinter.elm +7 -3
  89. package/src/Internal/ApiRoute.elm +7 -5
  90. package/src/PageServerResponse.elm +6 -1
  91. package/src/Pages/Form.elm +229 -0
  92. package/src/Pages/GeneratorProgramConfig.elm +15 -0
  93. package/src/Pages/Internal/FatalError.elm +5 -0
  94. package/src/Pages/Internal/Msg.elm +93 -0
  95. package/src/Pages/Internal/Platform/Cli.elm +612 -763
  96. package/src/Pages/Internal/Platform/CompatibilityKey.elm +6 -0
  97. package/src/Pages/Internal/Platform/Effect.elm +1 -2
  98. package/src/Pages/Internal/Platform/GeneratorApplication.elm +379 -0
  99. package/src/Pages/Internal/Platform/StaticResponses.elm +65 -276
  100. package/src/Pages/Internal/Platform/ToJsPayload.elm +6 -9
  101. package/src/Pages/Internal/Platform.elm +327 -194
  102. package/src/Pages/Internal/Script.elm +17 -0
  103. package/src/Pages/Internal/StaticHttpBody.elm +35 -1
  104. package/src/Pages/Manifest.elm +29 -4
  105. package/src/Pages/PageUrl.elm +23 -9
  106. package/src/Pages/ProgramConfig.elm +26 -15
  107. package/src/Pages/Script.elm +109 -0
  108. package/src/Pages/SiteConfig.elm +3 -2
  109. package/src/Pages/StaticHttp/Request.elm +2 -2
  110. package/src/Pages/StaticHttpRequest.elm +23 -99
  111. package/src/Pages/Transition.elm +12 -3
  112. package/src/PagesMsg.elm +82 -0
  113. package/src/Path.elm +16 -19
  114. package/src/QueryParams.elm +21 -172
  115. package/src/RequestsAndPending.elm +37 -20
  116. package/src/Result/Extra.elm +26 -0
  117. package/src/Scaffold/Form.elm +546 -0
  118. package/src/Scaffold/Route.elm +1402 -0
  119. package/src/Server/Request.elm +73 -72
  120. package/src/Server/Session.elm +62 -42
  121. package/src/Server/SetCookie.elm +12 -4
  122. package/src/Stub.elm +53 -0
  123. package/src/Test/Html/Internal/ElmHtml/ToString.elm +8 -9
  124. package/src/DataSource/Env.elm +0 -38
  125. package/src/DataSource/Http.elm +0 -446
  126. package/src/DataSource/Internal/Request.elm +0 -20
  127. package/src/DataSource/Port.elm +0 -90
  128. package/src/DataSource.elm +0 -538
  129. package/src/Form/Field.elm +0 -717
  130. package/src/Form/FieldStatus.elm +0 -36
  131. package/src/Form/FieldView.elm +0 -417
  132. package/src/Form/FormData.elm +0 -22
  133. package/src/Form/Validation.elm +0 -391
  134. package/src/Form/Value.elm +0 -118
  135. package/src/Form.elm +0 -1683
  136. package/src/FormDecoder.elm +0 -102
  137. package/src/Pages/FormState.elm +0 -256
  138. package/src/Pages/Generate.elm +0 -800
  139. package/src/Pages/Internal/Form.elm +0 -17
  140. package/src/Pages/Msg.elm +0 -79
@@ -0,0 +1,546 @@
1
+ module Scaffold.Form exposing
2
+ ( Kind(..), provide, restArgsParser
3
+ , Context
4
+ , fieldEncoder, recordEncoder
5
+ )
6
+
7
+ {-|
8
+
9
+ @docs Kind, provide, restArgsParser
10
+
11
+ @docs Context
12
+
13
+ @docs fieldEncoder, recordEncoder
14
+
15
+ -}
16
+
17
+ import Cli.Option
18
+ import Elm
19
+ import Elm.Annotation as Type
20
+ import Elm.Declare
21
+ import Elm.Op
22
+ import List.Extra
23
+ import Result.Extra
24
+
25
+
26
+ {-| -}
27
+ type Kind
28
+ = FieldInt
29
+ | FieldText
30
+ | FieldTextarea
31
+ | FieldFloat
32
+ | FieldTime
33
+ | FieldDate
34
+ | FieldCheckbox
35
+
36
+
37
+ {-| -}
38
+ type alias Context =
39
+ { errors : Elm.Expression
40
+ , submitting : Elm.Expression
41
+ , submitAttempted : Elm.Expression
42
+ , data : Elm.Expression
43
+ , expression : Elm.Expression
44
+ }
45
+
46
+
47
+ formWithFields : Bool -> List ( String, Kind ) -> ({ formState : { errors : Elm.Expression, submitting : Elm.Expression, submitAttempted : Elm.Expression, data : Elm.Expression, expression : Elm.Expression }, params : List { name : String, kind : Kind, param : Elm.Expression } } -> Elm.Expression) -> { declaration : Elm.Declaration, call : List Elm.Expression -> Elm.Expression, callFrom : List String -> List Elm.Expression -> Elm.Expression, value : List String -> Elm.Expression }
48
+ formWithFields elmCssView fields viewFn =
49
+ Elm.Declare.function "form"
50
+ []
51
+ (\_ ->
52
+ fields
53
+ |> List.foldl
54
+ (\( fieldName, kind ) chain ->
55
+ chain
56
+ |> Elm.Op.pipe
57
+ (formField fieldName
58
+ (case kind of
59
+ FieldText ->
60
+ formFieldText
61
+ |> Elm.Op.pipe (formFieldRequired (Elm.string "Required"))
62
+
63
+ FieldInt ->
64
+ formFieldInt { invalid = \_ -> Elm.string "" }
65
+ |> Elm.Op.pipe (formFieldRequired (Elm.string "Required"))
66
+
67
+ FieldTextarea ->
68
+ formFieldText
69
+ |> Elm.Op.pipe (formFieldRequired (Elm.string "Required"))
70
+ |> Elm.Op.pipe
71
+ (formFieldTextarea
72
+ { rows = Elm.nothing
73
+ , cols = Elm.nothing
74
+ }
75
+ )
76
+
77
+ FieldFloat ->
78
+ formFieldFloat { invalid = \_ -> Elm.string "" }
79
+ |> Elm.Op.pipe (formFieldRequired (Elm.string "Required"))
80
+
81
+ FieldTime ->
82
+ formFieldTime { invalid = \_ -> Elm.string "" }
83
+ |> Elm.Op.pipe (formFieldRequired (Elm.string "Required"))
84
+
85
+ FieldDate ->
86
+ formFieldDate { invalid = \_ -> Elm.string "" }
87
+ |> Elm.Op.pipe (formFieldRequired (Elm.string "Required"))
88
+
89
+ FieldCheckbox ->
90
+ formFieldCheckbox
91
+ )
92
+ )
93
+ )
94
+ (Elm.function (List.map fieldToParam fields)
95
+ (\params ->
96
+ Elm.record
97
+ [ ( "combine"
98
+ , params
99
+ |> List.foldl
100
+ (\fieldExpression chain ->
101
+ chain
102
+ |> Elm.Op.pipe (validationAndMap fieldExpression)
103
+ )
104
+ (Elm.val "ParsedForm"
105
+ |> Elm.Op.pipe validationSucceed
106
+ )
107
+ )
108
+ , ( "view"
109
+ , Elm.fn ( "formState", Nothing )
110
+ (\formState ->
111
+ let
112
+ mappedParams : List { name : String, kind : Kind, param : Elm.Expression }
113
+ mappedParams =
114
+ params
115
+ |> List.Extra.zip fields
116
+ |> List.map
117
+ (\( ( name, kind ), param ) ->
118
+ { name = name
119
+ , kind = kind
120
+ , param = param
121
+ }
122
+ )
123
+ in
124
+ viewFn
125
+ { formState =
126
+ { errors = formState |> Elm.get "errors"
127
+ , submitting = formState |> Elm.get "submitting"
128
+ , submitAttempted = formState |> Elm.get "submitAttempted"
129
+ , data = formState |> Elm.get "data"
130
+ , expression = formState
131
+ }
132
+ , params = mappedParams
133
+ }
134
+ )
135
+ )
136
+ ]
137
+ )
138
+ |> Elm.Op.pipe formInit
139
+ )
140
+ |> Elm.withType
141
+ (Type.namedWith [ "Form" ]
142
+ (if elmCssView then
143
+ "StyledHtmlForm"
144
+
145
+ else
146
+ "HtmlForm"
147
+ )
148
+ [ Type.string
149
+ , Type.named [] "ParsedForm"
150
+ , Type.var "input"
151
+ , Type.namedWith [ "PagesMsg" ] "PagesMsg" [ Type.named [] "Msg" ]
152
+ ]
153
+ )
154
+ )
155
+
156
+
157
+ fieldToParam : ( String, Kind ) -> ( String, Maybe Type.Annotation )
158
+ fieldToParam ( name, _ ) =
159
+ ( name, Nothing )
160
+
161
+
162
+ {-| -}
163
+ restArgsParser : Cli.Option.Option (List String) (List ( String, Kind )) Cli.Option.RestArgsOption
164
+ restArgsParser =
165
+ Cli.Option.restArgs "formFields"
166
+ |> Cli.Option.validateMap
167
+ (\items ->
168
+ items
169
+ |> List.map parseField
170
+ |> Result.Extra.combine
171
+ )
172
+
173
+
174
+ parseField : String -> Result String ( String, Kind )
175
+ parseField rawField =
176
+ case String.split ":" rawField of
177
+ [ fieldName ] ->
178
+ Ok ( fieldName, FieldText )
179
+
180
+ [ fieldName, fieldKind ] ->
181
+ (case fieldKind of
182
+ "text" ->
183
+ Ok FieldText
184
+
185
+ "textarea" ->
186
+ Ok FieldTextarea
187
+
188
+ "checkbox" ->
189
+ Ok FieldCheckbox
190
+
191
+ "time" ->
192
+ Ok FieldTime
193
+
194
+ "date" ->
195
+ Ok FieldDate
196
+
197
+ invalidFieldKind ->
198
+ Err ("I wasn't able to interpret the type of the field `" ++ fieldName ++ "` because it has an unexpected field type `" ++ invalidFieldKind ++ "`.")
199
+ )
200
+ |> Result.map (Tuple.pair fieldName)
201
+
202
+ _ ->
203
+ Err ("Unexpected form field format: `" ++ rawField ++ "`. Must be in format `first` or `checkin:date`.")
204
+
205
+
206
+ {-| -}
207
+ provide :
208
+ { fields : List ( String, Kind )
209
+ , elmCssView : Bool
210
+ , view :
211
+ { formState : Context
212
+ , params : List { name : String, kind : Kind, param : Elm.Expression }
213
+ }
214
+ -> Elm.Expression
215
+ }
216
+ ->
217
+ Maybe
218
+ { formHandlers : Elm.Expression
219
+ , form : Elm.Expression
220
+ , declarations : List Elm.Declaration
221
+ }
222
+ provide { fields, view, elmCssView } =
223
+ if List.isEmpty fields then
224
+ Nothing
225
+
226
+ else
227
+ let
228
+ form : { declaration : Elm.Declaration, call : List Elm.Expression -> Elm.Expression, callFrom : List String -> List Elm.Expression -> Elm.Expression, value : List String -> Elm.Expression }
229
+ form =
230
+ formWithFields elmCssView fields view
231
+
232
+ formHandlersDeclaration : { declaration : Elm.Declaration, call : List Elm.Expression -> Elm.Expression, callFrom : List String -> List Elm.Expression -> Elm.Expression, value : List String -> Elm.Expression }
233
+ formHandlersDeclaration =
234
+ -- TODO customizable formHandlers name?
235
+ Elm.Declare.function "formHandlers"
236
+ []
237
+ (\_ ->
238
+ initCombined (Elm.val "Action") (form.call [])
239
+ |> Elm.withType
240
+ (Type.namedWith [ "Form", "Handler" ]
241
+ "Handler"
242
+ [ Type.string
243
+ , Type.named [] "Action"
244
+ ]
245
+ )
246
+ )
247
+ in
248
+ Just
249
+ { formHandlers = formHandlersDeclaration.call []
250
+ , form = form.call []
251
+ , declarations =
252
+ [ formWithFields elmCssView fields view |> .declaration
253
+ , Elm.customType "Action"
254
+ [ Elm.variantWith "Action" [ Type.named [] "ParsedForm" ]
255
+ ]
256
+ , formHandlersDeclaration.declaration
257
+
258
+ -- TODO customize ParsedForm name?
259
+ , Elm.alias "ParsedForm"
260
+ (fields
261
+ |> List.map
262
+ (\( fieldName, kind ) ->
263
+ ( fieldName
264
+ , case kind of
265
+ FieldText ->
266
+ Type.string
267
+
268
+ FieldInt ->
269
+ Type.int
270
+
271
+ FieldTextarea ->
272
+ Type.string
273
+
274
+ FieldFloat ->
275
+ Type.float
276
+
277
+ FieldTime ->
278
+ Type.named [ "Form", "Field" ] "TimeOfDay"
279
+
280
+ FieldDate ->
281
+ Type.named [ "Date" ] "Date"
282
+
283
+ FieldCheckbox ->
284
+ Type.bool
285
+ )
286
+ )
287
+ |> Type.record
288
+ )
289
+ ]
290
+ }
291
+
292
+
293
+ validationAndMap : Elm.Expression -> Elm.Expression
294
+ validationAndMap andMapArg =
295
+ Elm.apply
296
+ (Elm.value
297
+ { importFrom = [ "Form", "Validation" ]
298
+ , name = "andMap"
299
+ , annotation = Nothing
300
+ }
301
+ )
302
+ [ andMapArg ]
303
+
304
+
305
+ validationSucceed : Elm.Expression
306
+ validationSucceed =
307
+ Elm.value
308
+ { importFrom = [ "Form", "Validation" ]
309
+ , name = "succeed"
310
+ , annotation = Nothing
311
+ }
312
+
313
+
314
+ formFieldText : Elm.Expression
315
+ formFieldText =
316
+ Elm.value
317
+ { importFrom = [ "Form", "Field" ]
318
+ , name = "text"
319
+ , annotation = Nothing
320
+ }
321
+
322
+
323
+ formFieldRequired : Elm.Expression -> Elm.Expression
324
+ formFieldRequired requiredArg =
325
+ Elm.apply
326
+ (Elm.value
327
+ { importFrom = [ "Form", "Field" ]
328
+ , name = "required"
329
+ , annotation = Nothing
330
+ }
331
+ )
332
+ [ requiredArg ]
333
+
334
+
335
+ formFieldInt : { invalid : Elm.Expression -> Elm.Expression } -> Elm.Expression
336
+ formFieldInt intArg =
337
+ Elm.apply
338
+ (Elm.value
339
+ { importFrom = [ "Form", "Field" ]
340
+ , name = "int"
341
+ , annotation =
342
+ Nothing
343
+ }
344
+ )
345
+ [ Elm.record
346
+ [ Tuple.pair
347
+ "invalid"
348
+ (Elm.functionReduced "intUnpack" intArg.invalid)
349
+ ]
350
+ ]
351
+
352
+
353
+ formFieldTextarea :
354
+ { rows : Elm.Expression, cols : Elm.Expression }
355
+ -> Elm.Expression
356
+ formFieldTextarea textareaArg =
357
+ Elm.apply
358
+ (Elm.value
359
+ { importFrom = [ "Form", "Field" ]
360
+ , name = "textarea"
361
+ , annotation = Nothing
362
+ }
363
+ )
364
+ [ Elm.record
365
+ [ Tuple.pair "rows" textareaArg.rows
366
+ , Tuple.pair "cols" textareaArg.cols
367
+ ]
368
+ ]
369
+
370
+
371
+ formFieldTime : { invalid : Elm.Expression -> Elm.Expression } -> Elm.Expression
372
+ formFieldTime timeArg =
373
+ Elm.apply
374
+ (Elm.value
375
+ { importFrom = [ "Form", "Field" ]
376
+ , name = "time"
377
+ , annotation = Nothing
378
+ }
379
+ )
380
+ [ Elm.record
381
+ [ Tuple.pair
382
+ "invalid"
383
+ (Elm.functionReduced "timeUnpack" timeArg.invalid)
384
+ ]
385
+ ]
386
+
387
+
388
+ formFieldDate : { invalid : Elm.Expression -> Elm.Expression } -> Elm.Expression
389
+ formFieldDate dateArg =
390
+ Elm.apply
391
+ (Elm.value
392
+ { importFrom = [ "Form", "Field" ]
393
+ , name = "date"
394
+ , annotation = Nothing
395
+ }
396
+ )
397
+ [ Elm.record
398
+ [ Tuple.pair
399
+ "invalid"
400
+ (Elm.functionReduced "dateUnpack" dateArg.invalid)
401
+ ]
402
+ ]
403
+
404
+
405
+ formFieldCheckbox : Elm.Expression
406
+ formFieldCheckbox =
407
+ Elm.value
408
+ { importFrom = [ "Form", "Field" ]
409
+ , name = "checkbox"
410
+ , annotation = Nothing
411
+ }
412
+
413
+
414
+ formFieldFloat : { invalid : Elm.Expression -> Elm.Expression } -> Elm.Expression
415
+ formFieldFloat floatArg =
416
+ Elm.apply
417
+ (Elm.value
418
+ { importFrom = [ "Form", "Field" ]
419
+ , name = "float"
420
+ , annotation = Nothing
421
+ }
422
+ )
423
+ [ Elm.record
424
+ [ Tuple.pair
425
+ "invalid"
426
+ (Elm.functionReduced "floatUnpack" floatArg.invalid)
427
+ ]
428
+ ]
429
+
430
+
431
+ formField : String -> Elm.Expression -> Elm.Expression
432
+ formField fieldArg fieldArg0 =
433
+ Elm.apply
434
+ (Elm.value
435
+ { importFrom = [ "Form" ]
436
+ , name = "field"
437
+ , annotation = Nothing
438
+ }
439
+ )
440
+ [ Elm.string fieldArg, fieldArg0 ]
441
+
442
+
443
+ formInit : Elm.Expression
444
+ formInit =
445
+ Elm.value
446
+ { importFrom = [ "Form" ]
447
+ , name = "form"
448
+ , annotation = Nothing
449
+ }
450
+ |> Elm.Op.pipe
451
+ (Elm.apply
452
+ (Elm.value
453
+ { importFrom = [ "Form" ]
454
+ , name = "hiddenKind"
455
+ , annotation = Nothing
456
+ }
457
+ )
458
+ [ Elm.tuple (Elm.string "kind") (Elm.string "regular")
459
+ , Elm.string "Expected kind."
460
+ ]
461
+ )
462
+
463
+
464
+ initCombined : Elm.Expression -> Elm.Expression -> Elm.Expression
465
+ initCombined initCombinedArg initCombinedArg0 =
466
+ Elm.apply
467
+ (Elm.value
468
+ { importFrom = [ "Form", "Handler" ]
469
+ , name = "init"
470
+ , annotation = Nothing
471
+ }
472
+ )
473
+ [ initCombinedArg, initCombinedArg0 ]
474
+
475
+
476
+ {-| Generate a JSON Encoder for the form fields. This can be helpful for sending the validated form data through a
477
+ BackendTask.Custom or to an external API from your scaffolded Route Module code.
478
+ -}
479
+ recordEncoder : Elm.Expression -> List ( String, Kind ) -> Elm.Expression
480
+ recordEncoder record fields =
481
+ fields
482
+ |> List.map
483
+ (\( field, kind ) ->
484
+ Elm.tuple
485
+ (Elm.string field)
486
+ (fieldEncoder record field kind)
487
+ )
488
+ |> Elm.list
489
+ |> List.singleton
490
+ |> Elm.apply
491
+ (Elm.value
492
+ { importFrom = [ "Json", "Encode" ]
493
+ , name = "object"
494
+ , annotation =
495
+ Just
496
+ (Type.function
497
+ [ Type.list
498
+ (Type.tuple
499
+ Type.string
500
+ (Type.namedWith [ "Json", "Encode" ] "Value" [])
501
+ )
502
+ ]
503
+ (Type.namedWith [ "Json", "Encode" ] "Value" [])
504
+ )
505
+ }
506
+ )
507
+
508
+
509
+ {-| -}
510
+ fieldEncoder : Elm.Expression -> String -> Kind -> Elm.Expression
511
+ fieldEncoder record name kind =
512
+ Elm.apply
513
+ (case kind of
514
+ FieldInt ->
515
+ encoder "int"
516
+
517
+ FieldText ->
518
+ encoder "string"
519
+
520
+ FieldTextarea ->
521
+ encoder "string"
522
+
523
+ FieldFloat ->
524
+ encoder "float"
525
+
526
+ FieldTime ->
527
+ -- TODO fix time encoder
528
+ encoder "int"
529
+
530
+ FieldDate ->
531
+ -- TODO fix date encoder
532
+ encoder "int"
533
+
534
+ FieldCheckbox ->
535
+ encoder "bool"
536
+ )
537
+ [ Elm.get name record ]
538
+
539
+
540
+ encoder : String -> Elm.Expression
541
+ encoder name =
542
+ Elm.value
543
+ { importFrom = [ "Json", "Encode" ]
544
+ , name = name
545
+ , annotation = Nothing
546
+ }