Skip to content

Instantly share code, notes, and snippets.

@napcs
Created March 22, 2016 04:17
Show Gist options
  • Save napcs/07a39d3258ef52f79e71 to your computer and use it in GitHub Desktop.
Save napcs/07a39d3258ef52f79e71 to your computer and use it in GitHub Desktop.
EFP 47
{
"message": "success",
"number": 6,
"people": [
{
"craft": "ISS",
"name": "Yuri Malenchenko"
},
{
"craft": "ISS",
"name": "Timothy Kopra"
},
{
"craft": "ISS",
"name": "Timothy Peake"
},
{
"craft": "ISS",
"name": "Alexey Ovchinin"
},
{
"craft": "ISS",
"name": "Oleg Skripochka"
},
{
"craft": "ISS",
"name": "Jeff Williams"
}
]
}
module Space where
import Html exposing(Html, div, li, ul, p, h1, text)
import Html.Attributes exposing(class, id)
import Html.Events exposing(onClick)
import StartApp
import Effects exposing (Effects, Never)
import Task exposing (Task)
import Http
import Json.Decode as Json exposing ((:=))
-- TYPES
type alias Person =
{ name : String
, craft : String
}
type alias Model =
List Person
type Action = SetPeople (Maybe Model)
-- MODEL
init : (Model, Effects Action)
init =
([], fetchPeople)
-- Main
main : Signal Html
main =
app.html
-- StartApp
app =
StartApp.start
{ init = init
, update = update
, view = view
, inputs = []
}
-- PORTS
port tasks : Signal (Task Never ())
port tasks =
app.tasks
-- VIEW
view : Signal.Address Action -> Model -> Html
view address model =
ul [ class "person" ] (List.map (resultItem address) model)
resultItem : Signal.Address Action -> Person -> Html
resultItem address person =
li [] [
p [] [text ( person.name) ],
p [] [text ( person.craft) ]
]
-- UPDATE
update : Action -> Model -> (Model, Effects Action)
update action model =
case action of
SetPeople people ->
let
newModel = Maybe.withDefault model people
in
(newModel, Effects.none)
-- HTTP
fetchPeople: Effects Action
fetchPeople =
Http.get decodePeople "http://localhost:4242/api/astros.json"
|> Task.toMaybe
|> Task.map SetPeople
|> Effects.task
decodePeople: Json.Decoder Model
decodePeople =
let
person =
Json.object2 (\name craft -> (Person name craft))
("name" := Json.string)
("craft" := Json.string)
in
Json.at ["people"] (Json.list person)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment