From decae84aec71458a429c8622483fdd8a9f188a67 Mon Sep 17 00:00:00 2001
From: Stefan Scheidewig <stefan.scheidewig@staffbase.com>
Date: Sun, 18 Apr 2021 18:38:38 +0200
Subject: [PATCH] Using SelectViewModel, bulk deletion of item attachments

---
 modules/webapp/src/main/elm/Api.elm           | 15 ++++
 .../src/main/elm/Comp/ItemDetail/Model.elm    |  7 +-
 .../elm/Comp/ItemDetail/SingleAttachment.elm  |  5 +-
 .../src/main/elm/Comp/ItemDetail/Update.elm   | 82 +++++++++++++------
 4 files changed, 79 insertions(+), 30 deletions(-)

diff --git a/modules/webapp/src/main/elm/Api.elm b/modules/webapp/src/main/elm/Api.elm
index 968f8041..2f2ea00a 100644
--- a/modules/webapp/src/main/elm/Api.elm
+++ b/modules/webapp/src/main/elm/Api.elm
@@ -19,6 +19,7 @@ module Api exposing
     , createScanMailbox
     , deleteAllItems
     , deleteAttachment
+    , deleteAttachments
     , deleteCustomField
     , deleteCustomValue
     , deleteCustomValueMultiple
@@ -609,6 +610,20 @@ deleteAttachment flags attachId receive =
         , expect = Http.expectJson receive Api.Model.BasicResult.decoder
         }
 
+--- Delete Attachments
+
+deleteAttachments :
+    Flags
+    -> Set String
+    -> (Result Http.Error BasicResult -> msg)
+    -> Cmd msg
+deleteAttachments flags attachIds receive =
+    Http2.authPost
+        { url = flags.config.baseUrl ++ "/api/v1/sec/attachments/delete"
+        , account = getAccount flags
+        , body = Http.jsonBody (Api.Model.IdList.encode (Set.toList attachIds |> IdList))
+        , expect = Http.expectJson receive Api.Model.BasicResult.decoder
+        }
 
 
 --- Attachment Metadata
diff --git a/modules/webapp/src/main/elm/Comp/ItemDetail/Model.elm b/modules/webapp/src/main/elm/Comp/ItemDetail/Model.elm
index 30085f5c..af0e5f86 100644
--- a/modules/webapp/src/main/elm/Comp/ItemDetail/Model.elm
+++ b/modules/webapp/src/main/elm/Comp/ItemDetail/Model.elm
@@ -4,15 +4,16 @@ module Comp.ItemDetail.Model exposing
     , Msg(..)
     , NotesField(..)
     , SaveNameState(..)
+    , SelectActionMode(..)
     , UpdateResult
     , ViewMode(..)
     , emptyModel
+    , initSelectViewModel
     , isEditNotes
     , personMatchesOrg
     , resultModel
     , resultModelCmd
     , resultModelCmdSub
-    , initSelectViewModel
     )
 
 import Api.Model.BasicResult exposing (BasicResult)
@@ -108,7 +109,6 @@ type alias Model =
     , attachmentDropdownOpen : Bool
     , editMenuTabsOpen : Set String
     , viewMode : ViewMode
-    , selectedAttachments: Set String
     }
 
 type ViewMode
@@ -202,7 +202,6 @@ emptyModel =
     , attachmentDropdownOpen = False
     , editMenuTabsOpen = Set.empty
     , viewMode = SimpleView
-    , selectedAttachments = Set.empty
     }
 
 initSelectViewModel : SelectViewModel
@@ -264,6 +263,8 @@ type Msg
     | TogglePdfNativeView Bool
     | RequestDeleteAttachment String
     | DeleteAttachConfirmed String
+    | RequestDeleteSelected
+    | DeleteSelectedConfirmed
     | AttachModalCancelled
     | DeleteAttachResp (Result Http.Error BasicResult)
     | AddFilesToggle
diff --git a/modules/webapp/src/main/elm/Comp/ItemDetail/SingleAttachment.elm b/modules/webapp/src/main/elm/Comp/ItemDetail/SingleAttachment.elm
index 2a91ecd9..abec8078 100644
--- a/modules/webapp/src/main/elm/Comp/ItemDetail/SingleAttachment.elm
+++ b/modules/webapp/src/main/elm/Comp/ItemDetail/SingleAttachment.elm
@@ -182,6 +182,7 @@ attachHeader texts settings model _ attach =
                 ]
                 , href "#"
                 , title texts.deleteAttachments
+                , onClick RequestDeleteSelected
                 ]
                 [ i [ class "fa fa-trash" ] []
                 ]
@@ -352,8 +353,8 @@ menuItem texts model pos attach =
 
         iconClass =
             case model.viewMode of
-                SelectView _ ->
-                    if Set.member attach.id model.selectedAttachments then
+                SelectView svm ->
+                    if Set.member attach.id svm.ids then
                         "fa fa-check-circle ml-1"
                     else
                         "fa fa-circle ml-1"
diff --git a/modules/webapp/src/main/elm/Comp/ItemDetail/Update.elm b/modules/webapp/src/main/elm/Comp/ItemDetail/Update.elm
index c5bf374a..95c7513f 100644
--- a/modules/webapp/src/main/elm/Comp/ItemDetail/Update.elm
+++ b/modules/webapp/src/main/elm/Comp/ItemDetail/Update.elm
@@ -24,19 +24,7 @@ import Comp.Dropdown exposing (isDropdownChangeMsg)
 import Comp.Dropzone
 import Comp.EquipmentForm
 import Comp.ItemDetail.FieldTabState as FTabState
-import Comp.ItemDetail.Model
-    exposing
-        ( AttachmentRename
-        , Model
-        , Msg(..)
-        , NotesField(..)
-        , SaveNameState(..)
-        , UpdateResult
-        , isEditNotes
-        , resultModel
-        , resultModelCmd
-        , resultModelCmdSub
-        )
+import Comp.ItemDetail.Model exposing (AttachmentRename, Model, Msg(..), NotesField(..), SaveNameState(..), SelectActionMode(..), UpdateResult, isEditNotes, resultModel, resultModelCmd, resultModelCmdSub)
 import Comp.ItemMail
 import Comp.KeyInput
 import Comp.LinkTarget
@@ -281,16 +269,19 @@ update key flags inav settings msg model =
                 }
 
         ToggleAttachment id ->
-            if Set.member id model.selectedAttachments then
-                resultModel
-                    { model
-                        | selectedAttachments = Set.remove id model.selectedAttachments
-                    }
-            else
-                resultModel
-                    { model
-                        | selectedAttachments = Set.insert id model.selectedAttachments
-                    }
+            case model.viewMode of
+                SelectView svm ->
+                    let
+                        svm_ =
+                            if Set.member id svm.ids then
+                                { svm | ids = Set.remove id svm.ids }
+                            else
+                                { svm | ids = Set.insert id svm.ids }
+                    in
+                    resultModel
+                        { model | viewMode = SelectView svm_ }
+                _ ->
+                    resultModel model
 
         ToggleMenu ->
             resultModel
@@ -948,6 +939,49 @@ update key flags inav settings msg model =
             in
             resultModel model_
 
+        RequestDeleteSelected ->
+            case model.viewMode of
+                SelectView svm ->
+                    if Set.isEmpty svm.ids then
+                        resultModel model
+
+                    else
+                        let
+                            confirmModal =
+                                Comp.ConfirmModal.defaultSettings
+                                    DeleteSelectedConfirmed
+                                    AttachModalCancelled
+                                    "Ok"
+                                    "Cancel"
+                                    "Really delete these files?"
+
+                            model_ =
+                                { model
+                                    | viewMode =
+                                        SelectView
+                                            { svm
+                                                | action = DeleteSelected
+                                            }
+                                    , attachModal = Just confirmModal
+                                }
+                        in
+                        resultModel model_
+
+                _ ->
+                    resultModel model
+
+        DeleteSelectedConfirmed ->
+            case model.viewMode of
+                SelectView svm ->
+                    let
+                        cmd =
+                            Api.deleteAttachments flags svm.ids DeleteAttachResp
+                    in
+                    resultModelCmd ( { model | attachModal = Nothing, viewMode = SimpleView }, cmd )
+
+                _ ->
+                    resultModel model
+
         AddFilesToggle ->
             resultModel
                 { model
@@ -1373,7 +1407,6 @@ update key flags inav settings msg model =
             resultModel { model
                             | attachMenuOpen = not model.attachMenuOpen
                             , viewMode = SimpleView
-                            , selectedAttachments = Set.empty
                         }
 
         UiSettingsUpdated ->
@@ -1598,7 +1631,6 @@ update key flags inav settings msg model =
             withSub
                 ( { model
                     | viewMode = nextView
-                    , selectedAttachments = Set.empty
                   }
                 , cmd
                 )