-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cats.elm
214 lines (145 loc) · 4.96 KB
/
Cats.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
module Cats exposing (..)
import Html exposing (div, h1, button, li, ul, text, Html, span, h2, img)
import Html.Attributes exposing (hidden, src, height)
import Html.Events exposing (onClick)
import Http
import Json.Decode as Decode
import Task
import List exposing (map, length, range)
-- EDIT: see http://package.elm-lang.org/packages/NoRedInk/elm-task-extra/2.0.0/Task-Extra#parallel
-- Achieves parallelism by creating empty cat then filling it in.
-- stillLoading counter = number of requests,
-- start requests pointing to cat (index?)
-- when each request returns, it fills in part of cat and decrements counter
-- could skip the counter and check vals if you know they must all not be
-- if some requests are optional, they don't have to decrement.
-- pass updater function to update to run against cats. map over cats, find id, fill in value.
main : Program Never Model Msg
main =
Html.program
{ init = ( initialModal, Cmd.batch (List.indexedMap (\id _ -> getCat id) initialModal.cats) )
, view = view
, update = update
, subscriptions = \_ -> Sub.none
}
numCatsToStart : Int
numCatsToStart =
2
-- Redux state = Elm model.
type alias Model =
{ flash : String
, cats : List Cat
}
initialModal : Model
initialModal =
{ flash = "Initializing…"
, cats = range 0 (numCatsToStart - 1) |> map (createCat numSubRequests)
}
type alias Cat =
{ id : Int, stillLoading : Int, fact : String, pic : String }
-- Redux actions = Elm Msg. Types specifiy actions and their payloads (if any).
type Msg
= Flash String
| RequestParallelCat
| ReceivePartialCat (Cat -> Cat)
| FetchCatFail Http.Error
-- Redux root reducer = Elm update. Note Elm runs side effects ¿BEFORE/AFTER?, called Commands)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Flash message ->
( { model | flash = message }, Cmd.none )
RequestParallelCat ->
( { model | cats = model.cats ++ [ createCat numSubRequests (nextIndex model.cats) ], flash = "Requesting parallel cat…" }, getCat (nextIndex model.cats) )
ReceivePartialCat catUpdater ->
( { model | cats = List.map catUpdater model.cats }, Cmd.none )
FetchCatFail error ->
( { model | flash = "fetch error" }, Cmd.none )
createCat : Int -> Int -> Cat
createCat requiredRequestCount id =
{ id = id, stillLoading = requiredRequestCount, fact = "", pic = "" }
nextIndex : List a -> Int
nextIndex list =
list
|> List.length
|> (+) 1
-- React render = Elm view.
view : Model -> Html Msg
view model =
div []
[ h1 [] [ text "Cats" ]
, h2 [] [ text model.flash ]
, button [ onClick (Flash "you flashed this") ] [ text "Flash a message" ]
, button [ onClick RequestParallelCat ] [ text "Add Cat" ]
, (renderCats model.cats)
]
renderCats : List Cat -> Html Msg
renderCats cats =
ul [] (List.map renderCat cats)
renderCat : Cat -> Html Msg
renderCat cat =
li [ hidden (cat.stillLoading > 0) ]
[ img [ src cat.pic, height 50 ] []
, span [] [ text cat.fact ]
]
-- HTTP
getCat : Int -> Cmd Msg
getCat catId =
Cmd.batch (map (\req -> req catId) subRequests)
-- Want to apply the id to each element but Elm has no apply.
numSubRequests : Int
numSubRequests =
length subRequests
subRequests : List (Int -> Cmd Msg)
subRequests =
[ getCatFact, getCatPic ]
getCatFact : Int -> Cmd Msg
getCatFact catId =
Task.attempt parseHttpResult
(Task.map
(\fact ->
(\cat ->
if cat.id == catId then
{ cat
| fact = fact
, stillLoading = cat.stillLoading - 1
}
else
cat
)
)
(Http.toTask (Http.get factUrl factDecoder))
)
getCatPic : Int -> Cmd Msg
getCatPic catId =
Task.attempt parseHttpResult
(Task.map
(\pic ->
(\cat ->
if cat.id == catId then
{ cat | pic = pic, stillLoading = cat.stillLoading - 1 }
else
cat
)
)
(Http.toTask (Http.get picUrl picDecoder))
)
parseHttpResult : Result Http.Error (Cat -> Cat) -> Msg
parseHttpResult result =
case result of
Ok catUpdater ->
ReceivePartialCat catUpdater
Err msg ->
FetchCatFail msg
factUrl : String
factUrl =
"https://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=cats"
factDecoder : Decode.Decoder String
factDecoder =
Decode.at [ "data", "id" ] Decode.string
picUrl : String
picUrl =
"https://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=cats"
picDecoder : Decode.Decoder String
picDecoder =
Decode.at [ "data", "image_url" ] Decode.string