From aba35dee617b0a538e000ad9094b51d56e89fdfd Mon Sep 17 00:00:00 2001
From: Andrey Antukh <niwi@niwi.nz>
Date: Tue, 31 Jan 2017 00:07:53 +0100
Subject: [PATCH] Add more safer shapes packing mechanism.

---
 frontend/src/uxbox/main/data/pages.cljs       | 28 ++++++++++++++++---
 frontend/src/uxbox/main/exports.cljs          |  2 +-
 frontend/src/uxbox/main/ui/shapes/common.cljs |  2 +-
 3 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/frontend/src/uxbox/main/data/pages.cljs b/frontend/src/uxbox/main/data/pages.cljs
index 2ea7224b6..f719b7905 100644
--- a/frontend/src/uxbox/main/data/pages.cljs
+++ b/frontend/src/uxbox/main/data/pages.cljs
@@ -76,15 +76,36 @@
 
 ;; --- Helpers
 
+;; TODO: make sure remove all :tmp-* related attrs from shape
+
+(defn pack-page-shapes
+  "Create a hash-map of shapes indexed by their id that belongs
+  to the provided page."
+  [state page]
+  (let [lookup-shape-xf (map #(get-in state [:shapes %]))]
+    (reduce (fn reducer [acc {:keys [id type items] :as shape}]
+              (let [shape (assoc shape :page (:id page))]
+                (cond
+                  (= type :group)
+                  (reduce reducer
+                          (assoc acc id shape)
+                          (sequence lookup-shape-xf items))
+
+                  (uuid? id)
+                  (assoc acc id shape)
+
+                  :else acc)))
+            {}
+            (sequence lookup-shape-xf (:shapes page)))))
+
 (defn pack-page
   "Return a packed version of page object ready
   for send to remore storage service."
   [state id]
   (let [page (get-in state [:pages id])
-        xf (filter #(= (:page (second %)) id))
-        shapes (into {} xf (:shapes state))]
+        shapes (pack-page-shapes state page)]
     (-> page
-        (assoc-in [:data :shapes] (into [] (:shapes page)))
+        (assoc-in [:data :shapes] (vec (:shapes page)))
         (assoc-in [:data :shapes-map] shapes)
         (dissoc :shapes))))
 
@@ -303,7 +324,6 @@
 
 (defn update-page
   [id data]
-  (println "update-page" data)
   {:pre [(uuid? id) (us/valid? ::page-entity data)]}
   (UpdatePage. id data))
 
diff --git a/frontend/src/uxbox/main/exports.cljs b/frontend/src/uxbox/main/exports.cljs
index 2b0f2e954..adaafa373 100644
--- a/frontend/src/uxbox/main/exports.cljs
+++ b/frontend/src/uxbox/main/exports.cljs
@@ -48,7 +48,7 @@
     (shape-wrapper shape)))
 
 (mx/defc page-svg
-  [{:keys [metadata] :as page}]
+  [{:keys [id metadata] :as page}]
   (let [{:keys [width height]} metadata]
     [:svg {:width width
            :height height
diff --git a/frontend/src/uxbox/main/ui/shapes/common.cljs b/frontend/src/uxbox/main/ui/shapes/common.cljs
index 9afbebcfa..877cccd97 100644
--- a/frontend/src/uxbox/main/ui/shapes/common.cljs
+++ b/frontend/src/uxbox/main/ui/shapes/common.cljs
@@ -42,7 +42,7 @@
   []
   (letfn [(on-move [shape delta]
             (st/emit! (uds/apply-temporal-displacement shape delta)))
-          (on-stop [{:keys [id] :as shape}]
+          (on-stop [shape]
             (st/emit! (uds/apply-displacement shape)))
           (on-start [shape]
             (let [stoper (->> streams/events