0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-14 11:09:04 -05:00

Improve state usage of colors page and its events.

This commit is contained in:
Andrey Antukh 2016-09-27 09:28:14 +03:00
parent 9af2e54a2c
commit 5bf3e9a06e
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95
2 changed files with 22 additions and 21 deletions

View file

@ -21,13 +21,13 @@
[state coll] [state coll]
(let [id (:id coll) (let [id (:id coll)
coll (assoc coll :type :own)] coll (assoc coll :type :own)]
(assoc-in state [:colors-by-id id] coll))) (assoc-in state [:color-colls-by-id id] coll)))
(defn- dissoc-collection (defn- dissoc-collection
"A reduce function for dissoc the color collection "A reduce function for dissoc the color collection
to the state map." to the state map."
[state id] [state id]
(update state :colors-by-id dissoc id)) (update state :color-colls-by-id dissoc id))
;; --- Initialize ;; --- Initialize
@ -39,18 +39,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/colors}] (-> state
(assoc state :dashboard data))) (assoc-in [:dashboard :colors] data)
(assoc-in [:dashboard :section] :dashboard/colors))))
;; rs/EffectEvent ;; rs/EffectEvent
;; (-apply-effect [_ state] ;; (-apply-effect [_ state]
;; (when (nil? (:colors-by-id state)) ;; (when (nil? (:color-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? (:colors-by-id state)) (if (nil? (:color-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)
@ -114,8 +115,8 @@
(-apply-update [_ state] (-apply-update [_ state]
(-> state (-> state
(assoc-collection item) (assoc-collection item)
(assoc-in [:dashboard :collection-id] (:id item)) (assoc-in [:dashboard :colors :id] (:id item))
(assoc-in [:dashboard :collection-type] :own)))) (assoc-in [:dashboard :colors :type] :own))))
(defn collection-created (defn collection-created
[item] [item]
@ -152,7 +153,7 @@
(defrecord UpdateCollection [id] (defrecord UpdateCollection [id]
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state s] (-apply-watch [_ state s]
(let [item (get-in state [:colors-by-id id])] (let [item (get-in state [:color-colls-by-id id])]
(->> (rp/req :update/color-collection item) (->> (rp/req :update/color-collection item)
(rx/map :payload) (rx/map :payload)
(rx/map collection-updated))))) (rx/map collection-updated)))))
@ -166,7 +167,7 @@
(defrecord RenameCollection [id name] (defrecord RenameCollection [id name]
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(assoc-in state [:colors-by-id id :name] name)) (assoc-in state [:color-colls-by-id id :name] name))
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state s] (-apply-watch [_ state s]
@ -198,7 +199,7 @@
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(let [replacer #(-> (disj % from) (conj to))] (let [replacer #(-> (disj % from) (conj to))]
(update-in state [:colors-by-id id :data] (fnil replacer #{})))) (update-in state [:color-colls-by-id id :data] (fnil replacer #{}))))
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state s] (-apply-watch [_ state s]
@ -214,7 +215,7 @@
(defrecord RemoveColors [id colors] (defrecord RemoveColors [id colors]
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(update-in state [:colors-by-id id :data] (update-in state [:color-colls-by-id id :data]
#(set/difference % colors))) #(set/difference % colors)))
rs/WatchEvent rs/WatchEvent
@ -231,17 +232,17 @@
(defrecord SelectColor [color] (defrecord SelectColor [color]
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(update-in state [:dashboard :selected] conj color))) (update-in state [:dashboard :colors :selected] conj color)))
(defrecord DeselectColor [color] (defrecord DeselectColor [color]
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(update-in state [:dashboard :selected] disj color))) (update-in state [:dashboard :colors :selected] disj color)))
(defrecord ToggleColorSelection [color] (defrecord ToggleColorSelection [color]
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 :colors :selected])]
(rx/of (rx/of
(if (selected color) (if (selected color)
(DeselectColor. color) (DeselectColor. color)
@ -257,9 +258,9 @@
(defrecord DeleteSelectedColors [] (defrecord DeleteSelectedColors []
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 :colors])]
(rx/of (remove-colors id selected) (rx/of (remove-colors id selected)
#(assoc-in % [:dashboard :selected] #{}))))) #(assoc-in % [:dashboard :colors :selected] #{})))))
(defn delete-selected-colors (defn delete-selected-colors
[] []

View file

@ -23,18 +23,18 @@
[uxbox.util.dom :as dom] [uxbox.util.dom :as dom]
[uxbox.util.i18n :as t :refer (tr)] [uxbox.util.i18n :as t :refer (tr)]
[uxbox.util.lens :as ul] [uxbox.util.lens :as ul]
[uxbox.util.mixins :as mx] [uxbox.util.mixins :as mx :include-macros true]
[uxbox.util.rstore :as rs] [uxbox.util.rstore :as rs]
[uxbox.util.schema :as sc])) [uxbox.util.schema :as sc]))
;; --- Refs ;; --- Refs
(def ^:private dashboard-ref (def ^:private dashboard-ref
(-> (l/key :dashboard) (-> (l/in [:dashboard :colors])
(l/derive st/state))) (l/derive st/state)))
(def ^:private collections-map-ref (def ^:private collections-map-ref
(-> (comp (l/key :colors-by-id) (-> (comp (l/key :color-colls-by-id)
(ul/merge library/+color-collections-by-id+)) (ul/merge library/+color-collections-by-id+))
(l/derive st/state))) (l/derive st/state)))