Skip to content

Instantly share code, notes, and snippets.

@hgwood
Forked from anonymous/Main.elm
Last active May 4, 2017 22:01
Show Gist options
  • Save hgwood/5296b047e2d7167c542fa0162968719e to your computer and use it in GitHub Desktop.
Save hgwood/5296b047e2d7167c542fa0162968719e to your computer and use it in GitHub Desktop.
Example Elm program that gets grants from the GitHub API on the click of a button
{
"version": "1.0.0",
"summary": "Tell the world about your project!",
"repository": "https://github.com/user/project.git",
"license": "BSD3",
"source-directories": [
"."
],
"exposed-modules": [],
"dependencies": {
"elm-lang/core": "5.1.1 <= v < 5.1.1",
"elm-lang/html": "2.0.0 <= v < 2.0.0",
"elm-lang/http": "1.0.0 <= v < 1.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}
<html>
<head>
<style>
html {
background: #F7F7F7;
color: red;
}
</style>
</head>
<body>
<script>
var app = Elm.Main.fullscreen()
</script>
</body>
</html>
module Main exposing (..)
import Html exposing (Html, text)
import Html.Events exposing (onClick)
import Json.Decode exposing (..)
import Http
main =
Html.program { init = init, subscriptions = subscriptions, view = view, update = update }
type alias Model =
List Int
init : ( Model, Cmd Msg )
init =
( [], Cmd.none )
type Msg
= Click
| GrantsArrived (Result Http.Error (List Int))
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Click ->
( model, getGrants )
GrantsArrived (Ok grants) ->
( grants, Cmd.none )
GrantsArrived (Err _) ->
( model, Cmd.none )
getGrants : Cmd Msg
getGrants =
let
url =
"https://api.github.com/applications/grants"
req =
Http.request
{ method = "GET"
, headers = [ Http.header "Authorization" "Basic ***" ]
, url = url
, body = Http.emptyBody
, expect = Http.expectJson decodeGrants
, timeout = Nothing
, withCredentials = False
}
in
Http.send GrantsArrived req
decodeGrants : Decoder (List Int)
decodeGrants =
list (field "id" int)
view : Model -> Html Msg
view model =
Html.div []
[ Html.button [ onClick Click ]
[ text "Hello, World!"
]
, text (String.join " " (List.map toString model))
]
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment