This repository has been archived by the owner on Feb 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.elm
161 lines (115 loc) · 3.39 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
module Main exposing (main)
import Dict exposing (Dict)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Http
import Json.Decode as Decode
import Random
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- MODEL
type alias IndicatorDatum =
{ countryId : String
, countryName : String
, year : String
, value : Maybe Float
}
type alias Model =
{ currentIdx : Maybe Int
, currentIndicatorName : String
, data : List IndicatorDatum
}
indicatorIds : Dict Int String
indicatorIds =
Dict.fromList [ ( 0, "EG.FEC.RNEW.ZS" ), ( 1, "EG.ELC.PETR.ZS" ), ( 2, "EG.ELC.ACCS.RU.ZS" ) ]
init : ( Model, Cmd Msg )
init =
( { currentIdx = Nothing
, currentIndicatorName = ""
, data = []
}
, Random.generate NewIndex (Random.int 0 2)
)
-- UPDATE
type Msg
= RequestData
| NewData (Result Http.Error (List IndicatorDatum))
| NewIndex Int
| GenerateRandomIdx
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
RequestData ->
( model, Random.generate NewIndex (Random.int 0 2) )
NewData (Ok data) ->
( { model | data = data }, Cmd.none )
NewData (Err error) ->
let
_ =
Debug.log "error" error
in
( model, Cmd.none )
GenerateRandomIdx ->
( model, Random.generate NewIndex (Random.int 0 2) )
NewIndex index ->
let
indicator =
indicatorIds |> Dict.get index |> Maybe.withDefault ""
in
( { model | currentIdx = Just index }, getData indicator )
-- VIEW
view : Model -> Html Msg
view model =
let
stringValue : Maybe Float -> String
stringValue value =
case value of
Just value ->
toString value
Nothing ->
"-"
currentIdx =
Maybe.withDefault 0 model.currentIdx
in
div []
[ h2 [] [ text (indicatorIds |> Dict.get currentIdx |> Maybe.withDefault "") ]
, button [ onClick GenerateRandomIdx ] [ text "Request Data From a random indicator!" ]
, div [ class "data" ]
[ ul [] <|
List.map
(\d ->
li [] [ text <| d.countryName ++ ": " ++ stringValue d.value ]
)
model.data
]
]
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
-- HTTP
getData : String -> Cmd Msg
getData indicator =
let
url =
"https://api.worldbank.org/v2/countries/all/indicators/"
++ indicator
++ "?date=2002:2002&format=json&per_page=500"
in
Http.send NewData (Http.get url decodeResponse)
decodeIndicator : Decode.Decoder IndicatorDatum
decodeIndicator =
Decode.map4 IndicatorDatum
(Decode.at [ "country", "id" ] Decode.string)
(Decode.at [ "country", "value" ] Decode.string)
(Decode.field "date" Decode.string)
(Decode.field "value" <| Decode.maybe Decode.float)
decodeResponse : Decode.Decoder (List IndicatorDatum)
decodeResponse =
Decode.index 1 (Decode.list decodeIndicator)