-
Notifications
You must be signed in to change notification settings - Fork 2
/
Main.elm
348 lines (292 loc) · 9.22 KB
/
Main.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
module Main exposing (main)
import Browser
import Html exposing (Html, text)
import Html.Attributes as HA
import Html.Events as HE
import Json.Decode as JD
import Permissions as P
import ServiceWorker as SW
import Time
import Worker as W
type alias Model =
{ text : String
, posts : List W.Post
, swavailability : SW.Availability
, swRegistration : SW.Registration
, swSubscription : Maybe Bool
, swErrors : List String
, swVapidKey : Maybe String
, permissionStatus : Maybe P.PermissionStatus
, login : Maybe W.Login
, loginForm : LoginForm
}
type alias LoginForm =
String
type Msg
= TextChanged String
| SendAndClear
| SWAvailability SW.Availability
| SWRegistration SW.Registration
| SWClientUpdate (Result JD.Error W.ClientState)
| Subscribe String
| RequestPermission
| ChangedName String
| Login
| Logout
main : Program () Model Msg
main =
Browser.document
{ view = view
, update = update
, init = init
, subscriptions = subscriptions
}
view : Model -> Browser.Document Msg
view model =
{ title = "Elm PWA example"
, body =
[ Html.div []
[ viewPwaInfo model
, case model.login of
Nothing ->
text "getting user info..."
Just login ->
case login of
W.LoggedIn user _ ->
viewChat model user
W.LoggedOut ->
viewLogin model.loginForm
]
]
}
viewLogin : LoginForm -> Html Msg
viewLogin form =
Html.div []
[ Html.form [ HE.onSubmit Login ]
[ Html.fieldset []
[ Html.input
[ HA.value form
, HE.onInput ChangedName
, HA.placeholder "username"
]
[]
]
, Html.button [] [ text "log in" ]
]
]
viewChat : Model -> W.User -> Html Msg
viewChat model user =
Html.div []
[ Html.div []
[ text ("logged in as " ++ user.name ++ " ")
, Html.button [ HE.onClick Logout ] [ text "Logout" ]
]
, Html.ul []
(List.map viewPost model.posts)
, Html.form [ HE.onSubmit SendAndClear ]
[ Html.input
[ HA.value model.text
, HE.onInput (\input -> TextChanged input)
]
[]
, Html.input [ HA.type_ "submit", HA.value "Senden" ] []
]
]
viewPwaInfo : Model -> Html Msg
viewPwaInfo model =
Html.table []
[ Html.caption [] [ text "PWA Info" ]
, Html.tr []
[ Html.td [] [ text "Service Worker API" ]
, Html.td []
[ text <|
case model.swavailability of
SW.Unknown ->
"Unknown"
SW.Available ->
"Available"
SW.NotAvailable ->
"Not Available"
]
]
, Html.tr []
[ Html.td [] [ text "Service Worker Registration" ]
, Html.td []
[ text <|
case model.swRegistration of
SW.RegistrationUnknown ->
"Unknown"
SW.RegistrationSuccess ->
"Registered"
SW.RegistrationError ->
"Registration error"
]
]
, Html.tr []
[ Html.td [] [ text "Service Worker Subscription" ]
, Html.td []
[ case model.swSubscription of
Nothing ->
text "unknown"
Just subscription ->
if subscription then
text "Has subscription"
else
text "No subscription"
]
]
, Html.tr []
[ Html.td [] [ text "Service Worker VAPID key" ]
, Html.td []
[ case model.swVapidKey of
Nothing ->
text "No VAPID key"
Just key ->
Html.div []
[ text "Has VAPID key"
, Html.button
[ HE.onClick (Subscribe key) ]
[ text "Subscribe" ]
]
]
]
, Html.tr []
[ Html.td [] [ text "Notification Permission" ]
, Html.td []
[ case model.permissionStatus of
Nothing ->
text "Unknown"
Just ps ->
case ps of
P.Granted ->
Html.div []
[ text "Granted" ]
P.Prompt ->
Html.div []
[ text (P.permissionStatusString ps)
, Html.button
[ HE.onClick RequestPermission ]
[ text "Request" ]
]
P.Denied ->
Html.div []
[ text (P.permissionStatusString ps)
]
]
]
, Html.tr []
[ Html.td [] [ text "Service Worker errors" ]
, Html.td []
[ Html.ul []
(List.map
(\error -> Html.li [] [ Html.text error ])
model.swErrors
)
]
]
]
viewPost : W.Post -> Html Msg
viewPost post =
Html.li []
[ text post.user.name
, text (formatTime post.time)
, text post.text
, text
(case "PENDING" of
"PENDING" ->
" ⌛"
_ ->
" ✅ "
)
]
formatTime : Time.Posix -> String
formatTime time =
String.fromInt (Time.toHour Time.utc time)
++ ":"
++ String.fromInt (Time.toMinute Time.utc time)
++ ":"
++ String.fromInt (Time.toSecond Time.utc time)
++ "z"
init : () -> ( Model, Cmd Msg )
init _ =
( { text = ""
, posts = []
, swavailability = SW.Unknown
, swRegistration = SW.RegistrationUnknown
, swSubscription = Nothing
, swVapidKey = Nothing
, permissionStatus = Nothing
, loginForm = ""
, login = Nothing
, swErrors = []
}
, SW.checkAvailability
)
subscriptions : Model -> Sub Msg
subscriptions _ =
Sub.batch
[ SW.getAvailability SWAvailability
, SW.getRegistration SWRegistration
, W.onClientUpdate SWClientUpdate
]
submitLogin : LoginForm -> Cmd msg
submitLogin form =
W.Login form
|> W.sendMessage
submitPost : String -> Cmd msg
submitPost text =
W.SubmitPost text |> W.sendMessage
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
SendAndClear ->
( { model | text = "" }, submitPost model.text )
TextChanged newText ->
( { model | text = newText }, Cmd.none )
ChangedName name ->
( { model | loginForm = name }, Cmd.none )
Login ->
( model, submitLogin model.loginForm )
Logout ->
( model, W.logout )
SWAvailability availability ->
( { model | swavailability = availability }
, if
availability
== SW.Available
&& (model.swRegistration == SW.RegistrationUnknown)
then
SW.register
else
Cmd.none
)
SWRegistration registration ->
( { model | swRegistration = registration }
, if registration == SW.RegistrationSuccess then
Cmd.batch [ W.sendMessage W.Hello ]
else
Cmd.none
)
Subscribe key ->
( model, W.sendMessage (W.Subscribe key) )
SWClientUpdate result ->
case result of
Err err ->
let
_ =
Debug.log "ClientUpdate" err
in
( model, Cmd.none )
Ok cu ->
( { model
| swSubscription = cu.subscription
, swVapidKey = cu.vapidKey
, permissionStatus = cu.permissionStatus
, login = cu.login
, posts = cu.posts
, swErrors = cu.swErrors
}
, Cmd.none
)
RequestPermission ->
( model, P.requestPermission )