Created
May 25, 2016 16:08
-
-
Save rohanorton/1b2a5e198c2d6d08d98262064ec8d8f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Html exposing (..) | |
import Html.App as Html | |
import Html.Attributes exposing (..) | |
import Html.Events exposing (..) | |
import Http | |
import Json.Decode as Json | |
import String exposing (..) | |
import Task | |
main = | |
Html.program | |
{ init = init "cats" | |
, view = view | |
, update = update | |
, subscriptions = subscriptions | |
} | |
-- MODEL | |
type alias Model = | |
{ topic : String | |
, gifUrl : String | |
, error: String | |
} | |
init : String -> (Model, Cmd Msg) | |
init topic = | |
(Model topic "waiting.gif" "", Cmd.none) | |
-- UPDATE | |
type Msg | |
= MorePlease | |
| FetchSucceed String | |
| FetchFail Http.Error | |
| Topic String | |
update : Msg -> Model -> (Model, Cmd Msg) | |
update msg model = | |
case msg of | |
MorePlease -> | |
-- (model, Cmd.none) | |
(model, getRandomGif model.topic) | |
FetchSucceed newUrl -> | |
(Model model.topic newUrl "", Cmd.none) | |
FetchFail httpErr -> | |
-- { model | error = errorMapper httpErr} | |
({ model | error = errorMapper httpErr }, Cmd.none) | |
Topic topic -> | |
({ model | topic = topic }, Cmd.none) | |
getRandomGif : String -> Cmd Msg | |
getRandomGif topic = | |
let | |
url = | |
"http://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=" ++ topic | |
in | |
Task.perform FetchFail FetchSucceed (Http.get decodeGifUrl url) | |
decodeGifUrl : Json.Decoder String | |
decodeGifUrl = | |
Json.at ["data", "image_url"] Json.string | |
errorMapper : Http.Error -> String | |
errorMapper err = | |
case err of | |
Http.Timeout -> "Http request timed out" | |
Http.NetworkError -> "Network error" | |
Http.UnexpectedPayload s -> s | |
Http.BadResponse status s -> "Bad response: " ++ toString status ++ s | |
-- VIEW | |
view : Model -> Html Msg | |
view model = | |
div [] | |
[ h2 [] [text model.topic] | |
, select [ on "change" (Json.map Topic targetValue) ] | |
[ option [ ] [ text "Cats" ] | |
, option [ ] [ text "Dogs" ] | |
] | |
-- , input [ type' "text", placeholder "Topic", onInput Topic ] [] | |
, button [ onClick MorePlease ] [ text "More Please!" ] | |
, br [] [] | |
, img [src model.gifUrl] [] | |
, viewError model | |
] | |
viewError : Model -> Html msg | |
viewError model = | |
if not (isEmpty model.error) then | |
div [ style [("color", "red")] ] [ text model.error ] | |
else | |
div [] [] | |
-- SUBSCRIPTIONS | |
subscriptions : Model -> Sub Msg | |
subscriptions model = | |
Sub.none |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment