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

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 (40) hide show
  1. package/README.md +1 -1
  2. package/codegen/elm-pages-codegen.cjs +251 -285
  3. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/d.dat +0 -0
  4. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/node_runner.js +1 -1
  5. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/node_supervisor.js +1 -1
  6. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/d.dat +0 -0
  7. package/generator/review/elm-stuff/tests-0.19.1/js/node_runner.js +1 -1
  8. package/generator/review/elm-stuff/tests-0.19.1/js/node_supervisor.js +1 -1
  9. package/generator/src/RouteBuilder.elm +19 -60
  10. package/generator/src/SharedTemplate.elm +5 -5
  11. package/generator/src/compatibility-key.js +2 -2
  12. package/package.json +2 -2
  13. package/src/ApiRoute.elm +3 -31
  14. package/src/BackendTask.elm +18 -24
  15. package/src/FormData.elm +21 -1
  16. package/src/Head/Seo.elm +4 -4
  17. package/src/Internal/Request.elm +84 -4
  18. package/src/Pages/ConcurrentSubmission.elm +127 -0
  19. package/src/Pages/Form.elm +151 -40
  20. package/src/Pages/FormData.elm +19 -0
  21. package/src/Pages/Internal/NotFoundReason.elm +4 -4
  22. package/src/Pages/Internal/Platform/Cli.elm +30 -17
  23. package/src/Pages/Internal/Platform/CompatibilityKey.elm +1 -1
  24. package/src/Pages/Internal/Platform.elm +39 -38
  25. package/src/Pages/Internal/ResponseSketch.elm +2 -2
  26. package/src/Pages/Manifest.elm +23 -7
  27. package/src/Pages/Navigation.elm +85 -0
  28. package/src/Pages/PageUrl.elm +3 -3
  29. package/src/Pages/ProgramConfig.elm +13 -11
  30. package/src/Pages/Script.elm +64 -7
  31. package/src/Pages/Url.elm +3 -3
  32. package/src/PagesMsg.elm +9 -3
  33. package/src/RenderRequest.elm +7 -7
  34. package/src/Scaffold/Form.elm +28 -5
  35. package/src/Scaffold/Route.elm +82 -53
  36. package/src/Server/Request.elm +446 -952
  37. package/src/Server/Session.elm +141 -91
  38. package/src/Server/SetCookie.elm +71 -31
  39. package/src/{Path.elm → UrlPath.elm} +21 -21
  40. package/src/Pages/Transition.elm +0 -79
@@ -0,0 +1,127 @@
1
+ module Pages.ConcurrentSubmission exposing
2
+ ( ConcurrentSubmission, Status(..)
3
+ , map
4
+ )
5
+
6
+ {-| When you render a `Form` with the [`Pages.Form.withConcurrent`](Pages-Form#withConcurrent) `Option`, the state of in-flight and completed submissions will be available
7
+ from your `Route` module through `app.concurrentSubmissions` as a `Dict String (ConcurrentSubmission (Maybe Action))`.
8
+
9
+ You can use this state to declaratively derive Pending UI or Optimistic UI from your pending submissions (without managing the state in your `Model`, since `elm-pages`
10
+ manages form submission state for you).
11
+
12
+ You can [see the full-stack TodoMVC example](https://github.com/dillonkearns/elm-pages-v3-beta/blob/master/examples/todos/app/Route/Visibility__.elm) for a complete example of deriving Pending UI state from `app.concurrentSubmissions`.
13
+
14
+ For example, this how the TodoMVC example derives the list of new items that are being created (but are still pending).
15
+
16
+ view :
17
+ App Data ActionData RouteParams
18
+ -> Shared.Model
19
+ -> Model
20
+ -> View (PagesMsg Msg)
21
+ view app shared model =
22
+ let
23
+ pendingActions : List Action
24
+ pendingActions =
25
+ app.concurrentSubmissions
26
+ |> Dict.values
27
+ |> List.filterMap
28
+ (\{ status, payload } ->
29
+ case status of
30
+ Pages.ConcurrentSubmission.Complete _ ->
31
+ Nothing
32
+
33
+ _ ->
34
+ allForms
35
+ |> Form.Handler.run payload.fields
36
+ |> Form.toResult
37
+ |> Result.toMaybe
38
+ )
39
+
40
+ newPendingItems : List Entry
41
+ newPendingItems =
42
+ pendingActions
43
+ |> List.filterMap
44
+ (\submission ->
45
+ case submission of
46
+ Add description ->
47
+ Just
48
+ { description = description
49
+ , completed = False
50
+ , id = ""
51
+ , isPending = True
52
+ }
53
+
54
+ _ ->
55
+ -- `newPendingItems` only cares about pending Add actions. Other values will use
56
+ -- pending submissions for other types of Actions.
57
+ Nothing
58
+ )
59
+ in
60
+ itemsView app newPendingItems
61
+
62
+ allForms : Form.Handler.Handler String Action
63
+ allForms =
64
+ |> Form.Handler.init Add addItemForm
65
+ -- |> Form.Handler.with ...
66
+
67
+
68
+ type Action
69
+ = UpdateEntry ( String, String )
70
+ | Add String
71
+ | Delete String
72
+ | DeleteComplete
73
+ | Check ( Bool, String )
74
+ | CheckAll Bool
75
+
76
+ @docs ConcurrentSubmission, Status
77
+
78
+ @docs map
79
+
80
+ -}
81
+
82
+ import Pages.FormData exposing (FormData)
83
+ import Time
84
+
85
+
86
+ {-| -}
87
+ type alias ConcurrentSubmission actionData =
88
+ { status : Status actionData
89
+ , payload : FormData
90
+ , initiatedAt : Time.Posix
91
+ }
92
+
93
+
94
+ {-| The status of a `ConcurrentSubmission`.
95
+
96
+ - `Submitting` - The submission is in-flight.
97
+ - `Reloading` - The submission has completed, and the page is now reloading the `Route`'s `data` to reflect the new state. The `actionData` holds any data returned from the `Route`'s `action`.
98
+ - `Complete` - The submission has completed, and the `Route`'s `data` has since reloaded so the state reflects the refreshed state after completing this specific form submission. The `actionData` holds any data returned from the `Route`'s `action`.
99
+
100
+ -}
101
+ type Status actionData
102
+ = Submitting
103
+ | Reloading actionData
104
+ | Complete actionData
105
+
106
+
107
+ {-| `map` a `ConcurrentSubmission`. Not needed for most high-level cases since this state is managed by the `elm-pages` framework for you.
108
+ -}
109
+ map : (a -> b) -> ConcurrentSubmission a -> ConcurrentSubmission b
110
+ map mapFn fetcherState =
111
+ { status = mapStatus mapFn fetcherState.status
112
+ , payload = fetcherState.payload
113
+ , initiatedAt = fetcherState.initiatedAt
114
+ }
115
+
116
+
117
+ mapStatus : (a -> b) -> Status a -> Status b
118
+ mapStatus mapFn fetcherSubmitStatus =
119
+ case fetcherSubmitStatus of
120
+ Submitting ->
121
+ Submitting
122
+
123
+ Reloading value ->
124
+ Reloading (mapFn value)
125
+
126
+ Complete value ->
127
+ Complete (mapFn value)
@@ -1,11 +1,47 @@
1
1
  module Pages.Form exposing
2
- ( Strategy(..), renderHtml, renderStyledHtml
2
+ ( renderHtml, renderStyledHtml
3
+ , Options
4
+ , withConcurrent
3
5
  , FormWithServerValidations, Handler
4
6
  )
5
7
 
6
- {-|
8
+ {-| `elm-pages` has a built-in integration with [`dillonkearns/elm-form`](https://package.elm-lang.org/packages/dillonkearns/elm-form/latest/). See the `dillonkearns/elm-form`
9
+ docs and examples for more information on how to define your [`Form`](https://package.elm-lang.org/packages/dillonkearns/elm-form/latest/Form). This module is the interface for rendering your `Form` in your `elm-pages` app.
7
10
 
8
- @docs Strategy, renderHtml, renderStyledHtml
11
+ By rendering your `Form` with this module,
12
+ you get all of the boilerplate managed for you automatically by the `elm-pages` framework. That means you do not need to use `Form.init`, `Form.update`, `Form.Model` since these are all
13
+ abstracted away. In addition to that, in-flight form state is automatically managed for you and exposed through the `app` argument in your Route modules.
14
+
15
+ This means that you can declaratively derive Pending UI or Optimistic UI state from `app.navigation` or `app.concurrentSubmissions` in your Route modules, and even build a
16
+ rich dynamic page that shows pending submissions in the UI without using your Route module's `Model`! This is the power of this abstraction - it's less error-prone to
17
+ declaratively derive state rather than imperatively managing your `Model`.
18
+
19
+
20
+ ## Rendering Forms
21
+
22
+ @docs renderHtml, renderStyledHtml
23
+
24
+ @docs Options
25
+
26
+
27
+ ## Form Submission Strategies
28
+
29
+ When you render with [`Pages.Form.renderHtml`](#renderHtml) or [`Pages.Form.renderStyledHtml`](#renderStyledHtml),
30
+ `elm-pages` progressively enhances form submissions to manage the requests through Elm (instead of as a vanilla HTML form submission, which performs a full page reload).
31
+
32
+ By default, `elm-pages` Forms will use the same mental model as the browser's default form submission behavior. That is, the form submission state will be tied to the page's navigation state.
33
+ If you click a link while a form is submitting, the form submission will be cancelled and the page will navigate to the new page. Conceptually, you can think of this as being tied to the navigation state.
34
+ A form submission is part of the page's navigation state, and so is a page navigation. So if you have a page with an edit form, a delete form (no inputs but only a delete button), and a link to a new page,
35
+ you can interact with any of these and it will cancel the previous interactions.
36
+
37
+ You can access this state through `app.navigation` in your `Route` module, which is a value of type [`Pages.Navigation`](Pages-Navigation).
38
+
39
+ This default form submission strategy is a good fit for more linear actions. This is more traditional server submission behavior that you might be familiar with from Rails or other server frameworks without JavaScript enhancement.
40
+
41
+ @docs withConcurrent
42
+
43
+
44
+ ## Server-Side Validation
9
45
 
10
46
  @docs FormWithServerValidations, Handler
11
47
 
@@ -19,8 +55,9 @@ import Form.Handler
19
55
  import Form.Validation exposing (Validation)
20
56
  import Html
21
57
  import Html.Styled
58
+ import Pages.ConcurrentSubmission
22
59
  import Pages.Internal.Msg
23
- import Pages.Transition
60
+ import Pages.Navigation
24
61
  import PagesMsg exposing (PagesMsg)
25
62
 
26
63
 
@@ -45,56 +82,122 @@ type alias Handler error combined =
45
82
  Form.Handler.Handler error (BackendTask FatalError (Validation error combined Never Never))
46
83
 
47
84
 
48
- {-| -}
49
- type Strategy
50
- = Parallel
51
- | Serial
85
+ {-| A replacement for [`Form.Options`](https://package.elm-lang.org/packages/dillonkearns/elm-form/latest/Form#Options)
86
+ with some extra configuration for the `elm-pages` integration. You can use this type to annotate your form's options.
87
+ -}
88
+ type alias Options error parsed input msg =
89
+ Form.Options error parsed input msg { concurrent : Bool }
52
90
 
53
91
 
54
- {-| -}
92
+ {-| Instead of using the default submission strategy (tied to the page's navigation state), you can use `withConcurrent`.
93
+ `withConcurrent` allows multiple form submissions to be in flight at the same time. It is useful for more dynamic applications. A good rule of thumb
94
+ is if you could have multiple pending spinners on the page at the same time, you should use `withConcurrent`. For example, if you have a page with a list of items,
95
+ say a Twitter clone. If you click the like button on a Tweet, it won't result in a page navigation. You can click the like button on multiple Tweets at the same time
96
+ and they will all submit independently.
97
+
98
+ In the case of Twitter, there isn't an indication of a loading spinner on the like button because it is expected that it will succeed. This is an example of a User Experience (UX) pattern
99
+ called Optimistic UI. Since it is very likely that liking a Tweet will be successful, the UI will update the UI as if it has immediately succeeded even though the request is still in flight.
100
+ If the request fails, the UI will be updated to reflect the failure with an animation to show that something went wrong.
101
+
102
+ The `withConcurrent` is a good fit for either of these UX patterns (Optimistic UI or Pending UI, i.e. showing a loading spinner). You can derive either of these
103
+ visual states from the `app.concurrentSubmissions` field in your `Route` module.
104
+
105
+ You can call `withConcurrent` on your `Form.Options`. Note that while `withConcurrent` will allow multiple form submissions to be in flight at the same time independently,
106
+ the ID of the Form will still have a unique submission. For example, if you click submit on a form with the ID `"edit-123"` and then submit it again before the first submission has completed,
107
+ the second submission will cancel the first submission. So it is important to use unique IDs for forms that represent unique operations.
108
+
109
+ import Form
110
+ import Pages.Form
111
+
112
+ todoItemView app todo =
113
+ deleteItemForm
114
+ |> Pages.Form.renderHtml []
115
+ (Form.options ("delete-" ++ todo.id)
116
+ |> Form.withInput todo
117
+ |> Pages.Form.withConcurrent
118
+ )
119
+ app
120
+
121
+ -}
122
+ withConcurrent : Options error parsed input msg -> Options error parsed input msg
123
+ withConcurrent options_ =
124
+ { options_ | extras = Just { concurrent = True } }
125
+
126
+
127
+
128
+ --init :
129
+ -- (parsed -> combined)
130
+ -- -> FormWithServerValidations error parsed input view
131
+ -- -> Handler error combined
132
+ --init mapFn form =
133
+ -- Form.Handler.init
134
+ -- (\something ->
135
+ -- let
136
+ -- foo : parsed
137
+ -- foo =
138
+ -- something
139
+ --
140
+ -- goal : BackendTask FatalError (Validation error combined Never Never)
141
+ -- goal =
142
+ -- Debug.todo ""
143
+ -- in
144
+ -- --Form.Validation.map (BackendTask.map (mapFn something))
145
+ -- --Debug.todo ""
146
+ -- goal
147
+ -- )
148
+ -- form
149
+
150
+
151
+ {-| A replacement for `Form.renderHtml` from `dillonkearns/elm-form` that integrates with `elm-pages`. Use this to render your [`Form`](https://package.elm-lang.org/packages/dillonkearns/elm-form/latest/Form)
152
+ as `elm/html` `Html`.
153
+ -}
55
154
  renderHtml :
56
155
  List (Html.Attribute (PagesMsg userMsg))
57
- -> Strategy
58
- -> Form.Options error parsed input userMsg
156
+ -> Options error parsed input userMsg
59
157
  ->
60
158
  { --path : Path
61
159
  --, url : Maybe PageUrl
62
160
  --, action : Maybe action
63
161
  app
64
162
  | pageFormState : Form.Model
65
- , transition : Maybe Pages.Transition.Transition
66
- , fetchers : Dict String (Pages.Transition.FetcherState (Maybe action))
163
+ , navigation : Maybe Pages.Navigation.Navigation
164
+ , concurrentSubmissions : Dict String (Pages.ConcurrentSubmission.ConcurrentSubmission (Maybe action))
67
165
  }
68
166
  -> Form.Form error { combine : Validation error parsed named constraints, view : Form.Context error input -> List (Html.Html (PagesMsg userMsg)) } parsed input
69
167
  -> Html.Html (PagesMsg userMsg)
70
- renderHtml attrs strategy options_ app form_ =
168
+ renderHtml attrs options_ app form_ =
169
+ let
170
+ concurrent : Bool
171
+ concurrent =
172
+ options_.extras |> Maybe.map .concurrent |> Maybe.withDefault False
173
+ in
71
174
  form_
72
175
  |> Form.renderHtml
73
176
  { state = app.pageFormState
74
177
  , submitting =
75
- (case app.fetchers |> Dict.get options_.id of
178
+ (case app.concurrentSubmissions |> Dict.get options_.id of
76
179
  Just { status } ->
77
180
  case status of
78
- Pages.Transition.FetcherComplete _ ->
181
+ Pages.ConcurrentSubmission.Complete _ ->
79
182
  False
80
183
 
81
- Pages.Transition.FetcherSubmitting ->
184
+ Pages.ConcurrentSubmission.Submitting ->
82
185
  True
83
186
 
84
- Pages.Transition.FetcherReloading _ ->
187
+ Pages.ConcurrentSubmission.Reloading _ ->
85
188
  True
86
189
 
87
190
  Nothing ->
88
191
  False
89
192
  )
90
- || (case app.transition of
91
- Just (Pages.Transition.Submitting formData) ->
193
+ || (case app.navigation of
194
+ Just (Pages.Navigation.Submitting formData) ->
92
195
  formData.id == Just options_.id
93
196
 
94
- Just (Pages.Transition.LoadAfterSubmit submitData _ _) ->
197
+ Just (Pages.Navigation.LoadAfterSubmit submitData _ _) ->
95
198
  submitData.id == Just options_.id
96
199
 
97
- Just (Pages.Transition.Loading _ _) ->
200
+ Just (Pages.Navigation.Loading _ _) ->
98
201
  False
99
202
 
100
203
  Nothing ->
@@ -113,7 +216,7 @@ renderHtml attrs strategy options_ app form_ =
113
216
  case submission.parsed of
114
217
  Form.Valid _ ->
115
218
  Pages.Internal.Msg.Submit
116
- { useFetcher = strategy == Parallel
219
+ { useFetcher = concurrent
117
220
  , action = submission.action
118
221
  , fields = submission.fields
119
222
  , method = submission.method
@@ -127,7 +230,7 @@ renderHtml attrs strategy options_ app form_ =
127
230
 
128
231
  Form.Invalid _ _ ->
129
232
  Pages.Internal.Msg.Submit
130
- { useFetcher = strategy == Parallel
233
+ { useFetcher = concurrent
131
234
  , action = submission.action
132
235
  , method = submission.method
133
236
  , fields = submission.fields
@@ -136,55 +239,62 @@ renderHtml attrs strategy options_ app form_ =
136
239
  , valid = False
137
240
  }
138
241
  )
242
+ , extras = Nothing
139
243
  }
140
244
  attrs
141
245
 
142
246
 
143
- {-| -}
247
+ {-| A replacement for `Form.renderStyledHtml` from `dillonkearns/elm-form` that integrates with `elm-pages`. Use this to render your [`Form`](https://package.elm-lang.org/packages/dillonkearns/elm-form/latest/Form)
248
+ as `rtfeldman/elm-css` `Html.Styled.Html`.
249
+ -}
144
250
  renderStyledHtml :
145
251
  List (Html.Styled.Attribute (PagesMsg userMsg))
146
- -> Strategy
147
- -> Form.Options error parsed input userMsg
252
+ -> Options error parsed input userMsg
148
253
  ->
149
254
  { --path : Path
150
255
  --, url : Maybe PageUrl
151
256
  --, action : Maybe action
152
257
  app
153
258
  | pageFormState : Form.Model
154
- , transition : Maybe Pages.Transition.Transition
155
- , fetchers : Dict String (Pages.Transition.FetcherState (Maybe action))
259
+ , navigation : Maybe Pages.Navigation.Navigation
260
+ , concurrentSubmissions : Dict String (Pages.ConcurrentSubmission.ConcurrentSubmission (Maybe action))
156
261
  }
157
262
  -> Form.Form error { combine : Validation error parsed named constraints, view : Form.Context error input -> List (Html.Styled.Html (PagesMsg userMsg)) } parsed input
158
263
  -> Html.Styled.Html (PagesMsg userMsg)
159
- renderStyledHtml attrs strategy options_ app form_ =
264
+ renderStyledHtml attrs options_ app form_ =
265
+ let
266
+ concurrent : Bool
267
+ concurrent =
268
+ options_.extras |> Maybe.map .concurrent |> Maybe.withDefault False
269
+ in
160
270
  form_
161
271
  |> Form.renderStyledHtml
162
272
  { state = app.pageFormState
163
273
  , toMsg = Pages.Internal.Msg.FormMsg
164
274
  , submitting =
165
- (case app.fetchers |> Dict.get options_.id of
275
+ (case app.concurrentSubmissions |> Dict.get options_.id of
166
276
  Just { status } ->
167
277
  case status of
168
- Pages.Transition.FetcherComplete _ ->
278
+ Pages.ConcurrentSubmission.Complete _ ->
169
279
  False
170
280
 
171
- Pages.Transition.FetcherSubmitting ->
281
+ Pages.ConcurrentSubmission.Submitting ->
172
282
  True
173
283
 
174
- Pages.Transition.FetcherReloading _ ->
284
+ Pages.ConcurrentSubmission.Reloading _ ->
175
285
  True
176
286
 
177
287
  Nothing ->
178
288
  False
179
289
  )
180
- || (case app.transition of
181
- Just (Pages.Transition.Submitting formData) ->
290
+ || (case app.navigation of
291
+ Just (Pages.Navigation.Submitting formData) ->
182
292
  formData.id == Just options_.id
183
293
 
184
- Just (Pages.Transition.LoadAfterSubmit submitData _ _) ->
294
+ Just (Pages.Navigation.LoadAfterSubmit submitData _ _) ->
185
295
  submitData.id == Just options_.id
186
296
 
187
- Just (Pages.Transition.Loading _ _) ->
297
+ Just (Pages.Navigation.Loading _ _) ->
188
298
  False
189
299
 
190
300
  Nothing ->
@@ -202,7 +312,7 @@ renderStyledHtml attrs strategy options_ app form_ =
202
312
  case submission.parsed of
203
313
  Form.Valid _ ->
204
314
  Pages.Internal.Msg.Submit
205
- { useFetcher = strategy == Parallel
315
+ { useFetcher = concurrent
206
316
  , action = submission.action
207
317
  , fields = submission.fields
208
318
  , method = submission.method
@@ -216,7 +326,7 @@ renderStyledHtml attrs strategy options_ app form_ =
216
326
 
217
327
  Form.Invalid _ _ ->
218
328
  Pages.Internal.Msg.Submit
219
- { useFetcher = strategy == Parallel
329
+ { useFetcher = concurrent
220
330
  , action = submission.action
221
331
  , fields = submission.fields
222
332
  , method = submission.method
@@ -225,5 +335,6 @@ renderStyledHtml attrs strategy options_ app form_ =
225
335
  , valid = False
226
336
  }
227
337
  )
338
+ , extras = Nothing
228
339
  }
229
340
  attrs
@@ -0,0 +1,19 @@
1
+ module Pages.FormData exposing (FormData)
2
+
3
+ {-|
4
+
5
+ @docs FormData
6
+
7
+ -}
8
+
9
+ import Form
10
+
11
+
12
+ {-| The payload for form submissions.
13
+ -}
14
+ type alias FormData =
15
+ { fields : List ( String, String )
16
+ , method : Form.Method
17
+ , action : String
18
+ , id : Maybe String
19
+ }
@@ -9,7 +9,7 @@ module Pages.Internal.NotFoundReason exposing (ModuleContext, NotFoundReason(..)
9
9
  import Html exposing (Html)
10
10
  import Html.Attributes as Attr
11
11
  import Pages.Internal.RoutePattern exposing (RoutePattern)
12
- import Path exposing (Path)
12
+ import UrlPath exposing (UrlPath)
13
13
 
14
14
 
15
15
  {-| -}
@@ -22,7 +22,7 @@ type alias ModuleContext =
22
22
 
23
23
  {-| -}
24
24
  type alias Payload =
25
- { path : Path
25
+ { path : UrlPath
26
26
  , reason : NotFoundReason
27
27
  }
28
28
 
@@ -58,7 +58,7 @@ document pathPatterns payload =
58
58
  , Html.code []
59
59
  [ Html.text
60
60
  (payload.path
61
- |> Path.toAbsolute
61
+ |> UrlPath.toAbsolute
62
62
  )
63
63
  ]
64
64
  , Html.text " Did you mean to go to one of these routes:"
@@ -85,7 +85,7 @@ document pathPatterns payload =
85
85
  , Html.code []
86
86
  [ Html.text
87
87
  (payload.path
88
- |> Path.toAbsolute
88
+ |> UrlPath.toAbsolute
89
89
  )
90
90
  ]
91
91
  , Html.text " successfully matched the route "