Last active
March 30, 2016 19:19
-
-
Save dfcarpenter/cef25f969b5cef8c0e8e0a3254236d19 to your computer and use it in GitHub Desktop.
Github users
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
module Main (..) where | |
import Effects exposing (Effects, Never) | |
import Html exposing (..) | |
import Html.Attributes exposing (style) | |
import Html.Events exposing (onClick) | |
import Http | |
import Json.Decode as Decode exposing ((:=)) | |
import Json.Decode.Extra exposing ((|:)) | |
import Task | |
import List exposing (..) | |
import StartApp | |
type alias Users = | |
{ | |
users : List User | |
} | |
type alias User = | |
{ | |
name : String | |
} | |
userModel : User | |
userModel = | |
{ | |
name = "" | |
} | |
usersModel : Users | |
usersModel = | |
{ | |
users = [] | |
} | |
type Action = | |
FetchAllDone (Result Http.Error List) | |
| NoOp | |
init : (Users, Effects Action) | |
init = | |
( usersModel, | |
getUsers | |
) | |
update : Users -> Action -> (Users, Effects Action) | |
update model action model = | |
case action of | |
FetchAllDone result -> | |
case result of | |
Ok users -> | |
let | |
newModel = | |
Users (List.map2 (+) model.users users) | |
in | |
(newModel, Effects.none) | |
Err error -> | |
( model.users, Effects.none ) | |
NoOp -> | |
( model.users, Effects.none ) | |
view : Signal.Address Action -> Users -> Html | |
view address model = | |
div [] | |
[ | |
div [] [ text "No idea"] | |
] | |
getUsers : Effects Action | |
getUsers = | |
Http.get decodeUsers "https://api.github.com/users" | |
|> Task.toResult | |
|> Task.map FetchAllDone | |
|> Effects.task | |
flowDecoder : Decode.Decoder User | |
flowDecoder = | |
Decode.object1 | |
User | |
( "name" := Decode.string ) | |
decodeUsers : Decode.Decoder Users | |
decodeUsers = | |
Decode.list User | |
app = | |
StartApp.start | |
{ init = getUsers, | |
inputs = [], | |
update = update, | |
view = view | |
} | |
main : Signal Html | |
main = | |
app.html | |
port tasks : Signal (Task.Task Never ()) | |
port tasks = | |
app.tasks | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment