generated from lindsaykwardell/vite-elm-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.elm
158 lines (106 loc) · 2.7 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
module Main exposing (main)
import Browser
import Html exposing (..)
import Html.Attributes exposing (style)
import Html.Events exposing (..)
import Http
import Json.Decode exposing (Decoder, field, string, map3)
import Debug
-- MAIN
main =
Browser.element
{ init = init
, update = update
, subscriptions = subscriptions
, view = view
}
-- MODEL
type Model
= Failure
| Loading
| Success Page
type alias Page =
{ title : String
, story : String
, journal : String
}
init : () -> (Model, Cmd Msg)
init _ =
(Loading, getWikiPageJson)
-- UPDATE
type Msg
= DoIt
| GotPage (Result Http.Error Page)
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
DoIt ->
(Loading, getWikiPageJson)
GotPage result ->
case result of
Ok page ->
(Success page, Cmd.none)
Err err ->
let
errorMsg =
case err of
Http.Timeout ->
"Timeout"
Http.NetworkError ->
"Network Error"
Http.BadStatus status ->
"Bad Status: " ++ String.fromInt status
Http.BadBody body ->
let
_ = Debug.log "GotPage JSON:" body -- Log the JSON content in case of Failure
in
"Bad Body: " ++ body
Http.BadUrl _ ->
Debug.todo "Handle BadUrl case" -- Placeholder for handling BadUrl case
in
(Failure, Cmd.none)
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
-- VIEW
view : Model -> Html Msg
view model =
div []
[ h2 [] [ text "Wiki Page JSON" ]
, viewPage model
]
viewPage : Model -> Html Msg
viewPage model =
case model of
Failure ->
div []
[ text "I could not load the Wiki Page JSON for some reason. "
, button [ onClick DoIt ] [ text "Try Again!" ]
]
Loading ->
text "Loading..."
Success page ->
div []
[ button [ onClick DoIt, style "display" "block" ] [ text "Do it" ]
, blockquote [] [ text page.title ]
]
-- HTTP
getWikiPageJson : Cmd Msg
getWikiPageJson =
Http.get
{ url = "https://wiki.ralfbarkow.ch/2023-05-31.json"
, expect = Http.expectJson GotPage pageDecoder
}
pageDecoder : Decoder Page
pageDecoder =
map3 Page
(field "title" string)
(field "story" storyDecoder)
(field "journal" journalDecoder)
storyDecoder : Decoder String
storyDecoder =
field "story" string |> Debug.log "Decoding story:"
journalDecoder : Decoder String
journalDecoder =
field "journal" string |> Debug.log "Decoding journal:"