Last active
March 30, 2016 20:32
-
-
Save fredcy/2dcabfe4df562e83c5154b107d0607fa to your computer and use it in GitHub Desktop.
Elm program that gets user info from Github
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 User)) | |
| NoOp | |
init : (Users, Effects Action) | |
init = | |
( usersModel, | |
getUsers | |
) | |
update : Action -> Users -> (Users, Effects Action) | |
update action model = | |
case action |> Debug.log "action" of | |
FetchAllDone result -> | |
case result of | |
Ok users -> | |
let | |
newUsers = List.append model.users users | |
newModel = Users newUsers | |
in | |
(newModel, Effects.none) | |
Err error -> | |
( model, Effects.none ) | |
NoOp -> | |
( model, Effects.none ) | |
view : Signal.Address Action -> Users -> Html | |
view address model = | |
div [] | |
[ | |
div [] [ text <| toString model ] | |
] | |
getUsers : Effects Action | |
getUsers = | |
Http.get decodeUsers "https://api.github.com/users" | |
|> Task.toResult | |
|> Task.map FetchAllDone | |
|> Effects.task | |
userDecoder : Decode.Decoder User | |
userDecoder = | |
Decode.object1 | |
User | |
( "login" := Decode.string ) | |
decodeUsers : Decode.Decoder (List User) | |
decodeUsers = | |
Decode.list userDecoder | |
app : StartApp.App Users | |
app = | |
StartApp.start | |
{ init = init, | |
view = view, | |
update = update, | |
inputs = [] | |
} | |
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