Skip to content

Instantly share code, notes, and snippets.

@avdgaag
Created May 29, 2016 09:23
Show Gist options
  • Save avdgaag/843b242408f77f9a5d1b86e4b738b7be to your computer and use it in GitHub Desktop.
Save avdgaag/843b242408f77f9a5d1b86e4b738b7be to your computer and use it in GitHub Desktop.
import Html.App as App
import Html exposing (..)
import Html.Attributes exposing (..)
import Http
import Task
import Json.Decode as Json exposing ((:=))
-- MODEL
type alias Model =
{ username : String
, images : List (String, String)
}
-- UPDATE
type Msg
= NoOp
| FetchFail Http.Error
| FetchSucceed Model
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
NoOp ->
(model, Cmd.none)
FetchFail _ ->
(model, Cmd.none)
FetchSucceed m ->
(m, Cmd.none)
-- HTTP
getRandomAvatar : Cmd Msg
getRandomAvatar =
Task.perform FetchFail FetchSucceed (Http.get decodeAvatar "http://uifaces.com/api/v1/random")
decodeAvatar : Json.Decoder Model
decodeAvatar =
Json.object2 Model
("username" := Json.string)
("image_urls" := Json.keyValuePairs Json.string)
-- VIEW
view : Model -> Html Msg
view model =
div
[]
[ h2 [] [ text model.username ]
, div [] (List.map viewAvatar model.images)
]
viewAvatar : (String, String) -> Html Msg
viewAvatar (name, url) =
img [ src url, alt name ] []
-- WIRING
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
init : (Model, Cmd Msg)
init =
({username = "", images = []}, getRandomAvatar)
main : Program Never
main =
App.program
{ subscriptions = subscriptions
, init = init
, update = update
, view = view
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment