about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@gmail.com>2017-12-19T17·26+0100
committerVincent Ambo <tazjin@gmail.com>2017-12-20T19·15+0100
commit26f365271e00a87374f4b6cc3877e3896c1b1425 (patch)
treee3eba13e4214d05e888a8cde166d9a2ee64fb42f
parent95e4971908574f14f6a1baa6b463aa1e7bc2b281 (diff)
feat(elm): Add initial logic for Gemma's Elm frontend
-rw-r--r--frontend/Main.elm101
-rw-r--r--frontend/elm-package.json16
2 files changed, 117 insertions, 0 deletions
diff --git a/frontend/Main.elm b/frontend/Main.elm
new file mode 100644
index 000000000000..c977e890ae95
--- /dev/null
+++ b/frontend/Main.elm
@@ -0,0 +1,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
+        }
diff --git a/frontend/elm-package.json b/frontend/elm-package.json
new file mode 100644
index 000000000000..e0c71aadfdc6
--- /dev/null
+++ b/frontend/elm-package.json
@@ -0,0 +1,16 @@
+{
+    "version": "1.0.0",
+    "summary": "helpful summary of your project, less than 80 characters",
+    "repository": "https://github.com/user/project.git",
+    "license": "BSD3",
+    "source-directories": [
+        "."
+    ],
+    "exposed-modules": [],
+    "dependencies": {
+        "elm-lang/core": "5.1.1 <= v < 6.0.0",
+        "elm-lang/html": "2.0.0 <= v < 3.0.0",
+        "elm-lang/http": "1.0.0 <= v < 2.0.0"
+    },
+    "elm-version": "0.18.0 <= v < 0.19.0"
+}