From 5e7d3536279edfb7c0a93ee12dca8ac4f3bc416e Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Thu, 24 Dec 2015 19:34:46 +0200 Subject: [PATCH] Add color collections related events. --- frontend/uxbox/data/dashboard.cljs | 78 +++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/frontend/uxbox/data/dashboard.cljs b/frontend/uxbox/data/dashboard.cljs index 10fdd1cef..f405e678a 100644 --- a/frontend/uxbox/data/dashboard.cljs +++ b/frontend/uxbox/data/dashboard.cljs @@ -86,9 +86,85 @@ (reify rs/UpdateEvent (-apply-update [_ state] - (println "set-collection" id) (assoc-in state [:dashboard :collection-id] id)) IPrintWithWriter (-pr-writer [mv writer _] (-write writer "#")))) + +(defn mk-color-collection + [] + (reify + rs/UpdateEvent + (-apply-update [_ state] + (let [id (random-uuid) + coll {:name "Unnamed collection" + :id id :colors #{}}] + (-> state + (assoc-in [:colors-by-id id] coll) + (assoc-in [:dashboard :collection-id] id) + (assoc-in [:dashboard :collection-type] :own)))) + + IPrintWithWriter + (-pr-writer [mv writer _] + (-write writer "#")))) + +(defn rename-color-collection + [id name] + (reify + rs/UpdateEvent + (-apply-update [_ state] + (assoc-in state [:colors-by-id id :name] name)) + + IPrintWithWriter + (-pr-writer [mv writer _] + (-write writer "#")))) + +(defn delete-color-collection + [id] + (reify + rs/UpdateEvent + (-apply-update [_ state] + (let [state (update state :colors-by-id dissoc id) + colls (sort-by :id (vals (:colors-by-id state)))] + (assoc-in state [:dashboard :collection-id] (:id (first colls))))) + + IPrintWithWriter + (-pr-writer [mv writer _] + (-write writer "#")))) + +(defn replace-color + "Add or replace color in a collection." + [{:keys [id from to] :as params}] + (sc/validate! +color-replace-schema+ params) + (reify + rs/UpdateEvent + (-apply-update [_ state] + (if-let [colors (get-in state [:colors-by-id id :colors])] + (as-> colors $ + (disj $ from) + (conj $ to) + (assoc-in state [:colors-by-id id :colors] $)) + state)) + + IPrintWithWriter + (-pr-writer [mv writer _] + (-write writer "#")))) + +(defn remove-color + "Remove color in a collection." + [{:keys [id color] :as params}] + (sc/validate! +remove-color-schema+ params) + (reify + rs/UpdateEvent + (-apply-update [_ state] + (if-let [colors (get-in state [:colors-by-id id :colors])] + (as-> colors $ + (disj $ color) + (assoc-in state [:colors-by-id id :colors] $)) + state)) + + IPrintWithWriter + (-pr-writer [mv writer _] + (-write writer "#")))) +