-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomGif.elm
183 lines (152 loc) · 4.29 KB
/
RandomGif.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
module RandomGif exposing (init, update, view)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onClick, onSubmit, onInput)
import Task
import Http
import Json.Decode as Json
type alias Gif =
{ identifier : Int
, topic : String
, gifUrl : String
}
type alias Model =
{ gifs : List Gif
, topic : String
, nextIdentifier : Int
}
init : String -> ( Model, Cmd Msg )
init topic =
let
gif =
Gif 0 topic "assets/waiting.gif"
in
( Model [ gif ] "" 1
, (getRandomGif gif)
)
type Msg
= RequestMore Gif
| FetchSucceed Int String
| FetchFail Gif
| UpdateField String
| AddGif
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
RequestMore gif ->
let
updateGif thisGif =
if thisGif.identifier == gif.identifier then
{ thisGif | gifUrl = "assets/waiting.gif" }
else
thisGif
in
( { model | gifs = List.map updateGif model.gifs }
, getRandomGif gif
)
FetchSucceed identifier maybeUrl ->
let
updateGif thisGif =
if thisGif.identifier == identifier then
{ thisGif | gifUrl = maybeUrl }
else
thisGif
in
{ model
| gifs = List.map updateGif model.gifs
}
! []
FetchFail gif ->
( model
, getRandomGif gif
)
UpdateField str ->
{ model
| topic = str
}
! []
AddGif ->
let
getNewRandomGif =
getRandomGif
{ identifier = model.nextIdentifier
, topic = model.topic
, gifUrl = "assets/waiting.gif"
}
in
( { model
| gifs =
{ identifier = model.nextIdentifier
, topic = model.topic
, gifUrl = "assets/waiting.gif"
}
:: model.gifs
, topic = ""
, nextIdentifier = model.nextIdentifier + 1
}
, getNewRandomGif
)
(=>) =
(,)
view : Model -> Html Msg
view model =
div []
[ Html.form
[ style [ "width" => "200px" ]
, onSubmit AddGif
]
[ input
[ type' "text"
, placeholder "New Topic"
, value model.topic
, autofocus True
, onInput UpdateField
]
[]
, button [ type' "submit" ] [ text "More Please" ]
]
, div [ style [ "display" => "flex" ] ]
(List.map (gifView) model.gifs)
]
gifView : Gif -> Html Msg
gifView gif =
div [ style [ "width" => "200px" ] ]
[ h2 [ headerStyle ] [ text gif.topic ]
, div [ imgStyle gif.gifUrl ] []
, button [ onClick (RequestMore gif) ] [ text "More Please" ]
]
headerStyle : Attribute Msg
headerStyle =
style
[ "width" => "200px"
, "text-align" => "center"
]
imgStyle : String -> Attribute Msg
imgStyle url =
style
[ "display" => "inline-block"
, "width" => "200px"
, "height" => "200px"
, "background-position" => "center center"
, "background-size" => "cover"
, "background-image" => ("url('" ++ url ++ "')")
]
getRandomGif : Gif -> Cmd Msg
getRandomGif gif =
let
url =
randomUrl gif.topic
in
Task.perform
(\_ -> FetchFail gif)
(FetchSucceed gif.identifier)
(Http.get decodeGifUrl url)
randomUrl : String -> String
randomUrl topic =
Http.url "http://api.giphy.com/v1/gifs/random"
[ "api_key" => "dc6zaTOxFJmzC"
, "tag" => topic
]
decodeGifUrl : Json.Decoder String
decodeGifUrl =
Json.at [ "data", "image_url" ] Json.string