forked from rtfeldman/elm-workshop
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Main.elm
97 lines (75 loc) · 2.63 KB
/
Main.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
module Main exposing (..)
{-| THIS FILE IS NOT PART OF THE WORKSHOP! It is only to verify that you
have everything set up properly.
-}
import Html exposing (..)
import Html.Attributes exposing (..)
import Auth
import Http
import Json.Decode exposing (Decoder)
main : Program Never Model Msg
main =
Html.program
{ view = view
, update = update
, init = ( initialModel, searchFeed )
, subscriptions = \_ -> Sub.none
}
initialModel : Model
initialModel =
{ status = "Verifying setup..."
}
type alias Model =
{ status : String }
searchFeed : Cmd Msg
searchFeed =
let
url =
"https://api.github.com/search/repositories?q=test&access_token=" ++ Auth.token
in
Json.Decode.succeed ()
|> Http.get url
|> Http.send Response
view : Model -> Html Msg
view model =
div [ class "content" ]
[ header [] [ h1 [] [ text "Elm Workshop" ] ]
, div
[ style
[ ( "font-size", "48px" )
, ( "text-align", "center" )
, ( "padding", "48px" )
]
]
[ text model.status ]
]
type Msg
= Response (Result Http.Error ())
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Response (Ok ()) ->
( { status = "You're all set!" }, Cmd.none )
Response (Err err) ->
let
status =
case err of
Http.Timeout ->
"Timed out trying to contact GitHub. Check your Internet connection?"
Http.NetworkError ->
"Network error. Check your Internet connection?"
Http.BadUrl url ->
"Invalid test URL: " ++ url
Http.BadPayload msg _ ->
"Something is misconfigured: " ++ msg
Http.BadStatus { status } ->
case status.code of
401 ->
"Auth.elm does not have a valid token. :( Try recreating Auth.elm by following the steps in the README under the section “Create a GitHub Personal Access Token”."
_ ->
"GitHub's Search API returned an error: "
++ (toString status.code)
++ " "
++ status.message
in
( { status = status }, Cmd.none )