Skip to content

Commit

Permalink
handle sending input + clean up Update
Browse files Browse the repository at this point in the history
  • Loading branch information
AnotherKamila committed Mar 13, 2018
1 parent a641c4b commit 2bb97dc
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
1 change: 1 addition & 0 deletions webui/elm-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
],
"exposed-modules": [],
"dependencies": {
"NoRedInk/elm-decode-pipeline": "3.0.0 <= v < 4.0.0",
"debois/elm-mdl": "8.1.0 <= v <= 8.1.0",
"elm-community/list-extra": "7.1.0 <= v <= 7.1.0",
"elm-community/result-extra": "2.2.0 <= v < 3.0.0",
Expand Down
12 changes: 12 additions & 0 deletions webui/src/CsvTsdb/Api.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module CsvTsdb.Api exposing (..)

import Http
import Json.Decode as Decode
import Json.Decode.Pipeline as Decode
import Result exposing (mapError, andThen)

import Config
Expand All @@ -10,6 +12,16 @@ import Model exposing (Msg(NewData)) -- TODO remove

track_url = Config.api_url ++ "track/"

decode_error : Decode.Decoder String
decode_error = Decode.decode
(\s -> if String.isEmpty s then Decode.succeed "42" else Decode.fail s)
|> Decode.optional "error" (Decode.string) ""
|> Decode.resolve

get_records msg =
Http.getString track_url
|> Http.send (mapError toString >> andThen decode_records >> msg)

send_input : String -> Http.Request String
send_input input =
Http.post track_url (Http.stringBody "text/plain" input) decode_error
3 changes: 3 additions & 0 deletions webui/src/Model.elm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Model exposing (Model, Tab(..), Msg(..), init)

import Http
import Material
import Navigation
import CsvTsdb.Model as CsvTsdb exposing (default_record) -- TODO remove exposing
Expand All @@ -21,9 +22,11 @@ type alias Model =

type Msg = SelectTab Tab
| NewData (Result String (List CsvTsdb.Record))
| RequestDone (Result Http.Error String)
| RefreshWanted
| DataInput String
| SliderInput Float
| DataSubmit
-- Boilerplate
| Mdl (Material.Msg Msg) -- internal Mdl messages

Expand Down
4 changes: 3 additions & 1 deletion webui/src/TrackView.elm
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ input model =
, Options.onInput DataInput -- TODO handle enter key
] []
, Button.render Mdl [1,1] model.mdl
[ Button.icon, Button.ripple, Button.colored ]
[ Button.icon, Button.ripple, Button.colored
, Options.onClick DataSubmit
]
[ Icon.i "send" ]
, div [ css "margin-left" "-3px"] (model.recent_labels |> List.map chip)
, Slider.view
Expand Down
31 changes: 21 additions & 10 deletions webui/src/Update.elm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Update exposing (update, init)

import Http
import Material
import List.Extra as List
import Result.Extra as Result
Expand All @@ -10,16 +11,17 @@ import CsvTsdb.Api as CsvTsdb
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
-- TODO handle errors :D
SelectTab new -> ({model | tab = new}, Cmd.none)
NewData (Ok new) -> ({model | data = new, recent_labels = find_labels new}, Cmd.none) -- TODO this will be a separate query one day
NewData (Err e) -> ({model | err = e}, Cmd.none)
DataInput s -> ({model | data_input = s}, Cmd.none)
SliderInput x -> ({model | data_input = add_or_replace_value model.data_input x, slider_input = x}, Cmd.none)
RefreshWanted -> (model, refresh)
Mdl m -> Material.update Mdl m model -- Mdl action handler

find_labels = List.map .label >> List.unique
SelectTab new -> ({model | tab = new}, Cmd.none)
NewData r -> check r update_data Cmd.none model
RequestDone r -> check r (\_ -> identity) Cmd.none model
DataInput s -> ({model | data_input = s}, Cmd.none)
SliderInput x -> ({model | data_input = add_or_replace_value model.data_input x, slider_input = x}, Cmd.none)
RefreshWanted -> (model, refresh)
DataSubmit -> ({model | data_input = ""}, send_input model.data_input)
Mdl m -> Material.update Mdl m model -- Mdl action handler

update_data new model = {model | data = new, recent_labels = find_labels new}
find_labels = List.map .label >> List.unique >> List.take 10 -- Note: recent labels will be a separate query one day

add_or_replace_value : String -> Float -> String
add_or_replace_value s v =
Expand All @@ -30,8 +32,17 @@ add_or_replace_value s v =
else ws
in newws++[toString v] |> String.join " "

check : Result e a -> (a -> Model -> Model) -> Cmd Msg -> Model -> (Model, Cmd Msg)
check result f c model =
case result of
Err e -> ({model | err = toString e}, Cmd.none)
Ok a -> (f a model, c)

-- COMMANDS --

send_input : String -> Cmd Msg
send_input = CsvTsdb.send_input >> Http.send RequestDone

refresh : Cmd Msg
refresh = CsvTsdb.get_records NewData

Expand Down

0 comments on commit 2bb97dc

Please sign in to comment.