From 5624adcedff3ba56fa24a17b46b530be78fd562b Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Tue, 12 Apr 2016 22:23:16 +0300 Subject: [PATCH] Remove some validations. --- src/uxbox/data/auth.cljs | 5 --- src/uxbox/data/dashboard.cljs | 23 +--------- src/uxbox/data/pages.cljs | 13 +++--- src/uxbox/data/projects.cljs | 41 ++++++++++++------ src/uxbox/data/shapes.cljs | 63 --------------------------- src/uxbox/data/users.cljs | 1 - src/uxbox/schema.cljs | 6 ++- src/uxbox/ui/dashboard/colors.cljs | 69 +++++++++++++++--------------- 8 files changed, 75 insertions(+), 146 deletions(-) diff --git a/src/uxbox/data/auth.cljs b/src/uxbox/data/auth.cljs index 9b70b6437..664b275d1 100644 --- a/src/uxbox/data/auth.cljs +++ b/src/uxbox/data/auth.cljs @@ -64,13 +64,8 @@ (dp/fetch-projects) (udu/fetch-profile)))))))) -(def ^:const ^:private +login-schema+ - {:username [sc/required sc/string] - :password [sc/required sc/string]}) - (defn login [params] - (sc/validate! +login-schema+ params) (map->Login params)) ;; --- Logout diff --git a/src/uxbox/data/dashboard.cljs b/src/uxbox/data/dashboard.cljs index 6ca4034c3..4a0e88872 100644 --- a/src/uxbox/data/dashboard.cljs +++ b/src/uxbox/data/dashboard.cljs @@ -12,22 +12,7 @@ [uxbox.schema :as sc] [uxbox.repo :as rp])) -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Schemas -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - -(def ^:static +color-replace-schema+ - {:id [sc/required sc/uuid] - :from [sc/color] - :to [sc/required sc/color]}) - -(def ^:static +remove-color-schema+ - {:id [sc/required sc/uuid] - :color [sc/required sc/color]}) - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Helpers -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; --- Helpers (defn assoc-page "A reduce function for assoc the page @@ -36,9 +21,7 @@ (let [uuid (:id page)] (update-in state [:pages-by-id] assoc uuid page))) -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Events -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; --- Events (defn merge-if-not-exists [map & maps] @@ -139,7 +122,6 @@ (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] @@ -153,7 +135,6 @@ (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] diff --git a/src/uxbox/data/pages.cljs b/src/uxbox/data/pages.cljs index 649544552..0ec5dcc6d 100644 --- a/src/uxbox/data/pages.cljs +++ b/src/uxbox/data/pages.cljs @@ -74,7 +74,7 @@ (rx/mapcat on-created) (rx/catch on-failed)))))) -(def ^:static +create-page-schema+ +(def ^:private create-page-schema {:name [sc/required sc/string] :layout [sc/required sc/string] :width [sc/required sc/integer] @@ -83,8 +83,8 @@ (defn create-page [data] - (sc/validate! +create-page-schema+ data) - (map->CreatePage data)) + (-> (sc/validate! data create-page-schema) + (map->CreatePage))) ;; --- Sync Page @@ -171,7 +171,7 @@ (rx/map on-success) (rx/catch on-failure))))) -(def ^:const +update-page-schema+ +(def ^:private update-page-schema {:name [sc/required sc/string] :width [sc/required sc/integer] :height [sc/required sc/integer] @@ -179,8 +179,9 @@ (defn update-page-metadata [data] - (sc/validate! +update-page-schema+ data) - (map->UpdatePageMetadata (dissoc data :data))) + (-> (sc/validate! data update-page-schema) + (dissoc :data) + (map->UpdatePageMetadata))) ;; --- Delete Page (by id) diff --git a/src/uxbox/data/projects.cljs b/src/uxbox/data/projects.cljs index b1e9e0dd2..38d911214 100644 --- a/src/uxbox/data/projects.cljs +++ b/src/uxbox/data/projects.cljs @@ -70,33 +70,48 @@ [] (FetchProjects.)) +;; --- Project Created + +(defrecord ProjectCreated [project] + rs/UpdateEvent + (-apply-update [_ state] + (stpr/assoc-project state project))) + +(defn project-created + [data] + (ProjectCreated. data)) + ;; --- Create Project (defrecord CreateProject [name width height layout] rs/WatchEvent (-apply-watch [this state s] - (letfn [(on-success [project] - (rx/of (rs/swap #(stpr/assoc-project % project)) - (udp/create-page (assoc (into {} this) - :project (:id project) - :name "Page 1" - :data nil)))) + (letfn [(on-success [{project :payload}] + (rx/of + (project-created project) + (udp/create-page {:width width + :height height + :layout layout + :project (:id project) + :name "Page 1" + :data nil}))) (on-failure [err] - (uum/error (tr "errors.create-project")))] + (uum/error (tr "errors.create-project")) + (rx/empty))] (->> (rp/req :create/project {:name name}) - (rx/mapcat on-success) - (rx/catch on-failure))))) + (rx/catch on-failure) + (rx/mapcat on-success))))) -(def ^:static +project-schema+ +(def ^:private create-project-schema {:name [sc/required sc/string] :width [sc/required sc/integer] :height [sc/required sc/integer] :layout [sc/required sc/string]}) (defn create-project - [{:keys [name width height layout] :as data}] - (sc/validate! +project-schema+ data) - (map->CreateProject data)) + [params] + (-> (sc/validate! params create-project-schema) + (map->CreateProject))) ;; --- Delete Project (by id) diff --git a/src/uxbox/data/shapes.cljs b/src/uxbox/data/shapes.cljs index 24a522cb5..c7de4ab5e 100644 --- a/src/uxbox/data/shapes.cljs +++ b/src/uxbox/data/shapes.cljs @@ -19,63 +19,9 @@ [uxbox.util.geom.point :as gpt] [uxbox.util.data :refer (index-of)])) -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Schemas -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - -(def ^:static +shape-schema+ - {:x [sc/integer] - :y [sc/integer] - :width [sc/integer] - :height [sc/integer] - :type [sc/required sc/shape-type]}) - -(def ^:static +shape-size-schema+ - {:width [sc/integer] - :height [sc/integer] - :lock [sc/boolean]}) - -(def ^:static +shape-fill-attrs-schema+ - {:color [sc/color] - :opacity [sc/number]}) - -(def ^:static +shape-stroke-attrs-schema+ - {:color [sc/color] - :width [sc/integer] - :type [sc/keyword] - :opacity [sc/number]}) - -(def ^:static +shape-line-attrs-schema+ - {:x1 [sc/integer] - :y1 [sc/integer] - :x2 [sc/integer] - :y2 [sc/integer]}) - -(def ^:static +shape-font-attrs-schema+ - {:family [sc/string] - :style [sc/string] - :weight [sc/string] - :align [sc/string] - :size [sc/number] - :letter-spacing [sc/number] - :line-height [sc/number]}) - -(def ^:static +shape-radius-attrs-schema+ - {:rx [sc/integer] - :ry [sc/integer]}) - -(def ^:static +shape-position-schema+ - {:x [sc/integer] - :y [sc/integer]}) - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Events (explicit) -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - (defn add-shape "Create and add shape to the current selected page." [shape] - (sc/validate! +shape-schema+ shape) (reify udp/IPageUpdate rs/UpdateEvent @@ -113,7 +59,6 @@ (defn update-line-attrs [sid {:keys [x1 y1 x2 y2] :as opts}] - (sc/validate! +shape-line-attrs-schema+ opts) (reify udp/IPageUpdate rs/UpdateEvent @@ -144,7 +89,6 @@ WARN: only works with shapes that works with height and width such are ::rect" [sid {:keys [width height] :as opts}] - (sc/validate! +shape-size-schema+ opts) (reify udp/IPageUpdate rs/UpdateEvent @@ -164,7 +108,6 @@ (defn update-position "Update the start position coordenate of the shape." [sid {:keys [x y] :as opts}] - (sc/validate! +shape-position-schema+ opts) (reify rs/UpdateEvent (-apply-update [_ state] @@ -182,7 +125,6 @@ (defn update-fill-attrs [sid {:keys [color opacity] :as opts}] - (sc/validate! +shape-fill-attrs-schema+ opts) (reify udp/IPageUpdate rs/UpdateEvent @@ -195,7 +137,6 @@ (defn update-font-attrs [sid {:keys [family style weight size align letter-spacing line-height] :as opts}] - (sc/validate! +shape-font-attrs-schema+ opts) (reify udp/IPageUpdate rs/UpdateEvent @@ -212,7 +153,6 @@ (defn update-stroke-attrs [sid {:keys [color opacity type width] :as opts}] - (sc/validate! +shape-stroke-attrs-schema+ opts) (reify udp/IPageUpdate rs/UpdateEvent @@ -226,7 +166,6 @@ (defn update-radius-attrs [sid {:keys [rx ry] :as opts}] - (sc/validate! +shape-radius-attrs-schema+ opts) (reify udp/IPageUpdate rs/UpdateEvent @@ -463,7 +402,6 @@ "Update the fill related attributed on selected shapes." [opts] - (sc/validate! +shape-fill-attrs-schema+ opts) (reify rs/WatchEvent (-apply-watch [_ state s] @@ -476,7 +414,6 @@ "Update the fill related attributed on selected shapes." [opts] - (sc/validate! +shape-stroke-attrs-schema+ opts) (reify rs/WatchEvent (-apply-watch [_ state s] diff --git a/src/uxbox/data/users.cljs b/src/uxbox/data/users.cljs index b3ca6f604..5c0f63c56 100644 --- a/src/uxbox/data/users.cljs +++ b/src/uxbox/data/users.cljs @@ -94,7 +94,6 @@ (defn update-password [data] (let [[errors data] (sc/validate data update-password-schema)] - (println errors) (if errors (forms/assign-errors :profile/password errors) (UpdatePassword. data)))) diff --git a/src/uxbox/schema.cljs b/src/uxbox/schema.cljs index ef8d6f3b2..183169dc3 100644 --- a/src/uxbox/schema.cljs +++ b/src/uxbox/schema.cljs @@ -77,8 +77,10 @@ (defn validate! [data schema] - (when-let [errors (first (validate schema data))] - (throw (ex-info "Invalid data" errors)))) + (let [[errors data] (validate data schema)] + (if errors + (throw (ex-info "Invalid data" errors)) + data))) (defn valid? [data schema] diff --git a/src/uxbox/ui/dashboard/colors.cljs b/src/uxbox/ui/dashboard/colors.cljs index 7cde68edd..35e9881d0 100644 --- a/src/uxbox/ui/dashboard/colors.cljs +++ b/src/uxbox/ui/dashboard/colors.cljs @@ -187,44 +187,43 @@ ;; --- Colors Create / Edit Lightbox -(def ^:const ^:private +color-form-schema+ - {:hex [sc/required sc/color]}) - (defn- color-lightbox-render [own {:keys [coll color]}] - (let [local (:rum/local own)] - (letfn [(submit [e] - (if-let [errors (sc/validate +color-form-schema+ @local)] - (swap! local assoc :errors errors) - (let [params {:id (:id coll) :from color - :to (:hex @local)}] - (rs/emit! (dd/replace-color params)) - (lightbox/close!)))) - (on-change [e] - (let [value (str/trim (dom/event->value e))] - (swap! local assoc :hex value)))] - (html - [:div.lightbox-body - [:h3 "New color"] - [:form - [:div.row-flex - [:input#color-hex.input-text - {:placeholder "#" - :class (form/error-class local :hex) - :on-change on-change - :value (or (:hex @local) color "") - :type "text"}]] - [:div.row-flex.center.color-picker-default - (colorpicker - :value (or (:hex @local) color "#00ccff") - :on-change #(swap! local assoc :hex %))] + (html + [:p "TODO"])) + ;; (let [local (:rum/local own)] + ;; (letfn [(submit [e] + ;; (if-let [errors (sc/validate +color-form-schema+ @local)] + ;; (swap! local assoc :errors errors) + ;; (let [params {:id (:id coll) :from color + ;; :to (:hex @local)}] + ;; (rs/emit! (dd/replace-color params)) + ;; (lightbox/close!)))) + ;; (on-change [e] + ;; (let [value (str/trim (dom/event->value e))] + ;; (swap! local assoc :hex value)))] + ;; (html + ;; [:div.lightbox-body + ;; [:h3 "New color"] + ;; [:form + ;; [:div.row-flex + ;; [:input#color-hex.input-text + ;; {:placeholder "#" + ;; :class (form/error-class local :hex) + ;; :on-change on-change + ;; :value (or (:hex @local) color "") + ;; :type "text"}]] + ;; [:div.row-flex.center.color-picker-default + ;; (colorpicker + ;; :value (or (:hex @local) color "#00ccff") + ;; :on-change #(swap! local assoc :hex %))] - [:input#project-btn.btn-primary - {:value "+ Add color" - :on-click submit - :type "button"}]] - [:a.close {:on-click #(lightbox/close!)} - i/close]])))) + ;; [:input#project-btn.btn-primary + ;; {:value "+ Add color" + ;; :on-click submit + ;; :type "button"}]] + ;; [:a.close {:on-click #(lightbox/close!)} + ;; i/close]])))) (def color-lightbox (mx/component