tsinfo: rename action
2 files changed, 119 insertions(+), 0 deletions(-)

M elm/Info.elm
M elm/Tsinfo.elm
M elm/Info.elm +61 -0
@@ 6,6 6,7 @@ module Info exposing
     , getwriteperms
     , idatesdecoder
     , metatype
+    , rename
     , savemeta
     , supervision
     , viewcomponents

          
@@ 15,10 16,12 @@ module Info exposing
     , viewdeletion
     , viewlog
     , viewmeta
+    , viewrenameaction
     , viewseealso
     , viewusermeta
     )
 
+
 import Array exposing (Array)
 import Dict exposing (Dict)
 import Html as H

          
@@ 100,6 103,22 @@ delete model dtype event =
         }
 
 
+rename model newname dtype event =
+    Http.request
+        { method = "put"
+        , body = Http.jsonBody <| E.object
+                 [ ( "name", E.string model.name )
+                 , ( "newname", E.string newname )
+                 ]
+        , url =
+            UB.crossOrigin model.baseurl [ "api", dtype, "state" ] [ ]
+        , expect = Http.expectString event
+        , headers = []
+        , timeout = Nothing
+        , tracker = Nothing
+        }
+
+
 savemeta model dtype callback =
     Http.request
         { method = "PUT"

          
@@ 519,3 538,45 @@ viewdeletion model dtype events =
                   , HE.onClick events.askdeletion ]
                   [ H.text "delete" ]
             ]
+
+
+viewrenameaction model dtype events =
+    if model.renaming then
+        let
+            value =
+                case model.newname of
+                    Nothing -> model.name
+                    Just newname -> newname
+        in
+        H.div [ HA.style "float" "right" ]
+            [ H.input [ HA.class "form-control-sm"
+                      , HA.size 80
+                      , HA.type_ "text"
+                      , HA.placeholder "new name"
+                      , HA.value value
+                      , HE.onInput events.editnewname
+                      ] [ ]
+            , case model.newname of
+                  Just newname ->
+                      H.button
+                          [ HA.type_ "button"
+                          , HA.class "btn btn-warning"
+                          , HE.onClick events.confirmrename
+                          ]
+                          [ H.text "confirm" ]
+                  Nothing -> H.span [ ] [ ]
+            , H.button
+                  [ HA.type_ "button"
+                  , HA.class "btn btn-success"
+                  , HE.onClick events.cancelrename
+                  ]
+                  [ H.text "cancel" ]
+            ]
+    else
+        H.div [ HA.style "float" "right" ]
+            [ H.button
+                  [ HA.type_ "button"
+                  , HA.class "btn btn-primary"
+                  , HE.onClick events.askrename ]
+                  [ H.text "rename" ]
+            ]

          
M elm/Tsinfo.elm +58 -0
@@ 76,6 76,9 @@ type alias Model =
     , editeditems : Dict String String
     -- deletion
     , deleting : Bool
+    -- renaming
+    , renaming : Bool
+    , newname : Maybe String
     }
 
 

          
@@ 119,6 122,12 @@ type Msg
     | CancelDeletion
     | ConfirmDeletion
     | Deleted (Result Http.Error String)
+    -- renaming
+    | AskRename
+    | EditNewName String
+    | ConfirmRename
+    | CancelRename
+    | Renamed (Result Http.Error String)
 
 
 logentrydecoder : D.Decoder Logentry

          
@@ 548,6 557,44 @@ update msg model =
         Deleted (Err err) ->
             doerr "deletion failed" <| U.unwraperror err
 
+        -- renaming
+
+        AskRename ->
+            U.nocmd { model | renaming = True }
+
+        CancelRename ->
+            U.nocmd { model
+                        | renaming = False
+                        , newname = Nothing
+                    }
+
+        EditNewName name ->
+            U.nocmd { model | newname = Just name }
+
+        ConfirmRename ->
+            let
+                cmd =
+                    case model.newname of
+                        Nothing -> Cmd.none
+                        Just newname ->
+                            I.rename model newname "series" Renamed
+            in
+            ( model
+            , cmd
+            )
+
+        Renamed (Ok _) ->
+            let name =
+                    case model.newname of
+                        Just newname -> newname
+                        Nothing -> model.name
+            in
+            ( model
+            , load <| UB.crossOrigin model.baseurl [ "tsinfo" ] [ UB.string "name" name ]
+            )
+
+        Renamed (Err err) ->
+            doerr "deletion failed" <| U.unwraperror err
 
 -- views
 

          
@@ 718,10 765,19 @@ deleteevents =
     }
 
 
+renameevents =
+    { confirmrename = ConfirmRename
+    , editnewname = EditNewName
+    , cancelrename = CancelRename
+    , askrename = AskRename
+    }
+
+
 view : Model -> Html Msg
 view model =
     div [ A.style "margin" ".5em" ]
         [ I.viewdeletion model "series" deleteevents
+        , I.viewrenameaction model "series" renameevents
         , h1 [ ]
               [ text "Series "
               , span

          
@@ 793,6 849,8 @@ main =
                            ("", "")
                            Dict.empty
                            False
+                           False
+                           Nothing
                in
                ( model
                , Cmd.batch