-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpTrackExample
More file actions
125 lines (86 loc) · 2.37 KB
/
Copy pathhttpTrackExample
File metadata and controls
125 lines (86 loc) · 2.37 KB
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
module Main exposing (main)
import Browser
import Html exposing (Html, pre, text)
import Http
-- MAIN
main =
Browser.element
{ init = init
, update = update
, subscriptions = subscriptions
, view = view
}
-- MODEL
type Model
= Failure
| Loading { tracker : String, progress : Http.Progress }
| Success String
init : () -> ( Model, Cmd Msg )
init _ =
( Loading
{ tracker = "tracker name"
, progress =
Http.Receiving
{ received = 0
, size = Nothing
}
}
, Http.request
{ method = "GET"
, headers = []
, url = "https://elm-lang.org/assets/public-opinion.txt"
, body = Http.emptyBody
, expect = Http.expectString GotText
, timeout = Nothing
, tracker = Just "tracker name"
}
)
-- UPDATE
type Msg
= GotText (Result Http.Error String)
| Progress Http.Progress
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Progress progress ->
case model of
Loading state ->
( Loading { state | progress = progress }, Cmd.none )
Success _ ->
( model, Cmd.none )
Failure ->
( model, Cmd.none )
GotText result ->
case result of
Ok fullText ->
( Success fullText, Cmd.none )
Err _ ->
( Failure, Cmd.none )
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
case model of
Loading state ->
Http.track state.tracker Progress
Failure ->
Sub.none
Success _ ->
Sub.none
-- VIEW
view : Model -> Html Msg
view model =
case model of
Failure ->
text "I was unable to load your book."
Loading state ->
text
("Loading..."
++ (case state.progress of
Http.Receiving progress ->
String.fromFloat (Http.fractionReceived progress) ++ "%"
Http.Sending _ ->
""
)
)
Success fullText ->
pre [] [ text fullText ]