-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.elm
147 lines (100 loc) · 3.01 KB
/
App.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
module MyApp (..) where
import Html exposing (..)
import Html.Events exposing (onClick, on, targetValue)
import Html.Attributes exposing (disabled, value)
import String
import Http
import Json.Decode as JD exposing ((:=), Decoder)
import Json.Decode.Extra exposing ((|:))
import Graphics.Element exposing (..)
import Task exposing (Task, andThen, onError)
import Debug
type alias Model =
{ person : Person
, input : String
, error : String
}
type Action
= NewGithubPerson Person
| GetGithubData
| UpdateInput String
| HttpError String
| NoOp
actions : Signal.Mailbox Action
actions =
Signal.mailbox NoOp
initialModel : Model
initialModel =
{ person = nullPerson
, input = "jackfranklin"
, error = ""
}
type alias Person =
{ login : String, reposCount : Int }
nullPerson : Person
nullPerson =
{ login = "", reposCount = 0 }
updateData : Person -> Task x ()
updateData person =
Signal.send actions.address (NewGithubPerson person)
errorMessage : Http.Error -> String
errorMessage error =
case error of
Http.Timeout ->
"Timeout"
Http.NetworkError ->
"Network error"
Http.UnexpectedPayload _ ->
"Error decoding JSON"
Http.BadResponse code str ->
"Error: " ++ (toString code) ++ " " ++ str
httpError : Http.Error -> Task () ()
httpError error =
Signal.send actions.address (HttpError (errorMessage error))
getGithubData : String -> Task () ()
getGithubData name =
(Http.get jsonToPerson ("https://api.github.com/users/" ++ name)) `andThen` updateData `onError` httpError
update : Action -> ( Model, Task () () ) -> ( Model, Task () () )
update action ( model, _ ) =
case action of
NoOp ->
( model, Task.succeed () )
HttpError str ->
( { model | error = str }, Task.succeed () )
UpdateInput string ->
( { model | input = string }, Task.succeed () )
GetGithubData ->
( model, getGithubData model.input )
NewGithubPerson person ->
( { model | person = person }, Task.succeed () )
jsonToPerson : Decoder Person
jsonToPerson =
JD.succeed Person
|: ("login" := JD.string)
|: ("public_repos" := JD.int)
view : Signal.Address Action -> Model -> Html
view address model =
div
[]
[ button [ onClick address GetGithubData, disabled (String.isEmpty model.input) ] [ text "Go!" ]
, input [ on "input" targetValue (\str -> Signal.message address (UpdateInput str)), value model.input ] []
, div [] [ text model.person.login ]
, div [] [ text ("Repo Count: " ++ (toString model.person.reposCount)) ]
, div [] [ text model.error ]
]
-- SIGNALS AND PORTS
modelAndTask : Signal ( Model, Task () () )
modelAndTask =
Signal.foldp update ( initialModel, getGithubData initialModel.input ) actions.signal
taskSignal : Signal (Task () ())
taskSignal =
Signal.map snd modelAndTask
modelSignal : Signal Model
modelSignal =
Signal.map fst modelAndTask
port tasks : Signal (Task () ())
port tasks =
taskSignal
main : Signal Html
main =
Signal.map (view actions.address) modelSignal