Commit 29f932e0 authored by André Espaze's avatar André Espaze
Browse files

Add delete action

parent eb2a6118f4e0
......@@ -12,6 +12,7 @@
"elm/http": "2.0.0",
"elm/json": "1.1.3",
"elm/time": "1.0.0",
"elm/url": "1.0.0",
"justgage/tachyons-elm": "4.1.3",
"rtfeldman/elm-css": "16.0.1"
},
......@@ -19,7 +20,6 @@
"Skinney/murmur3": "2.0.8",
"elm/bytes": "1.0.8",
"elm/file": "1.0.5",
"elm/url": "1.0.0",
"elm/virtual-dom": "1.0.2",
"rtfeldman/elm-hex": "1.0.0"
}
......@@ -32,4 +32,4 @@
"elm/random": "1.0.0"
}
}
}
}
\ No newline at end of file
module ItemSelector exposing (view)
module ItemSelector exposing (Config, Context, view)
import Common exposing (classes)
import Html.Styled exposing (..)
......@@ -7,9 +7,30 @@ import Html.Styled.Events exposing (onMouseDown)
import Tachyons.Classes as T
view : (String -> msg) -> List String -> List String -> Html msg
view toMsg items selectedItems =
type alias Config msg =
{ action :
Maybe
{ attrs : List (Attribute msg)
, html : Html msg
, clickMsg : msg
}
, defaultText : Html msg
, toggleMsg : String -> msg
}
type alias Context =
{ items : List String
, selectedItems : List String
}
view : Config msg -> Context -> Html msg
view cfg ctx =
let
enabled =
List.length ctx.items > 0
ulClass =
classes [ T.list, T.pl0, T.ml0, T.w_100, T.ba, T.b__light_silver, T.br3 ]
......@@ -21,16 +42,34 @@ view toMsg items selectedItems =
liSelected =
let
isSelected =
List.member item selectedItems
List.member item ctx.selectedItems
in
classList <|
List.map
(\x -> ( x, isSelected ))
[ T.white, T.bg_blue ]
in
[ liClass, liSelected, onMouseDown <| toMsg item ]
[ liClass, liSelected, onMouseDown <| cfg.toggleMsg item ]
lst =
ul [ ulClass ] <|
List.map
(\x -> li (liAttrs x) [ text x ])
ctx.items
lstWithAction act =
let
aClass =
classes
[ T.w_100, T.link, T.dim, T.ph3, T.pv2, T.ma2, T.dib, T.tc ]
attrs =
[ aClass, onMouseDown act.clickMsg ] ++ act.attrs
in
div [ classes [ T.dt, T.dt__fixed ] ] [ a attrs [ act.html ], lst ]
in
ul [ ulClass ] <|
List.map
(\x -> li (liAttrs x) [ text x ])
items
if enabled then
Maybe.map lstWithAction cfg.action |> Maybe.withDefault lst
else
div [ classes [ T.pl0, T.ml0 ] ] [ cfg.defaultText ]
......@@ -24,6 +24,7 @@ import Json.Decode as Decode
import KeywordSelector
import Tachyons.Classes as T
import Time
import Url.Builder as UB
type alias Model =
......@@ -44,14 +45,19 @@ type Msg
| ToggleItem String
| SearchSeries String
| MakeSearch
| OnDelete
| DeleteDone (Result Http.Error String)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
let
removeItem x xs =
List.filter ((/=) x) xs
toggleItem x xs =
if List.member x xs then
List.filter ((/=) x) xs
removeItem x xs
else
x :: xs
......@@ -65,6 +71,17 @@ update msg model =
else
KeywordSelector.select xm xs |> List.take 20
delete expect url =
Http.request
{ method = "DELETE"
, headers = []
, url = url
, body = Http.emptyBody
, expect = expect
, timeout = Nothing
, tracker = Nothing
}
in
case msg of
CatalogReceived (Ok x) ->
......@@ -86,6 +103,58 @@ update msg model =
MakeSearch ->
newModel { model | searchedSeries = keywordMatch model.searchString model.series }
OnDelete ->
let
expect =
Http.expectJson DeleteDone Decode.string
mkUrl serieName =
UB.crossOrigin "http://tshistory.test.pythonian.fr"
[ "series", "state" ]
[ UB.string "name" serieName ]
in
( model, Cmd.batch <| List.map (mkUrl >> delete expect) model.selectedSeries )
DeleteDone (Ok x) ->
let
_ =
Debug.log "Result on DeleteDone" x
in
newModel
{ model
| series = removeItem x model.series
, searchedSeries = removeItem x model.searchedSeries
, selectedSeries = removeItem x model.selectedSeries
}
DeleteDone (Err x) ->
let
_ =
Debug.log "Error on DeleteDone" x
in
newModel model
searchSelectorConfig : ItemSelector.Config Msg
searchSelectorConfig =
{ action = Nothing
, defaultText = text "Type some keywords in input bar for selecting time series"
, toggleMsg = ToggleItem
}
actionSelectorConfig : ItemSelector.Config Msg
actionSelectorConfig =
{ action =
Just
{ attrs = [ classes [ T.white, T.bg_light_red ] ]
, html = text "Delete"
, clickMsg = OnDelete
}
, defaultText = text ""
, toggleMsg = ToggleItem
}
view : Model -> Html Msg
view model =
......@@ -117,10 +186,17 @@ view model =
let
attrs =
[ classes [ T.dtc, T.pa1 ] ]
render ( cfg, series ) =
ItemSelector.view
cfg
(ItemSelector.Context series model.selectedSeries)
in
List.map
(\x -> div attrs [ ItemSelector.view ToggleItem x model.selectedSeries ])
[ model.searchedSeries, model.selectedSeries ]
(\x -> div attrs [ render x ])
[ ( searchSelectorConfig, model.searchedSeries )
, ( actionSelectorConfig, model.selectedSeries )
]
in
List.map
(div [ classes [ T.dt, T.dt__fixed ] ])
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment