This repository was 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
Expand file tree
/
Copy pathMainTODO.elm
More file actions
114 lines (74 loc) · 1.98 KB
/
MainTODO.elm
File metadata and controls
114 lines (74 loc) · 1.98 KB
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
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 = []
}
, Cmd.none
)
-- UPDATE
type Msg
= NoOp (Result Http.Error (List IndicatorDatum))
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
NoOp _ ->
( model, Cmd.none )
-- VIEW
view : Model -> Html Msg
view model =
div [] [ text "TODO" ]
-- 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 NoOp (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)