about summary refs log tree commit diff
path: root/fun/gemma/frontend/Main.elm
blob: e449908e499cf83b623cf48df027fc2b13606a6b (plain) (blame)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
-- Copyright (C) 2016-2017  Vincent Ambo <mail@tazj.in>
--
-- This file is part of Gemma.
--
-- Gemma is free software: you can redistribute it and/or modify it
-- under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.


module Main exposing (..)

import Html exposing (Html, text, div, span)
import Html.Attributes exposing (style)
import Json.Decode exposing (..)
import Http
import Time


--  Material design imports

import Material
import Material.Card as Card
import Material.Color as Color
import Material.Grid exposing (grid, cell, size, Device(..))
import Material.Layout as Layout
import Material.Scheme as Scheme
import Material.Options as Options
import Material.Elevation as Elevation
import Material.Button as Button


-- 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 "/tasks" (list decodeTask)
    in
        Http.send NewTasks request


completeTask : Task -> Cmd Msg
completeTask task =
    let
        request =
            Http.getString
                (String.concat
                    [ "/complete?task="
                    , task.name
                    ]
                )
    in
        Http.send (\_ -> LoadTasks) request



-- Elm architecture implementation


type Msg
    = None
    | LoadTasks
    | NewTasks (Result Http.Error (List Task))
    | Mdl (Material.Msg Msg)
    | Complete Task


type alias Model =
    { tasks : List Task
    , error : Maybe String
    , mdl : Material.Model
    }


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        LoadTasks ->
            ( model, loadTasks )

        Complete task ->
            ( model, completeTask task )

        NewTasks (Ok tasks) ->
            ( { model | tasks = tasks, error = Nothing }, Cmd.none )

        NewTasks (Err err) ->
            ( { model | error = Just (toString err) }, Cmd.none )

        _ ->
            ( model, Cmd.none )



-- View implementation


white =
    Color.text Color.white


taskColor : Task -> Color.Hue
taskColor task =
    if task.remaining > 2 then
        Color.Green
    else if task.remaining < 0 then
        Color.Red
    else
        Color.Yellow


within : Task -> String
within task =
    if task.remaining < 0 then
        "This task is overdue!"
    else if task.remaining > 2 then
        String.concat
            [ "Relax, this task has "
            , toString task.remaining
            , " days left before it is due."
            ]
    else
        String.concat
            [ "This task should be completed within "
            , toString task.remaining
            , " days. Consider doing it now!"
            ]


renderTask : Model -> Task -> Html Msg
renderTask model task =
    Card.view
        [ Color.background (Color.color (taskColor task) Color.S800)
        , Elevation.e3
        ]
        [ Card.title [] [ Card.head [ white ] [ text task.name ] ]
        , Card.text [ white ]
            [ text (Maybe.withDefault "" task.description)
            , Html.br [] []
            , text (within task)
            ]
        , Card.actions
            [ Card.border ]
            [ Button.render Mdl
                [ 0 ]
                model.mdl
                [ white, Button.ripple, Button.accent, Options.onClick (Complete task) ]
                [ text "Completed" ]
            ]
        ]


gemmaView : Model -> Html Msg
gemmaView model =
    grid []
        (List.map (\t -> cell [ size All 4 ] [ renderTask model t ])
            model.tasks
        )


view : Model -> Html Msg
view model =
    gemmaView model |> Scheme.top



-- subscriptions : Model -> Sub Msg


subscriptions model =
    Sub.batch
        [ Material.subscriptions Mdl model
        , Time.every (15 * Time.second) (\_ -> LoadTasks)
        ]


main : Program Never Model Msg
main =
    let
        model =
            { tasks = []
            , error = Nothing
            , mdl = Material.model
            }
    in
        Html.program
            { init = ( model, Cmd.batch [ loadTasks, Material.init Mdl ] )
            , view = view
            , update = update
            , subscriptions = subscriptions
            }