From 4c5b41b6b2f5547c86cf2e596f5928d50361044f Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 11 Dec 2019 16:35:30 +0100 Subject: [PATCH] :sparkles: Properly handle files and pages deletion (frontend). --- frontend/src/uxbox/main/data/pages.cljs | 49 +++++++++---------- frontend/src/uxbox/main/data/projects.cljs | 37 +++++++++----- frontend/src/uxbox/main/data/workspace.cljs | 16 ------ .../src/uxbox/main/ui/dashboard/projects.cljs | 8 +-- .../main/ui/workspace/sidebar/sitemap.cljs | 2 +- 5 files changed, 55 insertions(+), 57 deletions(-) diff --git a/frontend/src/uxbox/main/data/pages.cljs b/frontend/src/uxbox/main/data/pages.cljs index c0ca3c82c..179724fdb 100644 --- a/frontend/src/uxbox/main/data/pages.cljs +++ b/frontend/src/uxbox/main/data/pages.cljs @@ -12,6 +12,7 @@ [cuerdas.core :as str] [potok.core :as ptk] [uxbox.main.repo.core :as rp] + [uxbox.main.data.projects :as dp] [uxbox.util.data :refer [index-by-id concatv]] [uxbox.util.spec :as us] [uxbox.util.timers :as ts] @@ -106,17 +107,14 @@ (defn purge-page "Remove page and all related stuff from the state." [state id] - (if-let [project-id (get-in state [:pages id :project-id])] + (if-let [file-id (get-in state [:pages id :file-id])] (-> state - (update-in [:projects project-id :pages] #(filterv (partial not= id) %)) + (update-in [:files file-id :pages] #(filterv (partial not= id) %)) + (update-in [:workspace-file :pages] #(filterv (partial not= id) %)) (update :pages dissoc id) (update :pages-data dissoc id)) state)) -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Generic Page Events (mostly Fetch & CRUD) -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - ;; --- Fetch Pages (by File ID) (declare pages-fetched) @@ -216,10 +214,6 @@ (watch [_ state stream] (rx/of (uxbox.main.data.projects/fetch-file file-id))))) -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Workspace-Aware Page Events -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - ;; --- Rename Page (s/def ::rename-page @@ -242,6 +236,26 @@ (->> (rp/mutation :rename-page params) (rx/map #(ptk/data-event ::page-renamed data))))))) +;; --- Delete Page (by ID) + +(defn delete-page + [id] + {:pre [(uuid? id)]} + (reify + ptk/UpdateEvent + (update [_ state] + (purge-page state id)) + + ptk/WatchEvent + (watch [_ state s] + (let [page (:workspace-page state)] + (rx/merge + (->> (rp/mutation :delete-project-page {:id id}) + (rx/flat-map (fn [_] + (if (= id (:id page)) + (rx/of (dp/go-to (:file-id page))) + (rx/empty)))))))))) + ;; --- Persist Page (declare page-persisted) @@ -302,18 +316,3 @@ ptk/UpdateEvent (update [this state] (assoc-in state [:pages id :metadata] metadata)))) - -;; --- Delete Page (by id) - -(defn delete-page - [id] - {:pre [(uuid? id)]} - (reify - ptk/UpdateEvent - (update [_ state] - (purge-page state id)) - - ptk/WatchEvent - (watch [_ state s] - (->> (rp/mutation :delete-page {:id id}) - (rx/map (ptk/data-event ::page-deleted {:id id})))))) diff --git a/frontend/src/uxbox/main/data/projects.cljs b/frontend/src/uxbox/main/data/projects.cljs index 97b592e43..fa8d8d990 100644 --- a/frontend/src/uxbox/main/data/projects.cljs +++ b/frontend/src/uxbox/main/data/projects.cljs @@ -11,7 +11,6 @@ [beicon.core :as rx] [potok.core :as ptk] [uxbox.main.repo.core :as rp] - [uxbox.main.data.pages :as udp] [uxbox.util.uuid :as uuid] [uxbox.util.spec :as us] [uxbox.util.time :as dt] @@ -171,19 +170,33 @@ ;; --- Delete Project (by id) -(defrecord DeleteProject [id] - ptk/WatchEvent - (watch [_ state s] - (letfn [(on-success [_] - #(dissoc-project % id))] - (->> (rp/mutation :delete-project {:id id}) - (rx/map on-success))))) - (defn delete-project [id] - (if (map? id) - (DeleteProject. (:id id)) - (DeleteProject. id))) + (s/assert ::us/uuid id) + (ptk/reify ::delete-project + ptk/UpdateEvent + (update [_ state] + (dissoc-project state id)) + + ptk/WatchEvent + (watch [_ state s] + (->> (rp/mutation :delete-project {:id id}) + (rx/ignore))))) + +;; --- Delete File (by id) + +(defn delete-file + [id] + (s/assert ::us/uuid id) + (ptk/reify ::delete-file + ptk/UpdateEvent + (update [_ state] + (update state :files dissoc id)) + + ptk/WatchEvent + (watch [_ state s] + (->> (rp/mutation :delete-project-file {:id id}) + (rx/ignore))))) ;; --- Create Project diff --git a/frontend/src/uxbox/main/data/workspace.cljs b/frontend/src/uxbox/main/data/workspace.cljs index df399a21f..1ef5161f8 100644 --- a/frontend/src/uxbox/main/data/workspace.cljs +++ b/frontend/src/uxbox/main/data/workspace.cljs @@ -1177,22 +1177,6 @@ pages (vec (concat before [id] after))] (assoc-in state [:projects (:project-id page) :pages] pages))))) -;; --- Delete Page -;; TODO: join with udp/delete-page -(defn delete-page - [id] - (s/assert ::us/uuid id) - (ptk/reify ::delete-page - ptk/WatchEvent - (watch [_ state stream] - (let [project-id (get-in state [:pages id :project-id])] - (rx/merge - (rx/of (udp/delete-page id)) - (->> stream - (rx/filter #(= % ::udp/delete-completed)) - (rx/map #(navigate-to-project project-id)) - (rx/take 1))))))) - ;; -- Page Changes Watcher (def watch-page-changes diff --git a/frontend/src/uxbox/main/ui/dashboard/projects.cljs b/frontend/src/uxbox/main/ui/dashboard/projects.cljs index 7b5170993..1a136b356 100644 --- a/frontend/src/uxbox/main/ui/dashboard/projects.cljs +++ b/frontend/src/uxbox/main/ui/dashboard/projects.cljs @@ -131,7 +131,7 @@ [{:keys [file] :as props}] (let [local (mf/use-state {}) on-navigate #(st/emit! (udp/go-to (:id file))) - delete-fn #(st/emit! nil #_(udp/delete-file (:id file))) + delete-fn #(st/emit! nil (udp/delete-file (:id file))) on-delete #(do (dom/stop-propagation %) (modal/show! confirm-dialog {:on-accept delete-fn})) @@ -257,8 +257,10 @@ (def files-ref (letfn [(selector [state] (let [id (get-in state [:dashboard-projects :id]) - ids (get-in state [:dashboard-projects :files id])] - (mapv #(get-in state [:files %]) ids)))] + ids (get-in state [:dashboard-projects :files id]) + xf (comp (map #(get-in state [:files %])) + (remove nil?))] + (into [] xf ids)))] (-> (l/lens selector) (l/derive st/state)))) diff --git a/frontend/src/uxbox/main/ui/workspace/sidebar/sitemap.cljs b/frontend/src/uxbox/main/ui/workspace/sidebar/sitemap.cljs index 5af0c0725..29b570de4 100644 --- a/frontend/src/uxbox/main/ui/workspace/sidebar/sitemap.cljs +++ b/frontend/src/uxbox/main/ui/workspace/sidebar/sitemap.cljs @@ -30,7 +30,7 @@ (mf/defc page-item [{:keys [page index deletable? selected?] :as props}] (let [on-edit #(modal/show! page-form-dialog {:page page}) - delete-fn #(st/emit! (dw/delete-page (:id page))) + delete-fn #(st/emit! (udp/delete-page (:id page))) on-delete #(do (dom/prevent-default %) (dom/stop-propagation %)