mirror of
https://github.com/penpot/penpot.git
synced 2025-08-07 14:38:33 +02:00
Improve state usage on images page and its events.
This commit is contained in:
parent
a325e2e3ae
commit
9af2e54a2c
2 changed files with 32 additions and 31 deletions
|
@ -24,18 +24,19 @@
|
||||||
(-apply-update [_ state]
|
(-apply-update [_ state]
|
||||||
(let [type (or type :builtin)
|
(let [type (or type :builtin)
|
||||||
id (or id (if (= type :builtin) 1 nil))
|
id (or id (if (= type :builtin) 1 nil))
|
||||||
data {:type type :id id :selected #{}
|
data {:type type :id id :selected #{}}]
|
||||||
:section :dashboard/images}]
|
(-> state
|
||||||
(assoc state :dashboard data)))
|
(assoc-in [:dashboard :images] data)
|
||||||
|
(assoc-in [:dashboard :section] :dashboard/images))))
|
||||||
|
|
||||||
rs/EffectEvent
|
;; rs/EffectEvent
|
||||||
(-apply-effect [_ state]
|
;; (-apply-effect [_ state]
|
||||||
(when (nil? (:images-by-id state))
|
;; (when (nil? (:image-colls-by-id state))
|
||||||
(reset! st/loader true)))
|
;; (reset! st/loader true)))
|
||||||
|
|
||||||
rs/WatchEvent
|
rs/WatchEvent
|
||||||
(-apply-watch [_ state s]
|
(-apply-watch [_ state s]
|
||||||
(if (nil? (:images-by-id state))
|
(if (nil? (:image-colls-by-id state))
|
||||||
(rx/merge
|
(rx/merge
|
||||||
(rx/of (fetch-collections))
|
(rx/of (fetch-collections))
|
||||||
(->> (rx/filter collections-fetched? s)
|
(->> (rx/filter collections-fetched? s)
|
||||||
|
@ -71,7 +72,7 @@
|
||||||
(reduce (fn [state item]
|
(reduce (fn [state item]
|
||||||
(let [id (:id item)
|
(let [id (:id item)
|
||||||
item (assoc item :type :own)]
|
item (assoc item :type :own)]
|
||||||
(assoc-in state [:images-by-id id] item)))
|
(assoc-in state [:image-colls-by-id id] item)))
|
||||||
state
|
state
|
||||||
items)))
|
items)))
|
||||||
|
|
||||||
|
@ -99,9 +100,9 @@
|
||||||
(-apply-update [_ state]
|
(-apply-update [_ state]
|
||||||
(let [item (assoc item :type :own)]
|
(let [item (assoc item :type :own)]
|
||||||
(-> state
|
(-> state
|
||||||
(assoc-in [:images-by-id (:id item)] item)
|
(assoc-in [:image-colls-by-id (:id item)] item)
|
||||||
(assoc-in [:dashboard :collection-id] (:id item))
|
(assoc-in [:dashboard :images :id] (:id item))
|
||||||
(assoc-in [:dashboard :collection-type] :own)))))
|
(assoc-in [:dashboard :type] :own)))))
|
||||||
|
|
||||||
(defn collection-created
|
(defn collection-created
|
||||||
[item]
|
[item]
|
||||||
|
@ -131,7 +132,7 @@
|
||||||
(defrecord CollectionUpdated [item]
|
(defrecord CollectionUpdated [item]
|
||||||
rs/UpdateEvent
|
rs/UpdateEvent
|
||||||
(-apply-update [_ state]
|
(-apply-update [_ state]
|
||||||
(update-in state [:images-by-id (:id item)] merge item)))
|
(update-in state [:image-colls-by-id (:id item)] merge item)))
|
||||||
|
|
||||||
(defn collection-updated
|
(defn collection-updated
|
||||||
[item]
|
[item]
|
||||||
|
@ -142,7 +143,7 @@
|
||||||
(defrecord UpdateCollection [id]
|
(defrecord UpdateCollection [id]
|
||||||
rs/WatchEvent
|
rs/WatchEvent
|
||||||
(-apply-watch [_ state s]
|
(-apply-watch [_ state s]
|
||||||
(let [item (get-in state [:images-by-id id])]
|
(let [item (get-in state [:image-colls-by-id id])]
|
||||||
(->> (rp/req :update/image-collection item)
|
(->> (rp/req :update/image-collection item)
|
||||||
(rx/map :payload)
|
(rx/map :payload)
|
||||||
(rx/map collection-updated)))))
|
(rx/map collection-updated)))))
|
||||||
|
@ -156,7 +157,7 @@
|
||||||
(defrecord RenameCollection [id name]
|
(defrecord RenameCollection [id name]
|
||||||
rs/UpdateEvent
|
rs/UpdateEvent
|
||||||
(-apply-update [_ state]
|
(-apply-update [_ state]
|
||||||
(assoc-in state [:images-by-id id :name] name))
|
(assoc-in state [:image-colls-by-id id :name] name))
|
||||||
|
|
||||||
rs/WatchEvent
|
rs/WatchEvent
|
||||||
(-apply-watch [_ state s]
|
(-apply-watch [_ state s]
|
||||||
|
@ -171,7 +172,7 @@
|
||||||
(defrecord DeleteCollection [id]
|
(defrecord DeleteCollection [id]
|
||||||
rs/UpdateEvent
|
rs/UpdateEvent
|
||||||
(-apply-update [_ state]
|
(-apply-update [_ state]
|
||||||
(update state :images-by-id dissoc id))
|
(update state :image-colls-by-id dissoc id))
|
||||||
|
|
||||||
rs/WatchEvent
|
rs/WatchEvent
|
||||||
(-apply-watch [_ state s]
|
(-apply-watch [_ state s]
|
||||||
|
@ -187,7 +188,7 @@
|
||||||
(defrecord ImageCreated [item]
|
(defrecord ImageCreated [item]
|
||||||
rs/UpdateEvent
|
rs/UpdateEvent
|
||||||
(-apply-update [_ state]
|
(-apply-update [_ state]
|
||||||
(update-in state [:images-by-id (:collection item) :images]
|
(update-in state [:image-colls-by-id (:collection item) :images]
|
||||||
#(conj % item))))
|
#(conj % item))))
|
||||||
|
|
||||||
(defn image-created
|
(defn image-created
|
||||||
|
@ -221,7 +222,7 @@
|
||||||
(defrecord ImagesFetched [coll-id items]
|
(defrecord ImagesFetched [coll-id items]
|
||||||
rs/UpdateEvent
|
rs/UpdateEvent
|
||||||
(-apply-update [_ state]
|
(-apply-update [_ state]
|
||||||
(assoc-in state [:images-by-id coll-id :images] (set items))))
|
(assoc-in state [:image-colls-by-id coll-id :images] (set items))))
|
||||||
|
|
||||||
(defn images-fetched
|
(defn images-fetched
|
||||||
[coll-id items]
|
[coll-id items]
|
||||||
|
@ -246,7 +247,7 @@
|
||||||
(defrecord DeleteImage [coll-id image]
|
(defrecord DeleteImage [coll-id image]
|
||||||
rs/UpdateEvent
|
rs/UpdateEvent
|
||||||
(-apply-update [_ state]
|
(-apply-update [_ state]
|
||||||
(update-in state [:images-by-id coll-id :images] disj image))
|
(update-in state [:image-colls-by-id coll-id :images] disj image))
|
||||||
|
|
||||||
rs/WatchEvent
|
rs/WatchEvent
|
||||||
(-apply-watch [_ state s]
|
(-apply-watch [_ state s]
|
||||||
|
@ -262,7 +263,7 @@
|
||||||
(defrecord RemoveImages [id images]
|
(defrecord RemoveImages [id images]
|
||||||
rs/UpdateEvent
|
rs/UpdateEvent
|
||||||
(-apply-update [_ state]
|
(-apply-update [_ state]
|
||||||
#_(update-in state [:images-by-id id :data]
|
#_(update-in state [:image-colls-by-id id :data]
|
||||||
#(set/difference % images)))
|
#(set/difference % images)))
|
||||||
|
|
||||||
rs/WatchEvent
|
rs/WatchEvent
|
||||||
|
@ -280,17 +281,17 @@
|
||||||
(defrecord SelectImage [id]
|
(defrecord SelectImage [id]
|
||||||
rs/UpdateEvent
|
rs/UpdateEvent
|
||||||
(-apply-update [_ state]
|
(-apply-update [_ state]
|
||||||
(update-in state [:dashboard :selected] conj id)))
|
(update-in state [:dashboard :images :selected] conj id)))
|
||||||
|
|
||||||
(defrecord DeselectImage [id]
|
(defrecord DeselectImage [id]
|
||||||
rs/UpdateEvent
|
rs/UpdateEvent
|
||||||
(-apply-update [_ state]
|
(-apply-update [_ state]
|
||||||
(update-in state [:dashboard :selected] disj id)))
|
(update-in state [:dashboard :images :selected] disj id)))
|
||||||
|
|
||||||
(defrecord ToggleImageSelection [id]
|
(defrecord ToggleImageSelection [id]
|
||||||
rs/WatchEvent
|
rs/WatchEvent
|
||||||
(-apply-watch [_ state stream]
|
(-apply-watch [_ state stream]
|
||||||
(let [selected (get-in state [:dashboard :selected])]
|
(let [selected (get-in state [:dashboard :images :selected])]
|
||||||
(rx/of
|
(rx/of
|
||||||
(if (selected id)
|
(if (selected id)
|
||||||
(DeselectImage. id)
|
(DeselectImage. id)
|
||||||
|
@ -305,9 +306,9 @@
|
||||||
(defrecord DeleteSelected []
|
(defrecord DeleteSelected []
|
||||||
rs/WatchEvent
|
rs/WatchEvent
|
||||||
(-apply-watch [_ state stream]
|
(-apply-watch [_ state stream]
|
||||||
(let [{:keys [id selected]} (get state :dashboard)]
|
(let [{:keys [id selected]} (get-in state [:dashboard :images])]
|
||||||
(rx/of (remove-images id selected)
|
(rx/of (remove-images id selected)
|
||||||
#(assoc-in % [:dashboard :selected] #{})))))
|
#(assoc-in % [:dashboard :images :selected] #{})))))
|
||||||
|
|
||||||
(defn delete-selected
|
(defn delete-selected
|
||||||
[]
|
[]
|
||||||
|
@ -318,7 +319,7 @@
|
||||||
(defrecord UpdateOpts [order filter]
|
(defrecord UpdateOpts [order filter]
|
||||||
rs/UpdateEvent
|
rs/UpdateEvent
|
||||||
(-apply-update [_ state]
|
(-apply-update [_ state]
|
||||||
(update state :dashboard merge
|
(update-in state [:dashboard :images] merge
|
||||||
(when order {:order order})
|
(when order {:order order})
|
||||||
(when filter {:filter filter}))))
|
(when filter {:filter filter}))))
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@
|
||||||
[uxbox.main.data.lightbox :as udl]
|
[uxbox.main.data.lightbox :as udl]
|
||||||
[uxbox.main.data.images :as di]
|
[uxbox.main.data.images :as di]
|
||||||
[uxbox.main.ui.icons :as i]
|
[uxbox.main.ui.icons :as i]
|
||||||
[uxbox.util.mixins :as mx]
|
[uxbox.util.mixins :as mx :include-macros true]
|
||||||
[uxbox.main.ui.lightbox :as lbx]
|
[uxbox.main.ui.lightbox :as lbx]
|
||||||
[uxbox.main.ui.keyboard :as kbd]
|
[uxbox.main.ui.keyboard :as kbd]
|
||||||
[uxbox.main.ui.library-bar :as ui.library-bar]
|
[uxbox.main.ui.library-bar :as ui.library-bar]
|
||||||
|
@ -54,11 +54,11 @@
|
||||||
;; --- Refs
|
;; --- Refs
|
||||||
|
|
||||||
(def ^:private dashboard-ref
|
(def ^:private dashboard-ref
|
||||||
(-> (l/key :dashboard)
|
(-> (l/in [:dashboard :images])
|
||||||
(l/derive st/state)))
|
(l/derive st/state)))
|
||||||
|
|
||||||
(def ^:private collections-map-ref
|
(def ^:private collections-map-ref
|
||||||
(-> (comp (l/key :images-by-id)
|
(-> (comp (l/key :image-colls-by-id)
|
||||||
(ul/merge library/+image-collections-by-id+))
|
(ul/merge library/+image-collections-by-id+))
|
||||||
(l/derive st/state)))
|
(l/derive st/state)))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue