mirror of
https://github.com/penpot/penpot.git
synced 2025-01-25 07:58:49 -05:00
✨ Properly handle files and pages deletion (frontend).
This commit is contained in:
parent
87d41700fc
commit
4c5b41b6b2
5 changed files with 55 additions and 57 deletions
|
@ -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}))))))
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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))))
|
||||
|
||||
|
|
|
@ -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 %)
|
||||
|
|
Loading…
Add table
Reference in a new issue