From e056da04c34f667cf6b9344936ea8d3bed70d6a2 Mon Sep 17 00:00:00 2001
From: Andrey Antukh <niwi@niwi.nz>
Date: Sun, 19 Apr 2020 13:01:44 +0200
Subject: [PATCH] :bug: Fix shape hidden/blocked attrs handling.

---
 frontend/src/uxbox/main/data/workspace.cljs   | 101 +++---------------
 .../uxbox/main/ui/workspace/context_menu.cljs |  10 +-
 .../main/ui/workspace/sidebar/layers.cljs     |  17 +--
 3 files changed, 30 insertions(+), 98 deletions(-)

diff --git a/frontend/src/uxbox/main/data/workspace.cljs b/frontend/src/uxbox/main/data/workspace.cljs
index 2a8ab79a2..54bf6e439 100644
--- a/frontend/src/uxbox/main/data/workspace.cljs
+++ b/frontend/src/uxbox/main/data/workspace.cljs
@@ -1851,62 +1851,10 @@
     ptk/UpdateEvent
     (update [_ state]
       (let [page-id (::page-id state)]
-        (update-in state [:workspace-data page-id :objects id :segments index] gpt/add delta)))))
-
-
-;; --- Shape Visibility
-
-(declare impl-update-shape-hidden)
-
-(defn hide-shape
-  [id]
-  (us/verify ::us/uuid id)
-  (ptk/reify ::hide-shape
-    IBatchedChange
-    ptk/UpdateEvent
-    (update [_ state]
-      (impl-update-shape-hidden state id true))))
-
-(defn show-shape
-  [id]
-  (us/verify ::us/uuid id)
-  (ptk/reify ::show-shape
-    IBatchedChange
-    ptk/UpdateEvent
-    (update [_ state]
-      (impl-update-shape-hidden state id false))))
-
-(defn hide-frame
-  [id]
-  (us/verify ::us/uuid id)
-  (ptk/reify ::hide-shape
-    IBatchedChange
-    ptk/UpdateEvent
-    (update [_ state]
-      (let [hide #(impl-update-shape-hidden %1 %2 true)
-            page-id (::page-id state)
-            objects (get-in state [:workspace-data page-id :objects])
-            frame   (get objects id)]
-        (reduce hide state (cons id (:shapes frame)))))))
-
-(defn show-frame
-  [id]
-  (us/verify ::us/uuid id)
-  (ptk/reify ::hide-shape
-    IBatchedChange
-    ptk/UpdateEvent
-    (update [_ state]
-      (let [show #(impl-update-shape-hidden %1 %2 false)
-            page-id (::page-id state)
-            objects (get-in state [:workspace-data page-id :objects])
-            frame   (get objects id)]
-        (reduce show state (cons id (:shapes frame)))))))
-
-(defn- impl-update-shape-hidden
-  [state id hidden?]
-  (let [page-id (::page-id state)]
-    (assoc-in state [:workspace-data page-id :objects id :hidden] hidden?)))
+        (update-in state [:workspace-data page-id :objects id :segments index]
+                   gpt/add delta)))))
 
+;; --- Shape attrs (Layers Sidebar)
 
 (defn toggle-collapse
   [id]
@@ -1921,38 +1869,21 @@
     (update [_ state]
       (update state :workspace-local dissoc :expanded))))
 
-;; --- Shape Blocking
-
-(declare impl-update-shape-blocked)
-
-(defn block-shape
-  [id]
-  (us/verify ::us/uuid id)
-  (ptk/reify ::hide-shape
+(defn recursive-assign
+  "A helper for assign recursively a shape attr."
+  [id attr value]
+  (ptk/reify ::recursive-assign
     ptk/UpdateEvent
     (update [_ state]
-      (impl-update-shape-blocked state id true))))
-
-(defn unblock-shape
-  [id]
-  (us/verify ::us/uuid id)
-  (ptk/reify ::hide-shape
-    ptk/UpdateEvent
-    (update [_ state]
-      (impl-update-shape-blocked state id false))))
-
-(defn- impl-update-shape-blocked
-  [state id blocked?]
-  (let [page-id (::page-id state)
-        obj (get-in state [:workspace-data page-id :objects id])
-        obj (assoc obj :blocked blocked?)
-        state (assoc-in state [:workspace-data page-id :objects id] obj)]
-    (if (= :frame (:type obj))
-      (update-in state [:workspace-data page-id :objects]
-                 (fn [objects]
-                   (reduce #(update %1 %2 assoc :blocked blocked?) objects (:shapes obj))))
-      state)))
-
+      (let [page-id (get-in state [:workspace-page :id])
+            objects (get-in state [:workspace-data page-id :objects])
+            childs (helpers/get-children id objects)]
+        (update-in state [:workspace-data page-id :objects]
+                   (fn [objects]
+                     (reduce (fn [objects id]
+                               (assoc-in objects [id attr] value))
+                             objects
+                             (conj childs id))))))))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Navigation
diff --git a/frontend/src/uxbox/main/ui/workspace/context_menu.cljs b/frontend/src/uxbox/main/ui/workspace/context_menu.cljs
index 0e73698ad..0d57ab3f7 100644
--- a/frontend/src/uxbox/main/ui/workspace/context_menu.cljs
+++ b/frontend/src/uxbox/main/ui/workspace/context_menu.cljs
@@ -43,7 +43,7 @@
 
 (mf/defc shape-context-menu
   [{:keys [mdata] :as props}]
-  (let [shape (:shape mdata)
+  (let [{:keys [id] :as shape} (:shape mdata)
         selected (:selected mdata)
 
         do-duplicate #(st/emit! dw/duplicate-selected)
@@ -54,10 +54,10 @@
         do-bring-to-front #(st/emit! (dw/vertical-order-selected :top))
         do-send-backward #(st/emit! (dw/vertical-order-selected :down))
         do-send-to-back #(st/emit! (dw/vertical-order-selected :bottom))
-        do-show-shape #(st/emit! (dw/show-shape (:id shape)))
-        do-hide-shape #(st/emit! (dw/hide-shape (:id shape)))
-        do-lock-shape #(st/emit! (dw/block-shape (:id shape)))
-        do-unlock-shape #(st/emit! (dw/unblock-shape (:id shape)))
+        do-show-shape #(st/emit! (dw/recursive-assign id :hidden false))
+        do-hide-shape #(st/emit! (dw/recursive-assign id :hidden true))
+        do-lock-shape #(st/emit! (dw/recursive-assign id :blocked true))
+        do-unlock-shape #(st/emit! (dw/recursive-assign id :blocked false))
         do-create-group #(st/emit! dw/create-group)
         do-remove-group #(st/emit! dw/remove-group)]
     [:*
diff --git a/frontend/src/uxbox/main/ui/workspace/sidebar/layers.cljs b/frontend/src/uxbox/main/ui/workspace/sidebar/layers.cljs
index e1a275618..5ceb9ef33 100644
--- a/frontend/src/uxbox/main/ui/workspace/sidebar/layers.cljs
+++ b/frontend/src/uxbox/main/ui/workspace/sidebar/layers.cljs
@@ -83,11 +83,12 @@
 
 (mf/defc layer-item
   [{:keys [index item selected objects] :as props}]
-  (let [selected? (contains? selected (:id item))
+  (let [id        (:id item)
+        selected? (contains? selected id)
 
         expanded-iref (mf/use-memo
-                        (mf/deps (:id item))
-                        (make-collapsed-iref (:id item)))
+                        (mf/deps id)
+                        (make-collapsed-iref id))
 
         expanded? (mf/deref expanded-iref)
 
@@ -96,21 +97,21 @@
           (dom/stop-propagation event)
           (if (and expanded? (kbd/shift? event))
             (st/emit! dw/collapse-all)
-            (st/emit! (dw/toggle-collapse (:id item)))))
+            (st/emit! (dw/toggle-collapse id))))
 
         toggle-blocking
         (fn [event]
           (dom/stop-propagation event)
           (if (:blocked item)
-            (st/emit! (dw/unblock-shape (:id item)))
-            (st/emit! (dw/block-shape (:id item)))))
+            (st/emit! (dw/recursive-assign id :blocked false))
+            (st/emit! (dw/recursive-assign id :blocked true))))
 
         toggle-visibility
         (fn [event]
           (dom/stop-propagation event)
           (if (:hidden item)
-            (st/emit! (dw/show-shape (:id item)))
-            (st/emit! (dw/hide-shape (:id item)))))
+            (st/emit! (dw/recursive-assign id :hidden false))
+            (st/emit! (dw/recursive-assign id :hidden true))))
 
         select-shape
         (fn [event]