Skip to content

Instantly share code, notes, and snippets.

@gaku-sei
Last active October 4, 2016 08:07
Show Gist options
  • Save gaku-sei/7fbea5ed11e1c36905adbf2c346fcca6 to your computer and use it in GitHub Desktop.
Save gaku-sei/7fbea5ed11e1c36905adbf2c346fcca6 to your computer and use it in GitHub Desktop.
Big list with request example
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Html.App exposing (..)
import Json.Decode exposing (..)
import Http
import Task
type alias Photo =
{ albumId : Int
, id : Int
, title : String
, url : String
, thumbnailUrl : String }
type alias Model =
List Photo
type Msg = MorePlease
| FetchSucceed (List Photo)
| FetchFail Http.Error
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
MorePlease ->
(model, getAllPhotos)
FetchSucceed newModel ->
(newModel, Cmd.none)
FetchFail _ ->
(model, Cmd.none)
getAllPhotos : Cmd Msg
getAllPhotos =
let
url = "https://jsonplaceholder.typicode.com/photos"
in
Task.perform FetchFail FetchSucceed (Http.get decodePhotos url)
decodePhotos : Decoder (List Photo)
decodePhotos =
Json.Decode.list (object5 Photo
("albumId" := int)
("id" := int)
("title" := string)
("url" := string)
("thumbnailUrl" := string))
view : Model -> Html Msg
view model =
case model of
[] -> button [ onClick MorePlease ] [ text "go!" ]
photos -> photos |> List.map renderPhoto |> div []
renderPhoto : Photo -> Html Msg
renderPhoto photo =
div []
[ h2 [] [ text photo.title ]
, img [ src photo.thumbnailUrl ] [] ]
main = program
{ init = ([], Cmd.none)
, update = update
, subscriptions = \_ -> Sub.none
, view = view }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment