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
|
module Main exposing (..)
import Html exposing (Html, text, div, span)
import Json.Decode exposing (..)
import Http
-- API interface to Gemma
type alias Task =
{ name : String
, description : Maybe String
, remaining : Int
}
emptyStringFilter s =
if s == "" then
Nothing
else
Just s
decodeEmptyString : Decoder (Maybe String)
decodeEmptyString =
map emptyStringFilter string
decodeTask : Decoder Task
decodeTask =
map3 Task
(field "name" string)
(field "description" decodeEmptyString)
(field "remaining" int)
loadTasks : Cmd Msg
loadTasks =
let
request =
Http.get "http://localhost:4242/tasks" (list decodeTask)
in
Http.send NewTasks request
-- Elm architecture implementation
type Msg
= None
| LoadTasks
| NewTasks (Result Http.Error (List Task))
type alias Model =
{ tasks : List Task
, error : Maybe String
}
renderTask : Task -> Html Msg
renderTask task =
div [] [ span [] [ text task.name ] ]
view : Model -> Html Msg
view model =
div [] (List.map renderTask model.tasks)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
LoadTasks ->
( model, loadTasks )
NewTasks (Ok tasks) ->
( { tasks = tasks, error = Nothing }, Cmd.none )
NewTasks (Err err) ->
( { model | error = Just (toString err) }, Cmd.none )
_ ->
( model, Cmd.none )
init : ( Model, Cmd Msg )
init =
( { tasks = [], error = Nothing }, loadTasks )
main : Program Never Model Msg
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = always Sub.none
}
|