From 0f8420011867cd6b99941fbe59f7d63124ed2482 Mon Sep 17 00:00:00 2001
From: Eike Kettner <eike.kettner@posteo.de>
Date: Mon, 26 Oct 2020 15:32:58 +0100
Subject: [PATCH] Indicate saving when changing name for multiple items

---
 .../webapp/src/main/elm/Page/Home/Data.elm    |  7 +-
 .../webapp/src/main/elm/Page/Home/Update.elm  | 64 +++++++++++++++----
 .../webapp/src/main/elm/Page/Home/View.elm    |  7 +-
 3 files changed, 63 insertions(+), 15 deletions(-)

diff --git a/modules/webapp/src/main/elm/Page/Home/Data.elm b/modules/webapp/src/main/elm/Page/Home/Data.elm
index 2c6909a8..9754a70f 100644
--- a/modules/webapp/src/main/elm/Page/Home/Data.elm
+++ b/modules/webapp/src/main/elm/Page/Home/Data.elm
@@ -23,7 +23,8 @@ import Api.Model.ItemSearch
 import Browser.Dom as Dom
 import Comp.FixedDropdown
 import Comp.ItemCardList
-import Comp.ItemDetail.EditMenu
+import Comp.ItemDetail.EditMenu exposing (SaveNameState(..))
+import Comp.ItemDetail.FormChange exposing (FormChange)
 import Comp.SearchMenu
 import Comp.YesNoDimmer
 import Data.Flags exposing (Flags)
@@ -60,6 +61,7 @@ type alias SelectViewModel =
     , action : SelectActionMode
     , deleteAllConfirm : Comp.YesNoDimmer.Model
     , editModel : Comp.ItemDetail.EditMenu.Model
+    , saveNameState : SaveNameState
     }
 
 
@@ -69,6 +71,7 @@ initSelectViewModel =
     , action = NoneAction
     , deleteAllConfirm = Comp.YesNoDimmer.initActive
     , editModel = Comp.ItemDetail.EditMenu.init
+    , saveNameState = SaveSuccess
     }
 
 
@@ -167,7 +170,7 @@ type Msg
     | DeleteSelectedConfirmMsg Comp.YesNoDimmer.Msg
     | EditSelectedItems
     | EditMenuMsg Comp.ItemDetail.EditMenu.Msg
-    | MultiUpdateResp (Result Http.Error BasicResult)
+    | MultiUpdateResp FormChange (Result Http.Error BasicResult)
     | ReplaceChangedItemsResp (Result Http.Error ItemLightList)
     | DeleteAllResp (Result Http.Error BasicResult)
 
diff --git a/modules/webapp/src/main/elm/Page/Home/Update.elm b/modules/webapp/src/main/elm/Page/Home/Update.elm
index b9c3684f..35cc0147 100644
--- a/modules/webapp/src/main/elm/Page/Home/Update.elm
+++ b/modules/webapp/src/main/elm/Page/Home/Update.elm
@@ -3,12 +3,12 @@ module Page.Home.Update exposing (update)
 import Api
 import Api.Model.IdList exposing (IdList)
 import Api.Model.ItemLightList exposing (ItemLightList)
-import Api.Model.ItemSearch exposing (ItemSearch)
+import Api.Model.ItemSearch
 import Browser.Navigation as Nav
 import Comp.FixedDropdown
 import Comp.ItemCardList
-import Comp.ItemDetail.EditMenu
-import Comp.ItemDetail.FormChange
+import Comp.ItemDetail.EditMenu exposing (SaveNameState(..))
+import Comp.ItemDetail.FormChange exposing (FormChange(..))
 import Comp.SearchMenu
 import Comp.YesNoDimmer
 import Data.Flags exposing (Flags)
@@ -435,7 +435,16 @@ update mId key flags settings msg model =
                             Comp.ItemDetail.EditMenu.update flags lmsg svm.editModel
 
                         svm_ =
-                            { svm | editModel = res.model }
+                            { svm
+                                | editModel = res.model
+                                , saveNameState =
+                                    case res.change of
+                                        NameChange _ ->
+                                            Saving
+
+                                        _ ->
+                                            svm.saveNameState
+                            }
 
                         cmd_ =
                             Cmd.map EditMenuMsg res.cmd
@@ -447,7 +456,7 @@ update mId key flags settings msg model =
                             Comp.ItemDetail.FormChange.multiUpdate flags
                                 svm.ids
                                 res.change
-                                MultiUpdateResp
+                                (MultiUpdateResp res.change)
                     in
                     ( { model | viewMode = SelectView svm_ }
                     , Cmd.batch [ cmd_, upCmd ]
@@ -457,21 +466,28 @@ update mId key flags settings msg model =
                 _ ->
                     noSub ( model, Cmd.none )
 
-        MultiUpdateResp (Ok res) ->
+        MultiUpdateResp change (Ok res) ->
+            let
+                nm =
+                    updateSelectViewNameState res.success model change
+            in
             if res.success then
                 case model.viewMode of
                     SelectView svm ->
                         -- replace changed items in the view
-                        noSub ( model, loadChangedItems flags svm.ids )
+                        noSub ( nm, loadChangedItems flags svm.ids )
 
                     _ ->
-                        noSub ( model, Cmd.none )
+                        noSub ( nm, Cmd.none )
 
             else
-                noSub ( model, Cmd.none )
+                noSub ( nm, Cmd.none )
 
-        MultiUpdateResp (Err _) ->
-            noSub ( model, Cmd.none )
+        MultiUpdateResp change (Err _) ->
+            ( updateSelectViewNameState False model change
+            , Cmd.none
+            , Sub.none
+            )
 
         ReplaceChangedItemsResp (Ok items) ->
             noSub ( replaceItems model items, Cmd.none )
@@ -484,6 +500,32 @@ update mId key flags settings msg model =
 --- Helpers
 
 
+updateSelectViewNameState : Bool -> Model -> FormChange -> Model
+updateSelectViewNameState success model change =
+    case model.viewMode of
+        SelectView svm ->
+            case change of
+                NameChange _ ->
+                    let
+                        svm_ =
+                            { svm
+                                | saveNameState =
+                                    if success then
+                                        SaveSuccess
+
+                                    else
+                                        SaveFailed
+                            }
+                    in
+                    { model | viewMode = SelectView svm_ }
+
+                _ ->
+                    model
+
+        _ ->
+            model
+
+
 replaceItems : Model -> ItemLightList -> Model
 replaceItems model newItems =
     let
diff --git a/modules/webapp/src/main/elm/Page/Home/View.elm b/modules/webapp/src/main/elm/Page/Home/View.elm
index 593a29a3..47e3b813 100644
--- a/modules/webapp/src/main/elm/Page/Home/View.elm
+++ b/modules/webapp/src/main/elm/Page/Home/View.elm
@@ -174,8 +174,11 @@ viewLeftMenu flags settings model =
             case svm.action of
                 EditSelected ->
                     let
-                        cfg =
+                        cfg_ =
                             Comp.ItemDetail.EditMenu.defaultViewConfig
+
+                        cfg =
+                            { cfg_ | nameState = svm.saveNameState }
                     in
                     [ div [ class "ui dividing header" ]
                         [ text "Multi-Edit"
@@ -208,7 +211,7 @@ viewBar flags model =
 
 
 viewActionBar : Flags -> SelectViewModel -> Model -> Html Msg
-viewActionBar _ svm model =
+viewActionBar _ svm _ =
     let
         selectCount =
             Set.size svm.ids |> String.fromInt